diff --git a/spring-jms/src/main/java/com/baeldung/spring/jms/Employee.java b/spring-jms/src/main/java/com/baeldung/spring/jms/Employee.java index 4da02907a9..ff4f262b38 100644 --- a/spring-jms/src/main/java/com/baeldung/spring/jms/Employee.java +++ b/spring-jms/src/main/java/com/baeldung/spring/jms/Employee.java @@ -18,6 +18,6 @@ public class Employee { } public String toString() { - return "Person: name(" + name + "), age(" + age + ")"; + return "Employee: name(" + name + "), age(" + age + ")"; } } diff --git a/spring-jms/src/main/java/com/baeldung/spring/jms/SampleJMSExample.java b/spring-jms/src/main/java/com/baeldung/spring/jms/SampleJMSExample.java deleted file mode 100644 index a243bc1390..0000000000 --- a/spring-jms/src/main/java/com/baeldung/spring/jms/SampleJMSExample.java +++ /dev/null @@ -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"); - - } -} diff --git a/spring-jms/src/main/java/com/baeldung/spring/jms/SampleJmsMessageSender.java b/spring-jms/src/main/java/com/baeldung/spring/jms/SampleJmsMessageSender.java index 865a1d4747..8751b42ff8 100644 --- a/spring-jms/src/main/java/com/baeldung/spring/jms/SampleJmsMessageSender.java +++ b/spring-jms/src/main/java/com/baeldung/spring/jms/SampleJmsMessageSender.java @@ -1,17 +1,23 @@ 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.MessageCreator; -import javax.jms.*; - public class SampleJmsMessageSender { private JmsTemplate jmsTemplate; private Queue queue; - public void createJmsTemplate(ConnectionFactory cf) { - this.jmsTemplate = new JmsTemplate(cf); + public void setJmsTemplate(JmsTemplate jmsTemplate) { + this.jmsTemplate = jmsTemplate; } public void setQueue(Queue queue) { @@ -25,4 +31,12 @@ public class SampleJmsMessageSender { } }); } -} \ No newline at end of file + + public void sendMessage(final Employee employee) { + System.out.println("Jms Message Sender : " + employee); + Map map = new HashMap(); + map.put("name", employee.getName()); + map.put("age", employee.getAge()); + this.jmsTemplate.convertAndSend(map); + } +} diff --git a/spring-jms/src/main/java/com/baeldung/spring/jms/SampleListener.java b/spring-jms/src/main/java/com/baeldung/spring/jms/SampleListener.java index 0320870d88..3ad4d70829 100644 --- a/spring-jms/src/main/java/com/baeldung/spring/jms/SampleListener.java +++ b/spring-jms/src/main/java/com/baeldung/spring/jms/SampleListener.java @@ -1,18 +1,26 @@ package com.baeldung.spring.jms; +import java.util.Map; + import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; +import org.springframework.jms.core.JmsTemplate; + public class SampleListener implements MessageListener { - private String textMessage; + public JmsTemplate getJmsTemplate() { + return getJmsTemplate(); + } public void onMessage(Message message) { if (message instanceof TextMessage) { try { - textMessage = ((TextMessage) message).getText(); + + String msg = ((TextMessage) message).getText(); + System.out.println("Message has been consumed : " + msg); } catch (JMSException ex) { throw new RuntimeException(ex); } @@ -20,4 +28,10 @@ public class SampleListener implements MessageListener { throw new IllegalArgumentException("Message Error"); } } -} \ No newline at end of file + + public Employee receiveMessage() throws JMSException { + Map map = (Map) getJmsTemplate().receiveAndConvert(); + Employee employee = new Employee((String) map.get("name"), (Integer) map.get("age")); + return employee; + } +} diff --git a/spring-jms/src/main/java/com/baeldung/spring/jms/SampleMessageConverter.java b/spring-jms/src/main/java/com/baeldung/spring/jms/SampleMessageConverter.java index 6cfc588f13..c61cd5bc89 100644 --- a/spring-jms/src/main/java/com/baeldung/spring/jms/SampleMessageConverter.java +++ b/spring-jms/src/main/java/com/baeldung/spring/jms/SampleMessageConverter.java @@ -1,26 +1,27 @@ 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.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; + Employee employee = (Employee) object; MapMessage message = session.createMapMessage(); - message.setString("name", person.getName()); - message.setInt("age", person.getAge()); + message.setString("name", employee.getName()); + message.setInt("age", employee.getAge()); return message; } public Object fromMessage(Message message) throws JMSException, MessageConversionException { 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; } } diff --git a/spring-jms/src/main/resources/EmbeddedActiveMQ.xml b/spring-jms/src/main/resources/EmbeddedActiveMQ.xml new file mode 100644 index 0000000000..5e956144fd --- /dev/null +++ b/spring-jms/src/main/resources/EmbeddedActiveMQ.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/spring-jms/src/main/resources/applicationContext.xml b/spring-jms/src/main/resources/applicationContext.xml index c15289763f..d94eb4c371 100644 --- a/spring-jms/src/main/resources/applicationContext.xml +++ b/spring-jms/src/main/resources/applicationContext.xml @@ -3,16 +3,27 @@ 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"> - - - - - - - - + + + + + + + + + + + + + + + + + + - + @@ -23,8 +34,7 @@ - + - - \ No newline at end of file + diff --git a/spring-jms/src/test/com/baeldung/spring/jms/DefaultTextMessageSenderTest.java b/spring-jms/src/test/com/baeldung/spring/jms/DefaultTextMessageSenderTest.java new file mode 100644 index 0000000000..2016122105 --- /dev/null +++ b/spring-jms/src/test/com/baeldung/spring/jms/DefaultTextMessageSenderTest.java @@ -0,0 +1,26 @@ +package com.baeldung.spring.jms; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.spring.jms.SampleJmsMessageSender; + +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(); + } + +} diff --git a/spring-jms/src/test/com/baeldung/spring/jms/MapMessageConvertAndSendTest.java b/spring-jms/src/test/com/baeldung/spring/jms/MapMessageConvertAndSendTest.java new file mode 100644 index 0000000000..19b9097601 --- /dev/null +++ b/spring-jms/src/test/com/baeldung/spring/jms/MapMessageConvertAndSendTest.java @@ -0,0 +1,27 @@ +package com.baeldung.spring.jms; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.spring.jms.Employee; +import com.baeldung.spring.jms.SampleJmsMessageSender; + +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)); + } + +}