Asynchronous Messaging Example in Java

A Simple Example of Asynchronous Message Consumption

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

Writing the Clients for the Asynchronous Receive Example

The sending client is producer/src/java/Producer.java, the same client used in the example in A Simple Example of Synchronous Message Receives.

An asynchronous consumer normally runs indefinitely. This one runs until the user types the letter q or Q to stop the client.

    The receiving client, asynchconsumer/src/java/AsynchConsumer.java, performs the following steps:

  1. Injects resources for a connection factory, queue, and topic.

  2. Assigns either the queue or topic to a destination object, based on the specified destination type.

  3. Creates a Connection and a Session.

  4. Creates a MessageConsumer.

  5. Creates an instance of the TextListener class and registers it as the message listener for the MessageConsumer:

    listener = new TextListener();consumer.setMessageListener(listener);
  6. Starts the connection, causing message delivery to begin.

  7. Listens for the messages published to the destination, stopping when the user types the character q or Q:

    System.out.println("To end program, type Q or q, " + "then <return>"); inputStreamReader = new InputStreamReader(System.in); while (!((answer == 'q') || (answer == 'Q'))) {      try {          answer = (char) inputStreamReader.read();      } catch (IOException e) {          System.out.println("I/O exception: " + e.toString());      } }
  8. Closes the connection, which automatically closes the session and MessageConsumer.

    The message listener, asynchconsumer/src/java/TextListener.java, follows these steps:

  1. When a message arrives, the onMessage method is called automatically.

  2. The onMessage method converts the incoming message to a TextMessage and displays its content. If the message is not a text message, it reports this fact:

    public void onMessage(Message message) {      TextMessage msg = null;      try {          if (message instanceof TextMessage) {              msg = (TextMessage) message;               System.out.println("Reading message: " + msg.getText());          } else {               System.out.println("Message is not a " + "TextMessage");          }      } catch (JMSException e) {          System.out.println("JMSException in onMessage(): " + e.toString());      } catch (Throwable t) {          System.out.println("Exception in onMessage():" + t.getMessage());      } }

You will use the connection factory and destinations you created in To Create JMS Administered Objects for the Synchronous Receive Example.

ProcedureTo Build and Package the AsynchConsumer Client Using NetBeans IDE

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

  2. In the Open Project dialog, navigate to:


                                      tut-install/examples/jms/simple/              
  3. Select the asynchconsumer 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.

ProcedureTo Deploy and Run the Clients for the Asynchronous Receive Example Using NetBeans IDE

  1. Run the AsynchConsumer example:

    1. Right-click the asynchconsumer project and select Properties.

    2. Select Run from the Categories tree.

    3. In the Arguments field, type the following:

    4. Click OK.

    5. Right-click the project and select Run.

      The client displays the following lines and appears to hang:


      Destination type is topic To end program, type Q or q, then <return>
  2. Now run the Producer example:

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

    2. Select Run from the Categories tree.

    3. In the Arguments field, type the following:

    4. Click OK.

    5. Right-click the project and select Run.

      The output of the client looks like this:


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

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


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

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

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

  4. Now run the Producer client using a queue.

    In this case, as with the synchronous example, you can run the Producer client first, because there is no timing dependency between the sender and receiver.

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

    2. Select Run from the Categories tree.

    3. In the Arguments field, type the following:

    4. Click OK.

    5. Right-click the project and select Run.

      The output of the client looks like this:


      Destination type is queue Sending message: This is message 1 from producer Sending message: This is message 2 from producer Sending message: This is message 3 from producer
  5. Run the AsynchConsumer client.

    1. Right-click the asynchconsumer project and select Properties.

    2. Select Run from the Categories tree.

    3. In the Arguments field, type the following:

    4. Click OK.

    5. Right-click the project and select Run.

      The output of the client looks like this:


      Destination type is queue To end program, type Q or q, then <return> Reading message: This is message 1 from producer Reading message: This is message 2 from producer Reading message: This is message 3 from producer Message is not a TextMessage
  6. Type Q or q in the Output window and press Return to stop the client.

ProcedureTo Build and Package the AsynchConsumer Client Using Ant

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

  2. Type the following command:

    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.

ProcedureTo Deploy and Run the Clients for the Asynchronous Receive Example Using Ant and the appclient Command

  1. Deploy the client JAR file to the GlassFish Server, then retrieve the client stubs:

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

  2. Run the AsynchConsumer client, specifying the topic destination type.


                                      appclient -client client-jar/asynchconsumerClient.jar topic                              

    The client displays the following lines (along with some application client container output) and appears to hang:


    Destination type is topic To end program, type Q or q, then <return>
  3. In the terminal window where you ran the Producer client previously, run the client again, sending three messages.


                                      appclient -client client-jar/producerClient.jar topic 3                              

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


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

    In the other window, the AsynchConsumer client displays the following (along with some application client container output):


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

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

  4. Type Q or q and press Return to stop the client.

  5. Now run the clients using a queue.

    In this case, as with the synchronous example, you can run the Producer client first, because there is no timing dependency between the sender and receiver:


                                      appclient -client client-jar/producerClient.jar queue 3                              

    The output of the client looks like this:


    Destination type is queue Sending message: This is message 1 from producer Sending message: This is message 2 from producer Sending message: This is message 3 from producer
  6. Run the AsynchConsumer client:


                                      appclient -client client-jar/asynchconsumerClient.jar queue                              

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


    Destination type is queue To end program, type Q or q, then <return> Reading message: This is message 1 from producer Reading message: This is message 2 from producer Reading message: This is message 3 from producer Message is not a TextMessage
  7. Type Q or q to stop the client.

saltauthermse2001.blogspot.com

Source: https://docs.oracle.com/cd/E19798-01/821-1841/bncfh/index.html

0 Response to "Asynchronous Messaging Example in Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel