View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.test.integration.service;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.endpoint.InboundEndpoint;
11  import org.mule.api.service.Service;
12  import org.mule.transport.jms.JmsConnector;
13  import org.mule.transport.jms.JmsSupport;
14  
15  import javax.jms.JMSException;
16  import javax.jms.Message;
17  import javax.jms.MessageConsumer;
18  import javax.jms.MessageListener;
19  
20  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
21  
22  public class ServiceInFlightMessagesJMSTestCase extends ServiceInFlightMessagesTestCase
23  {
24      protected TestJMSMessageListener listener;
25  
26      @Override
27      protected String getConfigResources()
28      {
29          return "org/mule/test/integration/service/service-inflight-messages-jms.xml";
30      }
31  
32      @Override
33      protected void doSetUp() throws Exception
34      {
35          super.doSetUp();
36          listener = createTestJMSConsumer();
37      }
38  
39      protected void stopService(Service service) throws Exception
40      {
41          service.stop();
42          // Give connector and jms broker some time to process all pending messages
43          Thread.sleep(WAIT_TIME_MILLIS);
44      }
45  
46      protected void startService(Service service) throws Exception
47      {
48          service.start();
49      }
50  
51      private TestJMSMessageListener createTestJMSConsumer() throws MuleException, JMSException
52      {
53          TestJMSMessageListener messageListener = new TestJMSMessageListener();
54          createJMSMessageConsumer().setMessageListener(messageListener);
55          return messageListener;
56      }
57  
58      private MessageConsumer createJMSMessageConsumer() throws MuleException, JMSException
59      {
60          InboundEndpoint endpoint = muleContext.getEndpointFactory().getInboundEndpoint("jms://out");
61          JmsConnector jmsConnector = (JmsConnector) muleContext.getRegistry().lookupConnector(
62              "outPersistentConnector");
63          JmsSupport jmsSupport = jmsConnector.getJmsSupport();
64          MessageConsumer consumer = jmsSupport.createConsumer(jmsConnector.getSession(endpoint),
65              jmsSupport.createDestination(jmsConnector.getSession(endpoint), endpoint), false, endpoint);
66          return consumer;
67      }
68  
69      protected int getOutSize() throws Exception
70      {
71          return (int) (500 - listener.countdownLatch.getCount());
72      }
73  
74      protected void recreateAndStartMuleContext() throws Exception, MuleException
75      {
76          muleContext = createMuleContext();
77          muleContext.start();
78          createJMSMessageConsumer().setMessageListener(listener);
79      }
80  
81      private class TestJMSMessageListener implements MessageListener
82      {
83          public TestJMSMessageListener()
84          {
85              super();
86          }
87  
88          CountDownLatch countdownLatch = new CountDownLatch(ServiceInFlightMessagesJMSTestCase.NUM_MESSAGES);
89  
90          public void onMessage(Message message)
91          {
92              countdownLatch.countDown();
93          }
94      }
95  }