JMS Sender application with ActiveMQ and Maven
We have already seen how to create a JMS Receiver application with ActiveMQ and Maven. Let’s check out how we can similarly create the JMS Sender application.
web.xml remains the same as we had used for creating the receiver application:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/jmsContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
jmsContext.xml would change a bit to something like:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"> <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> </bean> <!-- <jms:listener-container container-type="default" connection-factory="connectionFactory" acknowledge="auto"> <jms:listener destination="testQueue" ref="simpleMessageListener" method="onMessage" /> </jms:listener-container> --> <!-- To send JMS message --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="defaultDestinationName" value="testQueue"/> </bean> <bean id="simpleMessageSender" class="com.jcombat.sender.SimpleMessageSender"> <property name="jmsTemplate" ref="jmsTemplate" /> </bean> </beans>
Note that we have added two new beans for jmsTemplate and messageSender class, which will be using the jmsTemplate instance to send message to the destination queue.
Now let’s create a message sender class SimpleMessageSender.java, which will actually be sending the message:
package com.jcombat.sender; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; public class SimpleMessageSender { private JmsTemplate jmsTemplate; public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public void sendMessage(final String message){ jmsTemplate.send(new MessageCreator(){ @Override public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); } }
Almost done. Now let’s create the client class TestClient.java, which will get the simpleMessageSender bean from jmsContext and send some message string by calling it’s sendMessage method, thus dropping the message to the configured queue testQueue (as seen highlighted in the jmsContext file content shown above).
package com.jcombat.client; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.jcombat.sender.SimpleMessageSender; public class TestClient { @SuppressWarnings("resource") public static void main(String[] args) throws Exception { ApplicationContext jmsContext = null; jmsContext = new FileSystemXmlApplicationContext( "path/to/jmsContext.xml"); SimpleMessageSender messageSender = (SimpleMessageSender) jmsContext .getBean("simpleMessageSender"); // Create a session within the connection. messageSender.sendMessage("hello"); } }
Now run the TestClient class as Java Application.
Verify message on the destination queue
To verify that the message that we have sent through our client class has successfully arrived at the destination queue i.e. testQueue, login to the Hawtio console and from the queues listed on the left, click on the testQueue link, which shows up the testQueue details on the main content area as can be seen below:
Now click on the Message that could be seen listed under the ‘Browse’ tab. We could see the same message that we had sent pops out as:
Reference: | JMS Sender application with ActiveMQ and Maven from our JCG partner Abhimanyu Prasad at the jCombat blog. |
This is a ready for use JMS message sender command-line tool, inspired by this article. Source Code and executable JAR: https://github.com/zappee/jms-message-sender