Skip to content

Lab M05P01

Distributed java applications, modules or systems often need to be integrated in loosely coupled manner. Java platform provides Java Message Service, which allows clients to send and receive asynchronous messages. In this lab, you learn how to send and receive such messages using JMS.

  1. In order to use JMS, you need the message broker. It is JMS API implementation and often it is part of Message Oriented Middleware (MOM) environment. This lab uses the Apache ActiveMQ Artemis project, which supports JMS messaging. (https://activemq.apache.org/artemis/).
    Just start Artemis instance:artemis/bin/artemis.cmd run You do not need any application server to complete this lab!

  2. There is JMS Queue named exampleQueue configured, so you will implement Point to Point messaging using message Producer and message Consumer. Message brokers provide native client API, but you will use just pure JMS API.

  3. Now, open the ite.jms.JMSQueueProducer source file. As you already know, you should get reference to JMS destination (Queue in this case) and a Connection factory in order to create connections to destination. Both resources provided by a broker are available via JNDI, so you can use JNDI lookup.

      Queue queue = (Queue)initialContext.lookup("queue/exampleQueue");
      ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("ConnectionFactory");
    

    Note: The jndi.properties from resource directory provides configuration for JNDI context.

  4. Creating JMS connection and sending a message is easy task, using JMS 2.0 simplified API. You can utilize the auto-closeable feature of the JMS context. The Artemis implements basic security mechanism, so you need provide user and password when you create the JMS context.

     try(
       JMSContext jmsContext = cf.createContext("user","password");
     )
    
  5. Now use JMS context to create message Producer and send a text message to the exampleQueue destination.

      JMSProducer jmsProducer = jmsContext.createProducer();
      jmsProducer.send(queue, "Hallo from JMS Producer!");
    
  6. Now it is time to create message Consumer, which receives messages from the exampleQueue destination. Open the ite.jms.JMSQueueConsumer source file. Creation of ConnectionFactory and exampleQueue resources is the same as for Producer. But now, use the JMS context to create Consumer and receive message from exampleQueue:

      JMSConsumer jmsConsumer = jmsContext.createConsumer(queue);
      logger.info("Waiting for a message...");
      Message message = jmsConsumer.receive();
      logger.info("Message Body: {}", message.getBody(String.class));
    

    Note: The receive() method blocks until some message is available in JMS Queue. You can use also variant with timeout value.

    Now start the JMSQueueConsumer and then JMSQueueProducer and check if message is received. Of course, Artemis should be running.

  7. Sometimes you need to send and receive messages in not blocking manner asynchronously. You can use JMS asynchronous feature of the JMS Producer and JMS Message listener. Open the ite.jms.JMSQueueAsynchronousProducer and use asynch feature of the Producer:

      jmsProducer.setAsync(new DefaultCompletionListener(latch));
    

    You should set the CompletionListener in order to receive message processing result from the message broker.

  8. Now implement the MessageListener interface and instruct the Consumer to use it. MessageListener has one onMessage() method, which is called by the JMS, when message is received by destination. Open the DefaultMessageListener class and complete the implementation of the onMessage() method. Just log the received message:

      logger.info("Received Message No {} with Body: {} ", messageCounter, message.getBody(String.class));      
    

    Open the JMSQueueAsynchronousConsumer and configure Consumer to use MessageListener. You should start message receiving using JMS contex's start() method:

      jmsConsumer.setMessageListener(new DefaultMessageListener(latch));
      jmsContext.start();
    
  9. Start the JMSQueueAsynchronousProducer and JMSQueueAsynchronousConsumer and check if they work as expected.

    Task: Implement Publish/Subscribe style of messaging using JMS Topic destination. Topic should be configured in Apache Artemis'es broker.xml configuration file.

    Tip: Open the Artemis console, and monitor the JMS destinations.

Artemis setup

  1. Extract artemis distribution into c:\apache-artemis
  2. Create new broker instance.

    • Goto c:\apache-artemis\bin
    • Execute .\artemis create c:\apache-artemis\broker\spring
    • Provide user as Username
    • Provide user01* as Username
    • Allow anonymous access