Merge pull request #722 from kirangonella/master
BAEL-319 A guide to Spring-JMS
This commit is contained in:
commit
1296f2e40f
|
@ -0,0 +1,57 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-jms</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
<name>spring-jms</name>
|
||||
<description>Introduction to Spring JMS</description>
|
||||
|
||||
<properties>
|
||||
<springframework.version>4.3.2.RELEASE</springframework.version>
|
||||
<activemq.version>5.12.0</activemq.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring JMS -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jms</artifactId>
|
||||
<version>${springframework.version}</version>
|
||||
</dependency>
|
||||
<!-- ActiveMQ -->
|
||||
<dependency>
|
||||
<groupId>org.apache.activemq</groupId>
|
||||
<artifactId>activemq-all</artifactId>
|
||||
<version>${activemq.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.2</version>
|
||||
<configuration>
|
||||
<source>1.7</source>
|
||||
<target>1.7</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
<configuration>
|
||||
<warSourceDirectory>src/main/webapp</warSourceDirectory>
|
||||
<warName>spring-jms</warName>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<finalName>spring-jms</finalName>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.spring.jms;
|
||||
|
||||
public class Employee {
|
||||
private String name;
|
||||
private Integer age;
|
||||
|
||||
public Employee(String name, Integer age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "Person: name(" + name + "), age(" + age + ")";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.spring.jms;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.apache.activemq.broker.BrokerFactory;
|
||||
import org.apache.activemq.broker.BrokerService;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class SampleJMSExample {
|
||||
public static void main(String[] args) throws URISyntaxException, Exception {
|
||||
BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:61616)"));
|
||||
broker.start();
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.spring.jms;
|
||||
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.Queue;
|
||||
import javax.jms.Session;
|
||||
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
import org.springframework.jms.core.MessageCreator;
|
||||
|
||||
public class SampleJmsMessageSender {
|
||||
|
||||
private JmsTemplate jmsTemplate;
|
||||
private Queue queue;
|
||||
|
||||
public void setConnectionFactory(ConnectionFactory cf) {
|
||||
this.jmsTemplate = new JmsTemplate(cf);
|
||||
}
|
||||
|
||||
public void setQueue(Queue queue) {
|
||||
this.queue = queue;
|
||||
}
|
||||
|
||||
public void simpleSend() {
|
||||
this.jmsTemplate.send(this.queue, new MessageCreator() {
|
||||
public Message createMessage(Session session) throws JMSException {
|
||||
return session.createTextMessage("hello queue world");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.spring.jms;
|
||||
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.MessageListener;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
public class SampleListener implements MessageListener {
|
||||
|
||||
private String textMessage;
|
||||
|
||||
public void onMessage(Message message) {
|
||||
if (message instanceof TextMessage) {
|
||||
try {
|
||||
textMessage = ((TextMessage) message).getText();
|
||||
} catch (JMSException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Message Error");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.spring.jms;
|
||||
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.MapMessage;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.Session;
|
||||
|
||||
import org.springframework.jms.support.converter.MessageConversionException;
|
||||
import org.springframework.jms.support.converter.MessageConverter;
|
||||
|
||||
public class SampleMessageConverter implements MessageConverter {
|
||||
|
||||
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
|
||||
Employee person = (Employee) object;
|
||||
MapMessage message = session.createMapMessage();
|
||||
message.setString("name", person.getName());
|
||||
message.setInt("age", person.getAge());
|
||||
return message;
|
||||
}
|
||||
|
||||
public Object fromMessage(Message message) throws JMSException, MessageConversionException {
|
||||
MapMessage mapMessage = (MapMessage) message;
|
||||
Employee person = new Employee(mapMessage.getString("name"), mapMessage.getInt("age"));
|
||||
return person;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="messageDestination" class="org.apache.activemq.command.ActiveMQQueue">
|
||||
<constructor-arg value="messageQueue1" />
|
||||
</bean>
|
||||
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
|
||||
<property name="connectionFactory" ref="connectionFactory" />
|
||||
<property name="receiveTimeout" value="10000" />
|
||||
</bean>
|
||||
|
||||
<bean id="SampleJmsMessageSender" class="com.baeldung.spring.jms.SampleJmsMessageSender">
|
||||
<property name="destination" ref="messageDestination" />
|
||||
<property name="jmsTemplate" ref="jmsTemplate" />
|
||||
</bean>
|
||||
|
||||
<!-- this is the Message-Driven POJO (MDP) -->
|
||||
<bean id="messageListener" class="com.baeldung.spring.jms.SampleListener" />
|
||||
|
||||
<!-- and this is the message listener container -->
|
||||
<bean id="jmsContainer"
|
||||
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
|
||||
<property name="connectionFactory" ref="connectionFactory" />
|
||||
<property name="destination" ref="messageDestination" />
|
||||
<property name="messageListener" ref="messageListener" />
|
||||
<property name="jmsTemplate" ref="jmsTemplate" />
|
||||
</bean>
|
||||
</beans>
|
Loading…
Reference in New Issue