A Simple Example of Asynchronous Message Consumption

This section describes the receiving programs in an example that uses
a message listener to consume messages asynchronously. This section then explains
how to compile and run the programs using the Application Server.

The following sections describe the steps in creating and running the
example:

Compiling and Packaging the AsynchConsumer Client

To compile and package the AsynchConsumer example
using NetBeans IDE, follow these steps:

In NetBeans IDE, choose Open Project from the File menu.

In the Open Project dialog, navigate to tut-install/javaeetutorial5/examples/jms/simple/.

Select the asynchconsumer folder.

Select the Open as Main Project check box.

Click Open Project.

Right-click the project and choose Build.

To compile and package the AsynchConsumer example
using Ant, follow these steps:

In a terminal window, go to the asynchconsumer directory:

cd ../../asynchconsumer

Type the following command:

ant

The targets package both the main class and the message listener class
in the JAR file and place the file in the dist directory
for the example.

Running the Clients for the Asynchronous Receive
Example

To run the programs using NetBeans IDE, follow these steps.

Run the AsynchConsumer example:

Right-click the asynchconsumer project
and choose Properties.

Select Run from the Categories tree.

In the Arguments field, type the following:

topic

Click OK.

Right-click the project and choose Run.

The program
displays the following lines and appears to hang:

Destination type is topic
To end program, type Q or q, then <return>

Now run the Producer example:

Right-click the producer project and choose
Properties.

Select Run from the Categories tree.

In the Arguments field, type the following:

topic 3

Click OK.

Right-click the project and choose Run.

The output
of the program looks like this:

Destination type is topic
Sending message: This is message 1
Sending message: This is message 2
Sending message: This is message 3

In the other window, the AsynchConsumer program displays
the following:

Destination type is topic
To end program, type Q or q, then <return>
Reading message: This is message 1
Reading message: This is message 2
Reading message: This is message 3
Message is not a TextMessage

The last line appears because the program has received the non-text
control message sent by the Producer program.

Type Q or q in the Output
window and press Return to stop the program.

Now run the programs using a queue. In this case, as with
the synchronous example, you can run the Producer program
first, because there is no timing dependency between the sender and receiver.

Right-click the producer project and choose
Properties.

Select Run from the Categories tree.

In the Arguments field, type the following:

queue 3

Click OK.

Right-click the project and choose Run.

The output
of the program looks like this:

Destination type is queue
Sending message: This is message 1
Sending message: This is message 2
Sending message: This is message 3

Run the AsynchConsumer program.

Right-click the asynchconsumer project
and choose Properties.

Select Run from the Categories tree.

In the Arguments field, type the following:

queue

Click OK.

Right-click the project and choose Run.

The output
of the program looks like this:

Destination type is queue
To end program, type Q or q, then <return>
Reading message: This is message 1
Reading message: This is message 2
Reading message: This is message 3
Message is not a TextMessage

Type Q or q in the Output
window and press Return to stop the program.

To run the clients using the appclient command, follow
these steps:

Run the AsynchConsumer program, specifying
the topic destination type.

cd dist
appclient -client asynchconsumer.jar topic

The program displays the following lines and appears to hang:

Destination type is topic
To end program, type Q or q, then <return>

In the terminal window where you ran the Producer program
previously, run the program again, sending three messages. The command looks
like this:

appclient -client producer.jar topic 3

The output of the program looks like this:

Destination type is topic
Sending message: This is message 1
Sending message: This is message 2
Sending message: This is message 3

In the other window, the AsynchConsumer program displays
the following:

Destination type is topic
To end program, type Q or q, then <return>
Reading message: This is message 1
Reading message: This is message 2
Reading message: This is message 3
Message is not a TextMessage

The last line appears because the program has received the non-text
control message sent by the Producer program.

Type Q or q and press
Return to stop the program.

Now run the programs using a queue. In this case, as with
the synchronous example, you can run the Producer program
first, because there is no timing dependency between the sender and receiver:

appclient -client producer.jar queue 3

The output of the program looks like this:

Destination type is queue
Sending message: This is message 1
Sending message: This is message 2
Sending message: This is message 3

Run the AsynchConsumer program:

appclient -client asynchconsumer.jar queue

The output of the program looks like this:

Destination type is queue
To end program, type Q or q, then <return>
Reading message: This is message 1
Reading message: This is message 2
Reading message: This is message 3
Message is not a TextMessage