View Javadoc

1   /*
2    * $Id: JmsMuleSideDurableTopicTestCase.java 22431 2011-07-18 07:40:35Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.jms.integration;
12  
13  import javax.jms.DeliveryMode;
14  import javax.jms.JMSException;
15  import javax.jms.Message;
16  import javax.jms.MessageConsumer;
17  import javax.jms.MessageProducer;
18  import javax.jms.Session;
19  import javax.jms.TextMessage;
20  import javax.jms.Topic;
21  import javax.jms.TopicConnection;
22  import javax.jms.TopicPublisher;
23  import javax.jms.TopicSession;
24  
25  import org.junit.Test;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertNotNull;
29  import static org.junit.Assert.assertTrue;
30  
31  public class JmsMuleSideDurableTopicTestCase extends AbstractJmsFunctionalTestCase
32  {
33  
34      public static final String CONNECTOR1_NAME = "jmsConnectorC1";
35  
36      @Override
37      protected String getConfigResources()
38      {
39          return "integration/jms-muleside-durable-topic.xml";
40      }
41  
42      @Test
43      public void testMuleDurableSubscriber() throws Exception
44      {
45          send(scenarioNoTx);
46          receive(scenarioNoTx);
47          receive(scenarioNoTx);
48          muleContext.getRegistry().lookupConnector(CONNECTOR1_NAME).stop();
49          assertEquals(muleContext.getRegistry().lookupConnector(CONNECTOR1_NAME).isStarted(), false);
50          logger.info(CONNECTOR1_NAME + " is stopped");
51          send(scenarioNoTx);
52          muleContext.getRegistry().lookupConnector(CONNECTOR1_NAME).start();
53          logger.info(CONNECTOR1_NAME + " is started");
54          receive(scenarioNoTx);
55          receive(scenarioNoTx);
56      }
57  
58      Scenario scenarioNoTx = new NonTransactedScenario()
59      {
60          @Override
61          public String getInputDestinationName()
62          {
63              return getJmsConfig().getBroadcastDestinationName();
64          }
65  
66          @Override
67          public void send(Session session, MessageProducer producer) throws JMSException
68          {
69              // publish and send is the same for ActiveMQ
70              producer.send(session.createTextMessage(DEFAULT_INPUT_MESSAGE));
71  
72          }
73  
74          @Override
75          public Message receive(Session session, MessageConsumer consumer) throws JMSException
76          {
77              Message message = consumer.receive(getTimeout());
78              assertNotNull(message);
79              assertTrue(TextMessage.class.isAssignableFrom(message.getClass()));
80              assertEquals(((TextMessage) message).getText(), DEFAULT_OUTPUT_MESSAGE);
81              return message;
82          }
83      };
84  
85      @Override
86      public void send(Scenario scenario) throws Exception
87      {
88          TopicConnection connection = null;
89          try
90          {
91              connection = (TopicConnection) getConnection(true, false);
92              connection.start();
93              TopicSession session = null;
94              try
95              {
96                  session = connection.createTopicSession(scenario.isTransacted(), scenario.getAcknowledge());
97                  Topic destination = session.createTopic(scenario.getInputDestinationName());
98                  TopicPublisher publisher = null;
99                  try
100                 {
101                     publisher = session.createPublisher(destination);
102                     publisher.setDeliveryMode(DeliveryMode.PERSISTENT);
103                     scenario.send(session, publisher);
104                 }
105                 finally
106                 {
107                     if (publisher != null)
108                     {
109                         publisher.close();
110                     }
111                 }
112             }
113             finally
114             {
115                 if (session != null)
116                 {
117                     session.close();
118                 }
119             }
120         }
121         finally
122         {
123             if (connection != null)
124             {
125                 connection.close();
126             }
127         }
128     }
129 
130 }