View Javadoc

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