View Javadoc

1   /*
2    * $Id: JmsDurableTopicTestCase.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.Connection;
14  import javax.jms.Message;
15  import javax.jms.MessageConsumer;
16  import javax.jms.Session;
17  import javax.jms.Topic;
18  
19  import org.junit.Test;
20  
21  public class JmsDurableTopicTestCase extends AbstractJmsFunctionalTestCase
22  {
23      private String clientId;
24  
25      protected String getConfigResources()
26      {
27          return "integration/jms-durable-topic.xml";
28      }
29  
30      @Test
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 getOutputDestinationName()
52          {
53              return getJmsConfig().getBroadcastDestinationName();
54          }
55      };
56  
57      Scenario scenarioNotReceive = new ScenarioNotReceive()
58      {
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 }