Merge branch 'kirangonella-master'
This commit is contained in:
commit
b279f27f35
|
@ -18,6 +18,6 @@ public class Employee {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Person: name(" + name + "), age(" + age + ")";
|
return "Employee: name(" + name + "), age(" + age + ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
package com.baeldung.spring.jms;
|
|
||||||
|
|
||||||
import org.apache.activemq.broker.BrokerFactory;
|
|
||||||
import org.apache.activemq.broker.BrokerService;
|
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
|
|
||||||
public class SampleJMSExample {
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:61616)"));
|
|
||||||
broker.start();
|
|
||||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,17 +1,23 @@
|
||||||
package com.baeldung.spring.jms;
|
package com.baeldung.spring.jms;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
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.JmsTemplate;
|
||||||
import org.springframework.jms.core.MessageCreator;
|
import org.springframework.jms.core.MessageCreator;
|
||||||
|
|
||||||
import javax.jms.*;
|
|
||||||
|
|
||||||
public class SampleJmsMessageSender {
|
public class SampleJmsMessageSender {
|
||||||
|
|
||||||
private JmsTemplate jmsTemplate;
|
private JmsTemplate jmsTemplate;
|
||||||
private Queue queue;
|
private Queue queue;
|
||||||
|
|
||||||
public void createJmsTemplate(ConnectionFactory cf) {
|
public void setJmsTemplate(JmsTemplate jmsTemplate) {
|
||||||
this.jmsTemplate = new JmsTemplate(cf);
|
this.jmsTemplate = jmsTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setQueue(Queue queue) {
|
public void setQueue(Queue queue) {
|
||||||
|
@ -25,4 +31,12 @@ public class SampleJmsMessageSender {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sendMessage(final Employee employee) {
|
||||||
|
System.out.println("Jms Message Sender : " + employee);
|
||||||
|
Map<String, Object> map = new HashMap<String, Object>();
|
||||||
|
map.put("name", employee.getName());
|
||||||
|
map.put("age", employee.getAge());
|
||||||
|
this.jmsTemplate.convertAndSend(map);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,18 +1,26 @@
|
||||||
package com.baeldung.spring.jms;
|
package com.baeldung.spring.jms;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.jms.JMSException;
|
import javax.jms.JMSException;
|
||||||
import javax.jms.Message;
|
import javax.jms.Message;
|
||||||
import javax.jms.MessageListener;
|
import javax.jms.MessageListener;
|
||||||
import javax.jms.TextMessage;
|
import javax.jms.TextMessage;
|
||||||
|
|
||||||
|
import org.springframework.jms.core.JmsTemplate;
|
||||||
|
|
||||||
public class SampleListener implements MessageListener {
|
public class SampleListener implements MessageListener {
|
||||||
|
|
||||||
private String textMessage;
|
public JmsTemplate getJmsTemplate() {
|
||||||
|
return getJmsTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
public void onMessage(Message message) {
|
public void onMessage(Message message) {
|
||||||
if (message instanceof TextMessage) {
|
if (message instanceof TextMessage) {
|
||||||
try {
|
try {
|
||||||
textMessage = ((TextMessage) message).getText();
|
|
||||||
|
String msg = ((TextMessage) message).getText();
|
||||||
|
System.out.println("Message has been consumed : " + msg);
|
||||||
} catch (JMSException ex) {
|
} catch (JMSException ex) {
|
||||||
throw new RuntimeException(ex);
|
throw new RuntimeException(ex);
|
||||||
}
|
}
|
||||||
|
@ -20,4 +28,10 @@ public class SampleListener implements MessageListener {
|
||||||
throw new IllegalArgumentException("Message Error");
|
throw new IllegalArgumentException("Message Error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Employee receiveMessage() throws JMSException {
|
||||||
|
Map map = (Map) getJmsTemplate().receiveAndConvert();
|
||||||
|
Employee employee = new Employee((String) map.get("name"), (Integer) map.get("age"));
|
||||||
|
return employee;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,26 +1,27 @@
|
||||||
package com.baeldung.spring.jms;
|
package com.baeldung.spring.jms;
|
||||||
|
|
||||||
import org.springframework.jms.support.converter.MessageConversionException;
|
|
||||||
import org.springframework.jms.support.converter.MessageConverter;
|
|
||||||
|
|
||||||
import javax.jms.JMSException;
|
import javax.jms.JMSException;
|
||||||
import javax.jms.MapMessage;
|
import javax.jms.MapMessage;
|
||||||
import javax.jms.Message;
|
import javax.jms.Message;
|
||||||
import javax.jms.Session;
|
import javax.jms.Session;
|
||||||
|
|
||||||
|
import org.springframework.jms.support.converter.MessageConversionException;
|
||||||
|
import org.springframework.jms.support.converter.MessageConverter;
|
||||||
|
|
||||||
public class SampleMessageConverter implements MessageConverter {
|
public class SampleMessageConverter implements MessageConverter {
|
||||||
|
|
||||||
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
|
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
|
||||||
Employee person = (Employee) object;
|
Employee employee = (Employee) object;
|
||||||
MapMessage message = session.createMapMessage();
|
MapMessage message = session.createMapMessage();
|
||||||
message.setString("name", person.getName());
|
message.setString("name", employee.getName());
|
||||||
message.setInt("age", person.getAge());
|
message.setInt("age", employee.getAge());
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object fromMessage(Message message) throws JMSException, MessageConversionException {
|
public Object fromMessage(Message message) throws JMSException, MessageConversionException {
|
||||||
MapMessage mapMessage = (MapMessage) message;
|
MapMessage mapMessage = (MapMessage) message;
|
||||||
return new Employee(mapMessage.getString("name"), mapMessage.getInt("age"));
|
Employee employee = new Employee(mapMessage.getString("name"), mapMessage.getInt("age"));
|
||||||
|
return employee;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
<?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:p="http://www.springframework.org/schema/p"
|
||||||
|
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||||
|
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
|
||||||
|
xmlns:amq="http://activemq.apache.org/schema/core"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://activemq.apache.org/schema/core
|
||||||
|
http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||||
|
|
||||||
|
<!-- Embedded ActiveMQ Broker -->
|
||||||
|
<amq:broker id="broker" useJmx="false" persistent="false">
|
||||||
|
<amq:transportConnectors>
|
||||||
|
<amq:transportConnector uri="tcp://localhost:61616" />
|
||||||
|
</amq:transportConnectors>
|
||||||
|
</amq:broker>
|
||||||
|
|
||||||
|
</beans>
|
|
@ -3,16 +3,27 @@
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
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">
|
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
|
||||||
<constructor-arg value="messageQueue1" />
|
<constructor-arg index="0" value="tcp://localhost:61616" />
|
||||||
</bean>
|
</bean>
|
||||||
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
|
|
||||||
<property name="connectionFactory" ref="connectionFactory" />
|
|
||||||
<property name="receiveTimeout" value="10000" />
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
|
<!-- ConnectionFactory Definition -->
|
||||||
|
<bean id="connectionFactory"
|
||||||
|
class="org.springframework.jms.connection.CachingConnectionFactory">
|
||||||
|
<constructor-arg ref="amqConnectionFactory" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
|
||||||
|
<constructor-arg index="0" value="IN_QUEUE" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<!-- JmsTemplate Definition -->
|
||||||
|
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
|
||||||
|
<property name="connectionFactory" ref="connectionFactory" />
|
||||||
|
<property name="defaultDestination" ref="destinationQueue" />
|
||||||
|
</bean>
|
||||||
<bean id="SampleJmsMessageSender" class="com.baeldung.spring.jms.SampleJmsMessageSender">
|
<bean id="SampleJmsMessageSender" class="com.baeldung.spring.jms.SampleJmsMessageSender">
|
||||||
<property name="destination" ref="messageDestination" />
|
<property name="queue" ref="destinationQueue" />
|
||||||
<property name="jmsTemplate" ref="jmsTemplate" />
|
<property name="jmsTemplate" ref="jmsTemplate" />
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
|
@ -23,8 +34,7 @@
|
||||||
<bean id="jmsContainer"
|
<bean id="jmsContainer"
|
||||||
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
|
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
|
||||||
<property name="connectionFactory" ref="connectionFactory" />
|
<property name="connectionFactory" ref="connectionFactory" />
|
||||||
<property name="destination" ref="messageDestination" />
|
<property name="destinationName" value="IN_QUEUE" />
|
||||||
<property name="messageListener" ref="messageListener" />
|
<property name="messageListener" ref="messageListener" />
|
||||||
<property name="jmsTemplate" ref="jmsTemplate" />
|
|
||||||
</bean>
|
</bean>
|
||||||
</beans>
|
</beans>
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.baeldung.spring.jms;
|
||||||
|
|
||||||
|
import com.baeldung.spring.jms.SampleJmsMessageSender;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
public class DefaultTextMessageSenderTest {
|
||||||
|
|
||||||
|
private static SampleJmsMessageSender messageProducer;
|
||||||
|
|
||||||
|
@SuppressWarnings("resource")
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUp() {
|
||||||
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "classpath:EmbeddedActiveMQ.xml", "classpath:applicationContext.xml" });
|
||||||
|
messageProducer = (SampleJmsMessageSender) applicationContext.getBean("SampleJmsMessageSender");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleSend() {
|
||||||
|
messageProducer.simpleSend();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.baeldung.spring.jms;
|
||||||
|
|
||||||
|
import com.baeldung.spring.jms.Employee;
|
||||||
|
import com.baeldung.spring.jms.SampleJmsMessageSender;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
public class MapMessageConvertAndSendTest {
|
||||||
|
|
||||||
|
private static SampleJmsMessageSender messageProducer;
|
||||||
|
|
||||||
|
@SuppressWarnings("resource")
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUp() {
|
||||||
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "classpath:EmbeddedActiveMQ.xml", "classpath:applicationContext.xml" });
|
||||||
|
messageProducer = (SampleJmsMessageSender) applicationContext.getBean("SampleJmsMessageSender");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSendMessage() {
|
||||||
|
messageProducer.sendMessage(new Employee("JavaDeveloper2", 22));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue