We will create a Spring application with JMS to send a text message using Liberty as a server.

Before you begin, complete the Creating a simple “HelloWorld” Spring servlet tutorial:

As we build our JMS application, we will also simultaneously edit our server’s server.xml for configurations. In this application, the core JMS APIs used are available in javax.jms.*.

Features needed in server.xml:

  <feature>servlet-3.1</feature>
  <feature>wasJmsClient-1.1</feature>
  <feature>wasJmsServer-1.0</feature>
  <feature>jmsMdb-3.1</feature>
  

Let’s create a SpringBean:

  1. Create and configure a queue connection factory:
    • Create a QueueConnectionFactory object:
     
        QueueConnectionFactory connectionFactory = (QueueConnectionFactory) new InitialContext().lookup("java:comp/env/jndi_JMS_BASE_QCF");
        
    • Modify your server.xml by creating a <jmsQueueConnectionFactory> element:
     
        <jmsQueueConnectionFactory connectionManagerRef="ConMgr" jndiName="jndi_JMS_BASE_QCF">
      		<properties.wasJms/>
        </jmsQueueConnectionFactory>
        <connectionManager id="ConMgr" maxPoolSize="5"/>
      

  2. Create a queue:
    • Create a Queue object:
     
       Queue queue = (Queue) new InitialContext().lookup("java:comp/env/jndi_INPUT_Q");
        
    • Configure a <jmsQueue> element in the server.xml so that the queue can be accessed with a JNDI lookup:
     
      <messagingEngine>
    	<queue id="QUEUE1">
    	</queue>
      </messagingEngine>
    
      <jmsQueue jndiName="jndi_INPUT_Q">
    	<properties.wasJms queueName="QUEUE1"/>
      </jmsQueue>
        

  3. We can then create a queue connection:
    • Create a QueueConnection object to start the connection and create a QueueSession for that queue which the QueueSender will use send a TextMessage through a SpringBean. Print the TextMessage object on the console using System.out to view the contents of it:
     
       TextMessage msg = sessionSender.createTextMessage("Welcome to Liberty");
       queueSender.send(msg);
        

  4. Create a Servlet that performs a get request. We will use the Spring framework to configure the bean and write the response received from that bean by using the method we created above:
     
       ApplicationContext context = new ClassPathXmlApplicationContext("SpringBean.xml");
       SpringJMSBean bean = (SpringJMSBean) context.getBean("SpringBean");
      

    SpringBean.xml will have the configuration that connects the SpringBean to the class where the bean is created as shown in the first two steps.

  5. Publish the app on the server and start the server. As the server starts, it will print the TextMessage on the console which will give us useful information such as JMSMessageID, JMSDestination, JMSRedelivered, JMSExpiration, JMSMessage class, and so on.

We now have a working Spring application on a Liberty server. The application uses JMS APIs to send a text message.

Join The Discussion

Your email address will not be published. Required fields are marked *