The Java EE 6 Tutorial

A Message Acknowledgment Example

The AckEquivExample.java client shows how both of the following two scenarios ensure that a message will not be acknowledged until processing of it is complete:

With a message listener, the automatic acknowledgment happens when the onMessage method returns (that is, after message processing has finished). With a synchronous receiver, the client acknowledges the message after processing is complete. If you use AUTO_ACKNOWLEDGE with a synchronous receive, the acknowledgment happens immediately after the receive call; if any subsequent processing steps fail, the message cannot be redelivered.

The example is in the following directory:

tut-install/examples/jms/advanced/ackequivexample/src/java/

The example contains an AsynchSubscriber class with a TextListener class, a MultiplePublisher class, a SynchReceiver class, a SynchSender class, a main method, and a method that runs the other classes’ threads.

The example uses the following objects:

To create the new queue and connection factory, you can use Ant targets defined in the file tut-install/examples/jms/advanced/ackequivexample/build.xml.

ProcedureTo Build, Package, Deploy, and Run the ackequivexample Using NetBeans IDE

  1. In a terminal window, go to the following directory:

    tut-install/examples/jms/advanced/ackequivexample/
    
  2. To create the objects needed in this example, type the following commands:


    ant create-control-queue
    ant create-durable-cf
    
  3. To build and package the client, follow these steps.

    1. In NetBeans IDE, select File->Open Project.

    2. In the Open Project dialog, navigate to:


      tut-install/examples/jms/advanced/
      
    3. Select the ackequivexample folder.

    4. Select the Open as Main Project check box.

    5. Click Open Project.

    6. In the Projects tab, right-click the project and select Build.

  4. To run the client, right-click the ackequivexample project and select Run.

    The client output looks something like this (along with some application client container output):


    Queue name is jms/ControlQueue
    Queue name is jms/Queue
    Topic name is jms/Topic
    Connection factory name is jms/DurableConnectionFactory
      SENDER: Created client-acknowledge session
      SENDER: Sending message: Here is a client-acknowledge message
      RECEIVER: Created client-acknowledge session
      RECEIVER: Processing message: Here is a client-acknowledge message
      RECEIVER: Now I’ll acknowledge the message
    SUBSCRIBER: Created auto-acknowledge session
    SUBSCRIBER: Sending synchronize message to control queue
    PUBLISHER: Created auto-acknowledge session
    PUBLISHER: Receiving synchronize messages from control queue; count = 1
    PUBLISHER: Received synchronize message;  expect 0 more
    PUBLISHER: Publishing message: Here is an auto-acknowledge message 1
    PUBLISHER: Publishing message: Here is an auto-acknowledge message 2
    SUBSCRIBER: Processing message: Here is an auto-acknowledge message 1
    PUBLISHER: Publishing message: Here is an auto-acknowledge message 3
    SUBSCRIBER: Processing message: Here is an auto-acknowledge message 2
    SUBSCRIBER: Processing message: Here is an auto-acknowledge message 3
  5. After you run the client, you can delete the destination resource jms/ControlQueue. Go to the directory tut-install/examples/jms/advanced/ackequivexample/ and type the following command:


    ant delete-control-queue
    

    You will need the other resources for other examples.

ProcedureTo Build, Package, Deploy, and Run ackequivexample Using Ant

  1. In a terminal window, go to the following directory:

    tut-install/examples/jms/advanced/ackequivexample/
    
  2. To create the objects needed in this example, type the following commands:


    ant create-control-queue
    ant create-durable-cf
    
  3. To compile and package the client, type the following command:


    ant
    
  4. To deploy the client JAR file to the GlassFish Server, then retrieve the client stubs, type the following command:


    ant getclient
    

    Ignore the message that states that the application is deployed at a URL.

  5. Because this example takes no command-line arguments, you can run the client using the following command:


    ant run
    

    Alternatively, you can type the following command:


    appclient -client client-jar/ackequivexampleClient.jar
    

    The client output looks something like this (along with some application client container output):


    Queue name is jms/ControlQueue
    Queue name is jms/Queue
    Topic name is jms/Topic
    Connection factory name is jms/DurableConnectionFactory
      SENDER: Created client-acknowledge session
      SENDER: Sending message: Here is a client-acknowledge message
      RECEIVER: Created client-acknowledge session
      RECEIVER: Processing message: Here is a client-acknowledge message
      RECEIVER: Now I’ll acknowledge the message
    SUBSCRIBER: Created auto-acknowledge session
    SUBSCRIBER: Sending synchronize message to control queue
    PUBLISHER: Created auto-acknowledge session
    PUBLISHER: Receiving synchronize messages from control queue; count = 1
    PUBLISHER: Received synchronize message;  expect 0 more
    PUBLISHER: Publishing message: Here is an auto-acknowledge message 1
    PUBLISHER: Publishing message: Here is an auto-acknowledge message 2
    SUBSCRIBER: Processing message: Here is an auto-acknowledge message 1
    PUBLISHER: Publishing message: Here is an auto-acknowledge message 3
    SUBSCRIBER: Processing message: Here is an auto-acknowledge message 2
    SUBSCRIBER: Processing message: Here is an auto-acknowledge message 3
  6. After you run the client, you can delete the destination resource jms/ControlQueue. Go to the directory tut-install/examples/jms/advanced/ackequivexample/ and type the following command:


    ant delete-control-queue
    

    You will need the other resources for other examples.