The Java EE 5 Tutorial

Running the Clients for the Synchronous Receive Example

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

  1. Run the Producer example:

    1. Right-click the producer project and choose Properties.

    2. Select Run from the Categories tree.

    3. In the Arguments field, type the following:


      queue 3
      
    4. Click OK.

    5. 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

      The messages are now in the queue, waiting to be received.

  2. Now run the SynchConsumer example:

    1. Right-click the synchconsumer project and choose Properties.

    2. Select Run from the Categories tree.

    3. In the Arguments field, type the following:


      queue
      
    4. Click OK.

    5. Right-click the project and choose Run.

      The output of the program looks like this:


      Destination type is queue
      Reading message: This is message 1
      Reading message: This is message 2
      Reading message: This is message 3
  3. Now try running the programs in the opposite order. Right-click the synchconsumer project and choose Run.

    The Output pane displays the destination type and then appears to hang, waiting for messages.

  4. Right-click the producer project and choose Run.

    The Output pane shows the output of both programs, in two different tabs.

  5. Now run the Producer example using a topic instead of a queue.

    1. Right-click the producer project and choose Properties.

    2. Select Run from the Categories tree.

    3. In the Arguments field, type the following:


      topic 3
      
    4. Click OK.

    5. 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
  6. Now run the SynchConsumer example using the topic.

    1. Right-click the synchconsumer project and choose Properties.

    2. Select Run from the Categories tree.

    3. In the Arguments field, type the following:


      topic
      
    4. Click OK.

    5. Right-click the project and choose Run.

      The result, however, is different. Because you are using a topic, messages that were sent before you started the consumer cannot be received. (See Publish/Subscribe Messaging Domain, for details.) Instead of receiving the messages, the program appears to hang.

  7. Run the Producer example again. Right-click the producer project and choose Run.

    Now the SynchConsumer example receives the messages:


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

You can also run the sample programs using the appclient command. Each of the programs takes one or more command-line arguments: a destination type and, for Producer, a number of messages.

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

  1. In a terminal window, go to the producer/dist directory:


    cd ../producer/dist
    
  2. Run the Producer program, sending three messages to the queue:


    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

    The messages are now in the queue, waiting to be received.

  3. In the same window, go to the synchconsumer/dist directory:


    cd ../../synchconsumer/dist
    
  4. Run the SynchConsumer program, specifying the queue:


    appclient -client synchconsumer.jar queue
    

    The output of the program looks like this:


    Destination type is queue
    Reading message: This is message 1
    Reading message: This is message 2
    Reading message: This is message 3
  5. Now try running the programs in the opposite order. Run the SynchConsumer program. It displays the destination type and then appears to hang, waiting for messages.


    appclient -client synchconsumer.jar queue
    
  6. In a different terminal window, run the Producer program.


    cd tut-install/javaeetutorial5/examples/jms/simple/producer/dist
    appclient -client producer.jar queue 3
    

    When the messages have been sent, the SynchConsumer program receives them and exits.

  7. Now run the Producer program using a topic instead of a queue:


    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
  8. Now run the SynchConsumer program using the topic:


    appclient -client synchconsumer.jar topic
    

    The result, however, is different. Because you are using a topic, messages that were sent before you started the consumer cannot be received. (See Publish/Subscribe Messaging Domain, for details.) Instead of receiving the messages, the program appears to hang.

  9. Run the Producer program again. Now the SynchConsumer program receives the messages:


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

Because the examples use the common interfaces, you can run them using either a queue or a topic.