Skip to content

Commit 65cd2df

Browse files
kirangonellapivovarit
authored andcommitted
Code refactored and updated (eugenp#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
1 parent c0d4eee commit 65cd2df

6 files changed

Lines changed: 77 additions & 61 deletions

File tree

‎spring-jms/pom.xml‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<properties>
1212
<springframework.version>4.3.2.RELEASE</springframework.version>
1313
<activemq.version>5.12.0</activemq.version>
14+
<junit.version>4.12</junit.version>
1415
</properties>
1516

1617
<dependencies>
@@ -29,7 +30,7 @@
2930
<dependency>
3031
<groupId>junit</groupId>
3132
<artifactId>junit</artifactId>
32-
<version>4.12</version>
33+
<version>${junit.version}</version>
3334
<scope>test</scope>
3435
</dependency>
3536
</dependencies>
@@ -58,16 +59,16 @@
5859
</plugin>
5960
</plugins>
6061
</pluginManagement>
61-
<plugins>
62-
<plugin>
63-
<groupId>org.apache.maven.plugins</groupId>
64-
<artifactId>maven-compiler-plugin</artifactId>
65-
<configuration>
66-
<source>1.8</source>
67-
<target>1.8</target>
68-
</configuration>
69-
</plugin>
70-
</plugins>
71-
<finalName>spring-jms</finalName>
62+
<plugins>
63+
<plugin>
64+
<groupId>org.apache.maven.plugins</groupId>
65+
<artifactId>maven-compiler-plugin</artifactId>
66+
<configuration>
67+
<source>1.8</source>
68+
<target>1.8</target>
69+
</configuration>
70+
</plugin>
71+
</plugins>
72+
<finalName>spring-jms</finalName>
7273
</build>
73-
</project>
74+
</project>

‎spring-jms/src/main/java/com/baeldung/spring/jms/SampleJmsMessageSender.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ public void simpleSend() {
2424
}
2525

2626
public void sendMessage(final Employee employee) {
27-
System.out.println("Jms Message Sender : " + employee);
28-
Map<String, Object> map = new HashMap<>();
29-
map.put("name", employee.getName());
30-
map.put("age", employee.getAge());
31-
this.jmsTemplate.convertAndSend(map);
27+
this.jmsTemplate.convertAndSend(employee);
3228
}
3329
}

‎spring-jms/src/main/java/com/baeldung/spring/jms/SampleListener.java‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,36 @@
55
import javax.jms.JMSException;
66
import javax.jms.Message;
77
import javax.jms.MessageListener;
8+
import javax.jms.Queue;
89
import javax.jms.TextMessage;
910
import java.util.Map;
1011

1112
public class SampleListener implements MessageListener {
1213

13-
public JmsTemplate getJmsTemplate() {
14-
return getJmsTemplate();
14+
private JmsTemplate jmsTemplate;
15+
private Queue queue;
16+
17+
public void setJmsTemplate(JmsTemplate jmsTemplate) {
18+
this.jmsTemplate = jmsTemplate;
19+
}
20+
21+
public void setQueue(Queue queue) {
22+
this.queue = queue;
1523
}
1624

1725
public void onMessage(Message message) {
1826
if (message instanceof TextMessage) {
1927
try {
2028

2129
String msg = ((TextMessage) message).getText();
22-
System.out.println("Message has been consumed : " + msg);
2330
} catch (JMSException ex) {
2431
throw new RuntimeException(ex);
2532
}
26-
} else {
27-
throw new IllegalArgumentException("Message Error");
2833
}
2934
}
3035

3136
public Employee receiveMessage() throws JMSException {
32-
Map map = (Map) getJmsTemplate().receiveAndConvert();
37+
Map map = (Map) this.jmsTemplate.receiveAndConvert();
3338
return new Employee((String) map.get("name"), (Integer) map.get("age"));
3439
}
3540
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4-
xmlns:amq="http://activemq.apache.org/schema/core"
5-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://activemq.apache.org/schema/core
6-
http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd">
7-
8-
<!-- Embedded ActiveMQ Broker -->
9-
<amq:broker id="broker" useJmx="false" persistent="false">
10-
<amq:transportConnectors>
11-
<amq:transportConnector uri="tcp://localhost:61616" />
12-
</amq:transportConnectors>
13-
</amq:broker>
14-
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4+
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
5+
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
6+
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
7+
xmlns:amq="http://activemq.apache.org/schema/core"
8+
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
9+
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">
10+
11+
<!-- Embedded ActiveMQ Broker -->
12+
<amq:broker id="broker" useJmx="false" persistent="false"
13+
useShutdownHook="false">
14+
<amq:transportConnectors>
15+
<amq:transportConnector uri="tcp://localhost:61616" />
16+
</amq:transportConnectors>
17+
</amq:broker>
1518
</beans>

‎spring-jms/src/main/resources/applicationContext.xml‎

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,45 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5-
6-
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
7-
<constructor-arg index="0" value="tcp://localhost:61616" />
8-
</bean>
9-
10-
<!-- ConnectionFactory Definition -->
11-
<bean id="connectionFactory"
12-
class="org.springframework.jms.connection.CachingConnectionFactory">
13-
<constructor-arg ref="amqConnectionFactory" />
14-
</bean>
15-
3+
xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
5+
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
6+
7+
<!-- JmsTemplate Definition -->
8+
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
9+
<property name="connectionFactory" ref="connectionFactory" />
10+
<property name="defaultDestination" ref="destinationQueue" />
11+
<property name="messageConverter" ref="myMessageConverter" />
12+
</bean>
13+
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
14+
<constructor-arg index="0" value="tcp://localhost:61616" />
15+
</bean>
16+
17+
<!-- ConnectionFactory Definition -->
18+
<bean id="connectionFactory"
19+
class="org.springframework.jms.connection.SingleConnectionFactory">
20+
<constructor-arg ref="amqConnectionFactory" />
21+
22+
</bean>
23+
24+
1625
<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
17-
<constructor-arg index="0" value="IN_QUEUE" />
18-
</bean>
19-
20-
<!-- JmsTemplate Definition -->
21-
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
22-
<property name="connectionFactory" ref="connectionFactory" />
23-
<property name="defaultDestination" ref="destinationQueue" />
24-
</bean>
26+
<constructor-arg index="0" value="IN_QUEUE" />
27+
</bean>
28+
29+
2530
<bean id="SampleJmsMessageSender" class="com.baeldung.spring.jms.SampleJmsMessageSender">
2631
<property name="queue" ref="destinationQueue" />
2732
<property name="jmsTemplate" ref="jmsTemplate" />
2833
</bean>
2934

35+
<bean id="myMessageConverter" class="com.baeldung.spring.jms.SampleMessageConverter" />
36+
3037
<!-- this is the Message-Driven POJO (MDP) -->
31-
<bean id="messageListener" class="com.baeldung.spring.jms.SampleListener" />
38+
<bean id="messageListener" class="com.baeldung.spring.jms.SampleListener">
39+
<property name="jmsTemplate" ref="jmsTemplate" />
40+
<property name="queue" ref="destinationQueue" />
41+
</bean>
42+
3243

3344
<!-- and this is the message listener container -->
3445
<bean id="jmsContainer"

‎spring-jms/src/test/java/com/baeldung/spring/jms/MapMessageConvertAndSendTest.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.springframework.context.ApplicationContext;
66
import org.springframework.context.support.ClassPathXmlApplicationContext;
77

8-
public class MapMessageConvertAndSendTest {
8+
public class DefaultTextMessageSenderTest {
99

1010
private static SampleJmsMessageSender messageProducer;
1111

@@ -17,8 +17,8 @@ public static void setUp() {
1717
}
1818

1919
@Test
20-
public void testSendMessage() {
21-
messageProducer.sendMessage(new Employee("JavaDeveloper2", 22));
20+
public void testSimpleSend() {
21+
messageProducer.simpleSend();
2222
}
2323

2424
}

0 commit comments

Comments
 (0)