SyntaxHighlighter

Friday, March 29, 2013

Spring Integration, Spring JMS, ActiveMQ and testing Topics

Spring Integration, Spring JMS, ActiveMQ and testing Topics

so everyone knows how to test a JMS Queue in JUnit, ActiveMQ and - to make life easy - Spring JmsTemplate.  but, testing a Topic is a slightly different matter.

first, a topic subscriber has to be 'connected' before you can publish the message.  that's a bit of a challenge to setup.  but, with a little Spring Integration to the rescue we can get a subscriber sorted and sne a message via the JmsTemplate.

another catch comes from ActiveMQ.  to test queues, you can setup the ActiveMQConnectionFactory and you're done.  however, with Topics, you'll need the PooledConnectionFactory as well.

first, a couple dependencies for our pom


   
    org.apache.activemq
    activemq-core
    5.6.0
    test
   
   
    org.apache.activemq
    activemq-pool
    5.6.0
    test
   


then a little Spring Integration to listen to the Topic




 
  
 
  
 
 



(i prefer a queue channel, just to make it easier to get the messages off in testing)
so the next part would be the test class

package de.incompleteco.spring.integration.test;

import static org.junit.Assert.assertNotNull;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.Topic;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.core.PollableChannel;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/META-INF/spring/integration-context.xml"})
public class TopicIntegrationTest {

 @Autowired
 private PollableChannel receiveChannel;
 
 @Autowired
 private ConnectionFactory connectionFactory;
 
 @Autowired
 private Topic topic;
 
 private JmsTemplate jmsTemplate;
 
 @Test
 public void test() throws Exception {
  //setup the jmsTemplate and send
  jmsTemplate = new JmsTemplate(connectionFactory);
  jmsTemplate.setDefaultDestination(topic);
  jmsTemplate.setPubSubDomain(true);//it's a topic
  //send
  jmsTemplate.send(new MessageCreator() {

   public javax.jms.Message createMessage(Session session) throws JMSException {
    return session.createTextMessage("hello world");
   } });
  //now grab it off the queue
  Message message = receiveChannel.receive(1000);//let's put a timeout
  //test
  assertNotNull(message);
 }
 
}


and here's the specific apache configuration to support Topics



 
  
   
    
   
  
 





1 comment:

  1. Hello,
    The Article on Spring Integration, Spring JMS, ActiveMQ and testing Topics is informative. It gives detailed information about it.Thanks for Sharing the information on Software Testing and Frame Work .For More information check the detail on Integration testing check, Software Testing Services

    ReplyDelete