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.transport.jms.integration;
8   
9   import javax.jms.DeliveryMode;
10  import javax.jms.JMSException;
11  import javax.jms.Message;
12  import javax.jms.MessageConsumer;
13  import javax.jms.MessageProducer;
14  import javax.jms.Session;
15  import javax.jms.TextMessage;
16  import javax.jms.Topic;
17  import javax.jms.TopicConnection;
18  import javax.jms.TopicPublisher;
19  import javax.jms.TopicSession;
20  
21  import org.junit.Test;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  
27  public class JmsMuleSideDurableTopicTestCase extends AbstractJmsFunctionalTestCase
28  {
29  
30      public static final String CONNECTOR1_NAME = "jmsConnectorC1";
31  
32      @Override
33      protected String getConfigResources()
34      {
35          return "integration/jms-muleside-durable-topic.xml";
36      }
37  
38      @Test
39      public void testMuleDurableSubscriber() throws Exception
40      {
41          send(scenarioNoTx);
42          receive(scenarioNoTx);
43          receive(scenarioNoTx);
44          muleContext.getRegistry().lookupConnector(CONNECTOR1_NAME).stop();
45          assertEquals(muleContext.getRegistry().lookupConnector(CONNECTOR1_NAME).isStarted(), false);
46          logger.info(CONNECTOR1_NAME + " is stopped");
47          send(scenarioNoTx);
48          muleContext.getRegistry().lookupConnector(CONNECTOR1_NAME).start();
49          logger.info(CONNECTOR1_NAME + " is started");
50          receive(scenarioNoTx);
51          receive(scenarioNoTx);
52      }
53  
54      Scenario scenarioNoTx = new NonTransactedScenario()
55      {
56          public String getInputDestinationName()
57          {
58              return getJmsConfig().getBroadcastDestinationName();
59          }
60  
61          public void send(Session session, MessageProducer producer) throws JMSException
62          {
63              // publish and send is the same for ActiveMQ
64              producer.send(session.createTextMessage(DEFAULT_INPUT_MESSAGE));
65  
66          }
67  
68          public Message receive(Session session, MessageConsumer consumer) throws JMSException
69          {
70              Message message = consumer.receive(getTimeout());
71              assertNotNull(message);
72              assertTrue(TextMessage.class.isAssignableFrom(message.getClass()));
73              assertEquals(((TextMessage) message).getText(), DEFAULT_OUTPUT_MESSAGE);
74              return message;
75          }
76  
77      };
78  
79      public void send(Scenario scenario) throws Exception
80      {
81          TopicConnection connection = null;
82          try
83          {
84              connection = (TopicConnection) getConnection(true, false);
85              connection.start();
86              TopicSession session = null;
87              try
88              {
89                  session = connection.createTopicSession(scenario.isTransacted(), scenario.getAcknowledge());
90                  Topic destination = session.createTopic(scenario.getInputDestinationName());
91                  TopicPublisher publisher = null;
92                  try
93                  {
94                      publisher = session.createPublisher(destination);
95                      publisher.setDeliveryMode(DeliveryMode.PERSISTENT);
96                      scenario.send(session, publisher);
97                  }
98                  finally
99                  {
100                     if (publisher != null)
101                     {
102                         publisher.close();
103                     }
104                 }
105             }
106             finally
107             {
108                 if (session != null)
109                 {
110                     session.close();
111                 }
112             }
113         }
114         finally
115         {
116             if (connection != null)
117             {
118                 connection.close();
119             }
120         }
121     }
122 
123 }