Spring 4 + JMS Messaging + Gradle Example with JmsTemplate
August 02, 2014
On this page we are providing JMS example using spring with JmsTemplate. Spring provides JMS API that send and receives the messages. Spring configures MessageListenerAdapter and SimpleMessageListenerContainer to listen the messages. JmsTemplate API sends the created messages. We will learn the use of these API by a simple example. Gradle will be used to build the project.
Software Used
In our example we are using below softwares to run the program.1. JDK 6
2. Eclipse Juno
3. Gradle 2.0
Spring Boot and JMS JAR Dependency in Gradle
Find the gradle script for jar dependency of spring 4 and JMS API.build.gradle
apply plugin: 'java' apply plugin: 'eclipse' archivesBaseName = 'Concretepage' version = '1.0-SNAPSHOT' repositories { mavenCentral() } jar { manifest { attributes 'Main-Class': 'com.concretepage.Spring4JMSTest' } } dependencies { compile 'org.springframework.boot:spring-boot-starter:1.1.4.RELEASE' compile 'org.springframework:spring-jms:4.0.6.RELEASE' compile 'org.apache.activemq:activemq-broker:5.10.0' }
Create MessageCreator
Spring provides org.springframework.jms.core.MessageCreator interface which has a method createMessage() that needs to be implemented.MyMessageCreator.java
package com.concretepage; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import org.springframework.jms.core.MessageCreator; public class MyMessageCreator implements MessageCreator { @Override public Message createMessage(Session session) throws JMSException { return session.createTextMessage("Hello World!"); } }
Create Message Receiver
Create a class to receive the message.MsgReceiver.java
package com.concretepage.receiver; import java.io.File; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.util.FileSystemUtils; public class MsgReceiver { @Autowired ConfigurableApplicationContext context; public void receiveMessage(String message) { System.out.println("Message Received:" + message ); context.close(); FileSystemUtils.deleteRecursively(new File("activemq.txt")); } }
MessageListenerAdapter messageListenerAdp = new MessageListenerAdapter(receiver); messageListenerAdp.setDefaultListenerMethod("receiveMessage");
Configuration for MessageListenerAdapter and SimpleMessageListenerContainer
To achieve JMS Messaging, MessageListenerAdapter and SimpleMessageListenerContainer bean should be created. MessageListenerAdapter is provided message receiver method that is called to print the message.SimpleMessageListenerContainer is configured to define container settings like MessageListener, ConnectionFactory and DestinationName etc.
AppConfig.java
package com.concretepage.config; import javax.jms.ConnectionFactory; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jms.core.MessageCreator; import org.springframework.jms.listener.SimpleMessageListenerContainer; import org.springframework.jms.listener.adapter.MessageListenerAdapter; import com.concretepage.MyMessageCreator; import com.concretepage.receiver.MsgReceiver; @Configuration @EnableAutoConfiguration public class AppConfig { @Bean MsgReceiver msgReceiver() { return new MsgReceiver(); } @Bean MessageCreator myMessageCreator() { return new MyMessageCreator(); } @Bean MessageListenerAdapter messageListenerAdp(MsgReceiver receiver) { MessageListenerAdapter messageListenerAdp = new MessageListenerAdapter(receiver); messageListenerAdp.setDefaultListenerMethod("receiveMessage"); return messageListenerAdp; } @Bean SimpleMessageListenerContainer simpleMsgLisContainer(MessageListenerAdapter messageListenerAdp, ConnectionFactory connectionFactory) { SimpleMessageListenerContainer simpleMsgLisContainer = new SimpleMessageListenerContainer(); simpleMsgLisContainer.setMessageListener(messageListenerAdp); simpleMsgLisContainer.setConnectionFactory(connectionFactory); simpleMsgLisContainer.setDestinationName("admin@concretepage.com"); simpleMsgLisContainer.setConcurrency("2-5"); return simpleMsgLisContainer; } }
Send Message Using JmsTemplate
To test the JMS Messaging with Spring , find the demo class. Spring provides JmsTemplate to send the message.Spring4JMSTest.java
package com.concretepage; import java.io.File; import org.springframework.boot.SpringApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.util.FileSystemUtils; import com.concretepage.config.AppConfig; public class Spring4JMSTest { public static void main(String[] args) { FileSystemUtils.deleteRecursively(new File("activemq.txt")); ConfigurableApplicationContext context = SpringApplication.run(AppConfig.class, args); MessageCreator messageCreator = context.getBean(MessageCreator.class); JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); System.out.println("---Sending Message----"); jmsTemplate.send("admin@concretepage.com", messageCreator); } }
---Sending Message---- Message Received:Hello World!