1   /*
2    * $Id: JmsDurableTopicTestCase.java 10489 2008-01-23 17:53:38Z dfeist $
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.transport.jms.integration;
11  
12  import javax.jms.Message;
13  import javax.jms.MessageConsumer;
14  import javax.jms.TopicConnection;
15  import javax.jms.TopicConnectionFactory;
16  import javax.jms.TopicSession;
17  
18  import org.apache.activemq.ActiveMQConnectionFactory;
19  import org.apache.activemq.command.ActiveMQTopic;
20  
21  public class JmsDurableTopicTestCase extends AbstractJmsFunctionalTestCase
22  {
23      public static final String TOPIC_QUEUE_NAME = "durable.broadcast";
24      private String clientId;
25  
26      protected String getConfigResources()
27      {
28          return "providers/activemq/jms-durable-topic.xml";
29      }
30  
31      public void testProviderDurableSubscriber() throws Exception
32      {
33          setClientId("Client1");
34          receive(scenarioNotReceive);
35          setClientId("Client2");
36          receive(scenarioNotReceive);
37  
38          setClientId("Sender");
39          send(scenarioNonTx);
40  
41          setClientId("Client1");
42          receive(scenarioNonTx);
43          receive(scenarioNotReceive);
44          setClientId("Client2");
45          receive(scenarioNonTx);
46          receive(scenarioNotReceive);
47      }
48  
49      Scenario scenarioNonTx = new NonTransactedScenario()
50      {
51          public String getOutputQueue()
52          {
53              return TOPIC_QUEUE_NAME;
54          }
55      };
56  
57      Scenario scenarioNotReceive = new ScenarioNotReceive()
58      {
59          public String getOutputQueue()
60          {
61              return TOPIC_QUEUE_NAME;
62          }
63      };
64  
65      public Message receive(Scenario scenario) throws Exception
66      {
67          TopicConnection connection = null;
68          try
69          {
70              TopicConnectionFactory factory = new ActiveMQConnectionFactory(scenario.getBrokerUrl());
71              connection = factory.createTopicConnection();
72              connection.setClientID(getClientId());
73              connection.start();
74              TopicSession session = null;
75              try
76              {
77                  session = connection.createTopicSession(scenario.isTransacted(), scenario.getAcknowledge());
78                  ActiveMQTopic destination = new ActiveMQTopic(scenario.getOutputQueue());
79                  MessageConsumer consumer = null;
80                  try
81                  {
82                      consumer = session.createDurableSubscriber(destination, getClientId());
83                      return scenario.receive(session, consumer);
84                  }
85                  catch (Exception e)
86                  {
87                      throw e;
88                  }
89                  finally
90                  {
91                      if (consumer != null)
92                      {
93                          consumer.close();
94                      }
95                  }
96              }
97              catch (Exception e)
98              {
99                  throw e;
100             }
101             finally
102             {
103                 if (session != null)
104                 {
105                     session.close();
106                 }
107             }
108         }
109         catch (Exception e)
110         {
111             throw e;
112         }
113         finally
114         {
115             if (connection != null)
116             {
117                 connection.close();
118             }
119         }
120     }
121 
122     public String getClientId()
123     {
124         return clientId;
125     }
126 
127     public void setClientId(String clientId)
128     {
129         this.clientId = clientId;
130     }
131 }