The Java EE 6 Tutorial

A Durable Subscription Example

The DurableSubscriberExample.java example shows how durable subscriptions work. It demonstrates that a durable subscription is active even when the subscriber is not active. The example contains a DurableSubscriber class, a MultiplePublisher class, a main method, and a method that instantiates the classes and calls their methods in sequence.

The example is in the following directory:

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

The example begins in the same way as any publish/subscribe client: The subscriber starts, the publisher publishes some messages, and the subscriber receives them. At this point, the subscriber closes itself. The publisher then publishes some messages while the subscriber is not active. The subscriber then restarts and receives the messages.

ProcedureTo Build, Package, Deploy, and Run durablesubscriberexample Using NetBeans IDE

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

    tut-install/examples/jms/advanced/durablesubscriberexample/
    
  2. If you did not do so for A Message Acknowledgment Example, create a connection factory named jms/DurableConnectionFactory:


    ant create-durable-cf
    
  3. To compile 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 durablesubscriberexample 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 durablesubscriberexample project and select Run.

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


    Connection factory without client ID is jms/ConnectionFactory
    Connection factory with client ID is jms/DurableConnectionFactory
    Topic name is jms/Topic
    Starting subscriber
    PUBLISHER: Publishing message: Here is a message 1
    SUBSCRIBER: Reading message: Here is a message 1
    PUBLISHER: Publishing message: Here is a message 2
    SUBSCRIBER: Reading message: Here is a message 2
    PUBLISHER: Publishing message: Here is a message 3
    SUBSCRIBER: Reading message: Here is a message 3
    Closing subscriber
    PUBLISHER: Publishing message: Here is a message 4
    PUBLISHER: Publishing message: Here is a message 5
    PUBLISHER: Publishing message: Here is a message 6
    Starting subscriber
    SUBSCRIBER: Reading message: Here is a message 4
    SUBSCRIBER: Reading message: Here is a message 5
    SUBSCRIBER: Reading message: Here is a message 6
    Closing subscriber
    Unsubscribing from durable subscription
  5. After you run the client, you can delete the connection factory jms/DurableConnectionFactory. Go to the directory tut-install/examples/jms/advanced/durablesubscriberexample/ and type the following command:


    ant delete-durable-cf
    

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

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

    tut-install/examples/jms/advanced/durablesubscriberexample/
    
  2. If you did not do so for A Message Acknowledgment Example, create a connection factory named jms/DurableConnectionFactory:


    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/durablesubscriberexampleClient.jar
    
  6. After you run the client, you can delete the connection factory jms/DurableConnectionFactory. Go to the directory tut-install/examples/jms/advanced/durablesubscriberexample/ and type the following command:


    ant delete-durable-cf