View Javadoc

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