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.Connection;
10  import javax.jms.Message;
11  import javax.jms.MessageConsumer;
12  import javax.jms.Session;
13  import javax.jms.Topic;
14  
15  import org.junit.Test;
16  
17  public class JmsDurableTopicTestCase extends AbstractJmsFunctionalTestCase
18  {
19  
20      private String clientId;
21  
22      @Override
23      protected String getConfigResources()
24      {
25          return "integration/jms-durable-topic.xml";
26      }
27  
28      @Test
29      public void testProviderDurableSubscriber() throws Exception
30      {
31          setClientId("Client1");
32          receive(scenarioNotReceive);
33          setClientId("Client2");
34          receive(scenarioNotReceive);
35  
36          setClientId("Sender");
37          send(scenarioNonTx);
38  
39          setClientId("Client1");
40          receive(scenarioNonTx);
41          receive(scenarioNotReceive);
42          setClientId("Client2");
43          receive(scenarioNonTx);
44          receive(scenarioNotReceive);
45      }
46  
47      Scenario scenarioNonTx = new NonTransactedScenario()
48      {
49          public String getOutputDestinationName()
50          {
51              return getJmsConfig().getBroadcastDestinationName();
52          }
53      };
54  
55      Scenario scenarioNotReceive = new ScenarioNotReceive()
56      {
57  
58          @Override
59          public String getOutputDestinationName()
60          {
61              return getJmsConfig().getBroadcastDestinationName();
62          }
63      };
64  
65      public Message receive(Scenario scenario) throws Exception
66      {
67          Connection connection = null;
68          try
69          {
70              connection = getConnection(true, false);
71              connection.setClientID(getClientId());
72              connection.start();
73              Session session = null;
74              try
75              {
76                  session = connection.createSession(scenario.isTransacted(), scenario.getAcknowledge());
77                  Topic destination = session.createTopic(scenario.getOutputDestinationName());
78                  MessageConsumer consumer = null;
79                  try
80                  {
81                      consumer = session.createDurableSubscriber(destination, getClientId());
82                      return scenario.receive(session, consumer);
83                  }
84                  finally
85                  {
86                      if (consumer != null)
87                      {
88                          consumer.close();
89                      }
90                  }
91              }
92              finally
93              {
94                  if (session != null)
95                  {
96                      session.close();
97                  }
98              }
99          }
100         finally
101         {
102             if (connection != null)
103             {
104                 connection.close();
105             }
106         }
107     }
108 
109     public String getClientId()
110     {
111         return clientId;
112     }
113 
114     public void setClientId(String clientId)
115     {
116         this.clientId = clientId;
117     }
118 }