Refactor JMS examples

This commit is contained in:
Grzegorz Piwowarek 2016-10-09 21:21:09 +02:00
parent b279f27f35
commit dd360db060
6 changed files with 81 additions and 78 deletions

View File

@ -26,6 +26,12 @@
<artifactId>activemq-all</artifactId>
<version>${activemq.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -36,8 +42,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
@ -52,6 +58,16 @@
</plugin>
</plugins>
</pluginManagement>
<finalName>spring-jms</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<finalName>spring-jms</finalName>
</build>
</project>

View File

@ -1,16 +1,11 @@
package com.baeldung.spring.jms;
import org.springframework.jms.core.JmsTemplate;
import javax.jms.Queue;
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;
public class SampleJmsMessageSender {
private JmsTemplate jmsTemplate;
@ -25,16 +20,12 @@ public class SampleJmsMessageSender {
}
public void simpleSend() {
this.jmsTemplate.send(this.queue, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("hello queue world");
}
});
jmsTemplate.send(queue, s -> s.createTextMessage("hello queue world"));
}
public void sendMessage(final Employee employee) {
System.out.println("Jms Message Sender : " + employee);
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> map = new HashMap<>();
map.put("name", employee.getName());
map.put("age", employee.getAge());
this.jmsTemplate.convertAndSend(map);

View File

@ -1,13 +1,12 @@
package com.baeldung.spring.jms;
import java.util.Map;
import org.springframework.jms.core.JmsTemplate;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.jms.core.JmsTemplate;
import java.util.Map;
public class SampleListener implements MessageListener {
@ -31,7 +30,6 @@ public class SampleListener implements MessageListener {
public Employee receiveMessage() throws JMSException {
Map map = (Map) getJmsTemplate().receiveAndConvert();
Employee employee = new Employee((String) map.get("name"), (Integer) map.get("age"));
return employee;
return new Employee((String) map.get("name"), (Integer) map.get("age"));
}
}

View File

@ -1,13 +1,13 @@
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 {
@ -20,8 +20,7 @@ public class SampleMessageConverter implements MessageConverter {
public Object fromMessage(Message message) throws JMSException, MessageConversionException {
MapMessage mapMessage = (MapMessage) message;
Employee employee = new Employee(mapMessage.getString("name"), mapMessage.getInt("age"));
return employee;
return new Employee(mapMessage.getString("name"), mapMessage.getInt("age"));
}
}

View File

@ -1,25 +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();
}
}
package com.baeldung.spring.jms;
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(
"classpath:EmbeddedActiveMQ.xml", "classpath:applicationContext.xml");
messageProducer = (SampleJmsMessageSender) applicationContext.getBean("SampleJmsMessageSender");
}
@Test
public void testSimpleSend() {
messageProducer.simpleSend();
}
}

View File

@ -1,26 +1,25 @@
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));
}
}
package com.baeldung.spring.jms;
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(
"classpath:EmbeddedActiveMQ.xml", "classpath:applicationContext.xml");
messageProducer = (SampleJmsMessageSender) applicationContext.getBean("SampleJmsMessageSender");
}
@Test
public void testSendMessage() {
messageProducer.sendMessage(new Employee("JavaDeveloper2", 22));
}
}