Code refactored and updated (#746)
* Updated indentation and refactored code * Updated indentation and refactored code * Updated indentation and refactored code * Updated indentation and refactored code * Updated indentation * Updated indentation and refactored code * Updated indentation and refactored code
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
c0d4eee669
commit
65cd2dfb06
@@ -24,10 +24,6 @@ public class SampleJmsMessageSender {
|
||||
}
|
||||
|
||||
public void sendMessage(final Employee employee) {
|
||||
System.out.println("Jms Message Sender : " + employee);
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("name", employee.getName());
|
||||
map.put("age", employee.getAge());
|
||||
this.jmsTemplate.convertAndSend(map);
|
||||
this.jmsTemplate.convertAndSend(employee);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,21 @@ import org.springframework.jms.core.JmsTemplate;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.MessageListener;
|
||||
import javax.jms.Queue;
|
||||
import javax.jms.TextMessage;
|
||||
import java.util.Map;
|
||||
|
||||
public class SampleListener implements MessageListener {
|
||||
|
||||
public JmsTemplate getJmsTemplate() {
|
||||
return getJmsTemplate();
|
||||
private JmsTemplate jmsTemplate;
|
||||
private Queue queue;
|
||||
|
||||
public void setJmsTemplate(JmsTemplate jmsTemplate) {
|
||||
this.jmsTemplate = jmsTemplate;
|
||||
}
|
||||
|
||||
public void setQueue(Queue queue) {
|
||||
this.queue = queue;
|
||||
}
|
||||
|
||||
public void onMessage(Message message) {
|
||||
@@ -19,17 +27,14 @@ public class SampleListener implements MessageListener {
|
||||
try {
|
||||
|
||||
String msg = ((TextMessage) message).getText();
|
||||
System.out.println("Message has been consumed : " + msg);
|
||||
} catch (JMSException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Message Error");
|
||||
}
|
||||
}
|
||||
|
||||
public Employee receiveMessage() throws JMSException {
|
||||
Map map = (Map) getJmsTemplate().receiveAndConvert();
|
||||
Map map = (Map) this.jmsTemplate.receiveAndConvert();
|
||||
return new Employee((String) map.get("name"), (Integer) map.get("age"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +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: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://activemq.apache.org/schema/core
|
||||
http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd">
|
||||
|
||||
<!-- Embedded ActiveMQ Broker -->
|
||||
<amq:broker id="broker" useJmx="false" persistent="false">
|
||||
<amq:transportConnectors>
|
||||
<amq:transportConnector uri="tcp://localhost:61616" />
|
||||
</amq:transportConnectors>
|
||||
</amq:broker>
|
||||
|
||||
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"
|
||||
useShutdownHook="false">
|
||||
<amq:transportConnectors>
|
||||
<amq:transportConnector uri="tcp://localhost:61616" />
|
||||
</amq:transportConnectors>
|
||||
</amq:broker>
|
||||
</beans>
|
||||
|
||||
@@ -1,34 +1,45 @@
|
||||
<?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">
|
||||
xmlns:amq="http://activemq.apache.org/schema/core" 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-2.0.xsd
|
||||
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
|
||||
|
||||
<!-- JmsTemplate Definition -->
|
||||
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
|
||||
<property name="connectionFactory" ref="connectionFactory" />
|
||||
<property name="defaultDestination" ref="destinationQueue" />
|
||||
<property name="messageConverter" ref="myMessageConverter" />
|
||||
</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.SingleConnectionFactory">
|
||||
<constructor-arg ref="amqConnectionFactory" />
|
||||
|
||||
</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>
|
||||
<constructor-arg index="0" value="IN_QUEUE" />
|
||||
</bean>
|
||||
|
||||
|
||||
<bean id="SampleJmsMessageSender" class="com.baeldung.spring.jms.SampleJmsMessageSender">
|
||||
<property name="queue" ref="destinationQueue" />
|
||||
<property name="jmsTemplate" ref="jmsTemplate" />
|
||||
</bean>
|
||||
|
||||
<bean id="myMessageConverter" class="com.baeldung.spring.jms.SampleMessageConverter" />
|
||||
|
||||
<!-- this is the Message-Driven POJO (MDP) -->
|
||||
<bean id="messageListener" class="com.baeldung.spring.jms.SampleListener" />
|
||||
<bean id="messageListener" class="com.baeldung.spring.jms.SampleListener">
|
||||
<property name="jmsTemplate" ref="jmsTemplate" />
|
||||
<property name="queue" ref="destinationQueue" />
|
||||
</bean>
|
||||
|
||||
|
||||
<!-- and this is the message listener container -->
|
||||
<bean id="jmsContainer"
|
||||
|
||||
Reference in New Issue
Block a user