JMS Using Spring Framework and Unit Test

This is working code…

  • serviceBus.xml
  • PrinciplesMessageProducer
  • PrinciplesMessageConsumer
  • MessagingTest

<?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:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:jms="http://www.springframework.org/schema/jms"
 xmlns:amq="http://activemq.apache.org/schema/core"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.2.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

<!-- enable annotations -->
 <context:annotation-config />

<context:component-scan base-package="principles" />
 <context:component-scan base-package="activemq" />
 <context:component-scan base-package="commands" />
 <context:component-scan base-package="events" />

<!-- active mq connection factory -->
 <bean id="amqConnectionFactory"
 class="org.apache.activemq.ActiveMQConnectionFactory"
 p:brokerURL="tcp://localhost:61616"
 p:userName="admin"
 p:password="admin" />

<bean id="cachedConnectionFactory"
 class="org.springframework.jms.connection.CachingConnectionFactory"
 p:targetConnectionFactory-ref="amqConnectionFactory"
 p:sessionCacheSize="10" />

<!-- queue destination -->
 <amq:queue id="principlesRequestQueue" physicalName="Principles.Request"/>
 <amq:queue id="principlesResponseQueue" physicalName="Principles.Response"/>

<!-- jms template instance -->
 <bean id="producerTemplate"
 class="org.springframework.jms.core.JmsTemplate"
 p:connectionFactory-ref="cachedConnectionFactory"
 p:defaultDestination-ref="principlesRequestQueue" />

<!-- jms queue listener, response to requests -->
 <bean id="principlesListener" class="activemq.PrinciplesMessageConsumer" />

<!-- jms listener container -->
 <jms:listener-container
 container-type="default"
 connection-factory="cachedConnectionFactory"
 acknowledge="auto"
 concurrency="10-50">
 <jms:listener destination="Principles.Request"
 response-destination="Principles.Response"
 ref="principlesListener" />
 </jms:listener-container>
</beans>


@Component
public class PrinciplesMessageProducer {
 @Autowired
 private JmsTemplate producerTemplate;

public PrinciplesMessageProducer() {}

public void sendMessage(final String message) throws JMSException {
 producerTemplate.send(new MessageCreator() {
 @Override
 public Message createMessage(Session session) throws JMSException {
 return session.createTextMessage(message);
 }
 });
 }
}

&nbsp;

@Component
public class PrinciplesMessageConsumer implements MessageListener {
 @Override
 public void onMessage(Message message) {
 TextMessage msgText = (TextMessage)message;
 try {
 System.out.format("Consumed message -> %s", msgText.getText());
 } catch (JMSException e) {
 System.out.format("%s", e.toString());
 }
 }
}

&nbsp;

public class MessagingTest {
 private Logger LOG = LoggerFactory.getLogger("MessagingTest.class");

private ApplicationContext applicationContext;
 private PrinciplesMessageProducer msgSender;

@Test
 public void testApplicationContext() {
 assertNotNull("Application Context was null.", applicationContext);
 LOG.info("Application Context instance -> {}", applicationContext);
 }

@Test
 public void testSendAndReceive() {
 msgSender = applicationContext.getBean("principlesMessageProducer", PrinciplesMessageProducer.class);
 assertNotNull("PrinciplesMessageProducer was null", msgSender);

System.out.printf("PrinciplesMessageProducer instance -> %s%n", msgSender);
 try {
 msgSender.sendMessage(dataAsJson(data()));
 } catch (JMSException e) {
 LOG.error(e.toString());
 }
 }

public Map<String, Object> data() {
 Map<String, Object> data = new HashMap<>();
 String[] langs = {"Java", "JavaScript", "Clojure", "Groovy"};
 data.put("Programming Languages", Arrays.asList(langs));
 return data;
 }

public String dataAsJson(Map<String, Object> data) {
 Gson gson = new Gson();
 return gson.toJson(data, Map.class);
 }
 @Before
 public void setUp() {
 applicationContext = new ClassPathXmlApplicationContext("classpath:/principles/serviceBus.xml");
 }

@After
 public void tearDown() {
 applicationContext = null;
 }
}

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s