Merge branch 'master' of git://github.com/kirangonella/tutorials into kirangonella-master

# Conflicts:
#	spring-jms/src/main/java/com/baeldung/spring/jms/Employee.java
#	spring-jms/src/main/java/com/baeldung/spring/jms/SampleJMSExample.java
#	spring-jms/src/main/java/com/baeldung/spring/jms/SampleJmsMessageSender.java
#	spring-jms/src/main/java/com/baeldung/spring/jms/SampleListener.java
#	spring-jms/src/main/java/com/baeldung/spring/jms/SampleMessageConverter.java
This commit is contained in:
Grzegorz Piwowarek 2016-10-09 20:36:03 +02:00
commit 7fd2511b93
9 changed files with 138 additions and 44 deletions

View File

@ -18,6 +18,6 @@ public class Employee {
}
public String toString() {
return "Person: name(" + name + "), age(" + age + ")";
return "Employee: name(" + name + "), age(" + age + ")";
}
}

View File

@ -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");
}
}

View File

@ -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 {
}
});
}
}
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);
}
}

View File

@ -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");
}
}
}
public Employee receiveMessage() throws JMSException {
Map map = (Map) getJmsTemplate().receiveAndConvert();
Employee employee = new Employee((String) map.get("name"), (Integer) map.get("age"));
return employee;
}
}

View File

@ -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;
}
}

View File

@ -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>

View File

@ -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">
<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="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg index="0" value="tcp://localhost:61616" />
</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">
<property name="destination" ref="messageDestination" />
<property name="queue" ref="destinationQueue" />
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
@ -23,8 +34,7 @@
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="messageDestination" />
<property name="destinationName" value="IN_QUEUE" />
<property name="messageListener" ref="messageListener" />
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
</beans>
</beans>

View File

@ -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();
}
}

View File

@ -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));
}
}