background image

Synchronous Receive Example

<< Writing Simple JMS Client Applications | Creates a Connection and a Session >>
<< Writing Simple JMS Client Applications | Creates a Connection and a Session >>

Synchronous Receive Example

The following sections describe the steps in creating and running the example:
"Writing the Client Programs for the Synchronous Receive Example" on page 912
"Starting the JMS Provider" on page 915
"Creating JMS Administered Objects for the Synchronous Receive Example" on page 915
"Compiling and Packaging the Clients for the Synchronous Receive Example" on page 915
"Running the Clients for the Synchronous Receive Example" on page 917
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