1   /*
2    * $Id: JmsMuleSideDurableTopicTestCase.java 9384 2007-10-26 08:45:02Z akuzmin $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  package org.mule.test.integration.providers.jms.functional;
11  
12  import org.mule.MuleManager;
13  
14  import javax.jms.DeliveryMode;
15  import javax.jms.JMSException;
16  import javax.jms.Message;
17  import javax.jms.MessageConsumer;
18  import javax.jms.MessageProducer;
19  import javax.jms.Session;
20  import javax.jms.TextMessage;
21  import javax.jms.TopicConnection;
22  import javax.jms.TopicConnectionFactory;
23  import javax.jms.TopicPublisher;
24  import javax.jms.TopicSession;
25  
26  import org.apache.activemq.ActiveMQConnectionFactory;
27  import org.apache.activemq.command.ActiveMQTopic;
28  
29  /**
30   * Testing durable topic when Mule is durable subscriber
31   */
32  public class JmsMuleSideDurableTopicTestCase extends AbstractJmsFunctionalTestCase
33  {
34  
35      public static final String TOPIC_QUEUE_NAME = "durable.broadcast";
36      public static final String CONNECTOR1_NAME = "jmsConnectorC1";
37  
38      protected String getConfigResources()
39      {
40          return "providers/activemq/jms-muleside-durable-topic.xml";
41      }
42  
43      public void testMuleDurableSubscriber() throws Exception
44      {
45          send(scenarioNoTx);
46          receive(scenarioNoTx);
47          receive(scenarioNoTx);
48          MuleManager.getInstance().lookupConnector(CONNECTOR1_NAME).stopConnector();
49          assertEquals(MuleManager.getInstance().lookupConnector(CONNECTOR1_NAME).isStarted(), false);
50          logger.info(CONNECTOR1_NAME + " is stopped");
51          send(scenarioNoTx);
52          MuleManager.getInstance().lookupConnector(CONNECTOR1_NAME).startConnector();
53          logger.info(CONNECTOR1_NAME + " is started");
54          receive(scenarioNoTx);
55          receive(scenarioNoTx);
56  
57      }
58  
59      Scenario scenarioNoTx = new AbstractScenario()
60      {
61          public String getInputQueue()
62          {
63              return TOPIC_QUEUE_NAME;
64          }
65  
66          public void send(Session session, MessageProducer producer) throws JMSException
67          {
68              //publish and send is the same for ActiveMQ
69              producer.send(session.createTextMessage(DEFAULT_INPUT_MESSAGE));
70  
71          }
72  
73          public Message receive(Session session, MessageConsumer consumer) throws JMSException
74          {
75              Message message = consumer.receive(TIMEOUT);
76              assertNotNull(message);
77              assertTrue(TextMessage.class.isAssignableFrom(message.getClass()));
78              assertEquals(((TextMessage) message).getText(), DEFAULT_INPUT_MESSAGE);
79              return message;
80          }
81  
82      };
83  
84  
85      public void send(Scenario scenario) throws Exception
86      {
87          TopicConnection connection = null;
88          try
89          {
90              TopicConnectionFactory factory = new ActiveMQConnectionFactory(scenario.getBrokerUrl());
91              connection = factory.createTopicConnection();
92              connection.start();
93              TopicSession session = null;
94              try
95              {
96                  session = connection.createTopicSession(scenario.isTransacted(), scenario.getAcknowledge());
97                  ActiveMQTopic destination = new ActiveMQTopic(scenario.getInputQueue());
98                  TopicPublisher publisher = null;
99                  try
100                 {
101                     publisher = session.createPublisher(destination);
102                     publisher.setDeliveryMode(DeliveryMode.PERSISTENT);
103                     scenario.send(session, publisher);
104                 }
105                 catch (Exception e)
106                 {
107                     throw e;
108                 }
109                 finally
110                 {
111                     if (publisher != null)
112                     {
113                         publisher.close();
114                     }
115                 }
116             }
117             catch (Exception e)
118             {
119                 throw e;
120             }
121             finally
122             {
123                 if (session != null)
124                 {
125                     session.close();
126                 }
127             }
128         }
129         catch (Exception e)
130         {
131             throw e;
132         }
133         finally
134         {
135             if (connection != null)
136             {
137                 connection.close();
138             }
139         }
140     }
141 
142 }