This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Using ActiveMQ Server in Integration Tests

The test class given below demonstrates how to use the ActiveMQ broker to push and pop messages from queues. You can start and stop the ActiveMQ server and send messages to a queue or a topic. First, the test class starts a JMS broker server under the startJMSBroker method and then pushes and pops a message in subsequent test methods. After the test, the JMS broker is stopped under the stopJMSBroker method.

public class ActiveMQJMSBrokerTestCase {

     private JMSBrokerController activeMqBroker;

     @BeforeClass(alwaysRun = true)
    public void startJMSBroker() throws Exception {

         //starting jms broker
        activeMqBroker = new JMSBrokerController("localhost", getJMSBrokerConfiguration());
        if (!JMSBrokerController.isBrokerStarted()) {
            Assert.assertTrue(activeMqBroker.start(), "JMS Broker(ActiveMQ) stating failed");
        }
    }

     @AfterClass(alwaysRun = true)
    public void stopJMSBroker() throws Exception {

         if (activeMqBroker != null) {
            Assert.assertTrue(activeMqBroker.stop(), "JMS Broker(ActiveMQ) Stopping failed");
        }
    }

     @Test(groups = {"wso2.esb"}, description = "Test JMS broker queue clients with popMessage(java.lang.Class<T> clzz)")
    public void JMSBrokerQueueTest1() throws Exception {
        int numberOfMsgToExpect = 10;
        JMSQueueMessageProducer sender = 
                new JMSQueueMessageProducer(JMSBrokerConfigurationProvider.getInstance().getBrokerConfiguration());
        String queueName = "JmsBrokerTestQueue1";
        String message = "<?xml version='1.0' encoding='UTF-8'?>" +
                         "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"" +
                         " xmlns:ser=\"http://services.samples\" xmlns:xsd=\"http://services.samples/xsd\">" +
                         "  <soapenv:Header/>" +
                         "  <soapenv:Body>" +
                         "   <ser:placeOrder>" +
                         "     <ser:order>" +
                         "      <xsd:price>100</xsd:price>" +
                         "      <xsd:quantity>2000</xsd:quantity>" +
                         "      <xsd:symbol>JMSTransport</xsd:symbol>" +
                         "     </ser:order>" +
                         "   </ser:placeOrder>" +
                         "  </soapenv:Body>" +
                         "</soapenv:Envelope>";
        try {
            sender.connect(queueName);
            for (int i = 0; i < numberOfMsgToExpect; i++) {
                sender.pushMessage(message);
            }
        } finally {
            sender.disconnect();
        }

         JMSQueueMessageConsumer consumer = 
                new JMSQueueMessageConsumer(JMSBrokerConfigurationProvider.getInstance().getBrokerConfiguration());
        try {
            consumer.connect(queueName);
            for (int i = 0; i < numberOfMsgToExpect; i++) {
                if (consumer.popMessage(javax.jms.Message.class) == null) {
                    Assert.fail("Unable to pop the expected number of message in the queue" + queueName);
                }
            }
        } finally {
            consumer.disconnect();
        }
    }

     @Test(groups = {"wso2.esb"}, description = "Test JMS broker queue clients with popMessage()")
    public void JMSBrokerQueueTest2() throws Exception {
        int numberOfMsgToExpect = 10;
        JMSQueueMessageProducer sender = new JMSQueueMessageProducer(JMSBrokerConfigurationProvider.getInstance().getBrokerConfiguration());
        String queueName = "JmsBrokerTestQueue2";
        String message = "<?xml version='1.0' encoding='UTF-8'?>" +
                         "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"" +
                         " xmlns:ser=\"http://services.samples\" xmlns:xsd=\"http://services.samples/xsd\">" +
                         "  <soapenv:Header/>" +
                         "  <soapenv:Body>" +
                         "   <ser:placeOrder>" +
                         "     <ser:order>" +
                         "      <xsd:price>100</xsd:price>" +
                         "      <xsd:quantity>2000</xsd:quantity>" +
                         "      <xsd:symbol>JMSTransport</xsd:symbol>" +
                         "     </ser:order>" +
                         "   </ser:placeOrder>" +
                         "  </soapenv:Body>" +
                         "</soapenv:Envelope>";
        try {
            sender.connect(queueName);
            for (int i = 0; i < numberOfMsgToExpect; i++) {
                sender.pushMessage(message);
            }
        } finally {
            sender.disconnect();
        }

         JMSQueueMessageConsumer consumer =
                new JMSQueueMessageConsumer(JMSBrokerConfigurationProvider.getInstance().getBrokerConfiguration());
        try {
            consumer.connect(queueName);
            for (int i = 0; i < numberOfMsgToExpect; i++) {
                if (consumer.popMessage() == null) {
                    Assert.fail("Unable to pop the expected number of message in the queue" + queueName);
                }
            }
        } finally {
            consumer.disconnect();
        }
    }

     @Test(groups = {"wso2.esb"}, description = "Test JMS broker queue clients with popRawMessage()")
    public void JMSBrokerQueueTest3() throws Exception {
        int numberOfMsgToExpect = 10;
        JMSQueueMessageProducer sender = 
                new JMSQueueMessageProducer(JMSBrokerConfigurationProvider.getInstance().getBrokerConfiguration());
        String queueName = "JmsBrokerTestQueue3";
        String message = "<?xml version='1.0' encoding='UTF-8'?>" +
                         "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"" +
                         " xmlns:ser=\"http://services.samples\" xmlns:xsd=\"http://services.samples/xsd\">" +
                         "  <soapenv:Header/>" +
                         "  <soapenv:Body>" +
                         "   <ser:placeOrder>" +
                         "     <ser:order>" +
                         "      <xsd:price>100</xsd:price>" +
                         "      <xsd:quantity>2000</xsd:quantity>" +
                         "      <xsd:symbol>JMSTransport</xsd:symbol>" +
                         "     </ser:order>" +
                         "   </ser:placeOrder>" +
                         "  </soapenv:Body>" +
                         "</soapenv:Envelope>";
        try {
            sender.connect(queueName);
            for (int i = 0; i < numberOfMsgToExpect; i++) {
                sender.pushMessage(message);
            }
        } finally {
            sender.disconnect();
        }

         JMSQueueMessageConsumer consumer = 
                new JMSQueueMessageConsumer(JMSBrokerConfigurationProvider.getInstance().getBrokerConfiguration());
        try {
            consumer.connect(queueName);
            for (int i = 0; i < numberOfMsgToExpect; i++) {
                if (consumer.popRawMessage() == null) {
                    Assert.fail("Unable to pop the expected number of message in the queue" + queueName);
                }
            }
        } finally {
            consumer.disconnect();
        }
    }

 
     private JMSBrokerConfiguration getJMSBrokerConfiguration() {
        return JMSBrokerConfigurationProvider.getInstance().getBrokerConfiguration();
    }
}Â