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:
Kiran 2016-10-12 12:08:13 -04:00 committed by Grzegorz Piwowarek
parent c0d4eee669
commit 65cd2dfb06
6 changed files with 76 additions and 60 deletions

View File

@ -11,6 +11,7 @@
<properties> <properties>
<springframework.version>4.3.2.RELEASE</springframework.version> <springframework.version>4.3.2.RELEASE</springframework.version>
<activemq.version>5.12.0</activemq.version> <activemq.version>5.12.0</activemq.version>
<junit.version>4.12</junit.version>
</properties> </properties>
<dependencies> <dependencies>
@ -29,7 +30,7 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.12</version> <version>${junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
@ -58,16 +59,16 @@
</plugin> </plugin>
</plugins> </plugins>
</pluginManagement> </pluginManagement>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>1.8</source> <source>1.8</source>
<target>1.8</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
<finalName>spring-jms</finalName> <finalName>spring-jms</finalName>
</build> </build>
</project> </project>

View File

@ -24,10 +24,6 @@ public class SampleJmsMessageSender {
} }
public void sendMessage(final Employee employee) { public void sendMessage(final Employee employee) {
System.out.println("Jms Message Sender : " + employee); this.jmsTemplate.convertAndSend(employee);
Map<String, Object> map = new HashMap<>();
map.put("name", employee.getName());
map.put("age", employee.getAge());
this.jmsTemplate.convertAndSend(map);
} }
} }

View File

@ -5,13 +5,21 @@ import org.springframework.jms.core.JmsTemplate;
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.Queue;
import javax.jms.TextMessage; import javax.jms.TextMessage;
import java.util.Map; import java.util.Map;
public class SampleListener implements MessageListener { public class SampleListener implements MessageListener {
public JmsTemplate getJmsTemplate() { private JmsTemplate jmsTemplate;
return getJmsTemplate(); private Queue queue;
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void setQueue(Queue queue) {
this.queue = queue;
} }
public void onMessage(Message message) { public void onMessage(Message message) {
@ -19,17 +27,14 @@ public class SampleListener implements MessageListener {
try { try {
String msg = ((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);
} }
} else {
throw new IllegalArgumentException("Message Error");
} }
} }
public Employee receiveMessage() throws JMSException { 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")); return new Employee((String) map.get("name"), (Integer) map.get("age"));
} }
} }

View File

@ -1,15 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" <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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:amq="http://activemq.apache.org/schema/core" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://activemq.apache.org/schema/core xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd"> xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
xmlns:amq="http://activemq.apache.org/schema/core"
<!-- Embedded ActiveMQ Broker --> 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
<amq:broker id="broker" useJmx="false" persistent="false"> 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">
<amq:transportConnectors>
<amq:transportConnector uri="tcp://localhost:61616" /> <!-- Embedded ActiveMQ Broker -->
</amq:transportConnectors> <amq:broker id="broker" useJmx="false" persistent="false"
</amq:broker> useShutdownHook="false">
<amq:transportConnectors>
<amq:transportConnector uri="tcp://localhost:61616" />
</amq:transportConnectors>
</amq:broker>
</beans> </beans>

View File

@ -1,34 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd"> 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"> <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="IN_QUEUE" /> <constructor-arg index="0" value="IN_QUEUE" />
</bean> </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="queue" ref="destinationQueue" /> <property name="queue" ref="destinationQueue" />
<property name="jmsTemplate" ref="jmsTemplate" /> <property name="jmsTemplate" ref="jmsTemplate" />
</bean> </bean>
<bean id="myMessageConverter" class="com.baeldung.spring.jms.SampleMessageConverter" />
<!-- this is the Message-Driven POJO (MDP) --> <!-- 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 --> <!-- and this is the message listener container -->
<bean id="jmsContainer" <bean id="jmsContainer"

View File

@ -5,7 +5,7 @@ import org.junit.Test;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MapMessageConvertAndSendTest { public class DefaultTextMessageSenderTest {
private static SampleJmsMessageSender messageProducer; private static SampleJmsMessageSender messageProducer;
@ -17,8 +17,8 @@ public class MapMessageConvertAndSendTest {
} }
@Test @Test
public void testSendMessage() { public void testSimpleSend() {
messageProducer.sendMessage(new Employee("JavaDeveloper2", 22)); messageProducer.simpleSend();
} }
} }