Synchronous Receive Example
Synchronous Receive Example
The following sections describe the steps in creating and running the example:
Writing the Client Programs for the Synchronous Receive Example
The sending program, producer/src/java/Producer.java, performs the following steps:
1. Injects resources for a connection factory, queue, and topic:
@Resource(mappedName=
"jms/ConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(mappedName=
"jms/Queue")private static Queue queue;
@Resource(mappedName=
"jms/Topic")private static Topic topic;
2. Retrieves and verifies command-line arguments that specify the destination type and the
number of arguments:
final int NUM_MSGS;
String destType = args[0];
System.out.println(
"Destination type is " + destType);
if ( ! ( destType.equals(
"queue") || destType.equals("topic") ) ) {
System.err.println(
"Argument must be \"queue\" or " + "\"topic\"");
System.exit(1);
}
if (args.length == 2){
NUM_MSGS = (new Integer(args[1])).intValue();
}
else {
NUM_MSGS = 1;
}
3. Assigns either the queue or topic to a destination object, based on the specified destination
type:
Destination dest = null;
try {
if (destType.equals(
"queue")) {
dest = (Destination) queue;
} else {
dest = (Destination) topic;
}
}
catch (Exception e) {
System.err.println(
"Error setting destination: " + e.toString());
Writing Simple JMS Client Applications
The Java EE 5 Tutorial · September 2007
912