1
2
3
4
5
6
7
8
9
10 package org.mule.test.integration.providers.jms.functional;
11
12 import javax.jms.JMSException;
13 import javax.jms.Message;
14 import javax.jms.MessageConsumer;
15 import javax.jms.MessageProducer;
16 import javax.jms.Session;
17 import javax.jms.TopicConnection;
18 import javax.jms.TopicConnectionFactory;
19 import javax.jms.TopicSession;
20
21 import org.apache.activemq.ActiveMQConnectionFactory;
22 import org.apache.activemq.command.ActiveMQTopic;
23
24
25
26
27 public class JmsDurableTopicTestCase extends AbstractJmsFunctionalTestCase
28 {
29
30 public static final String TOPIC_QUEUE_NAME = "durable.broadcast";
31 private String clientId;
32
33 protected String getConfigResources()
34 {
35 return "providers/activemq/jms-durable-topic.xml";
36 }
37
38 public void testProviderDurableSubscriber() throws Exception
39 {
40 setClientId("Client1");
41 receive(scenarioNotReceive);
42 setClientId("Client2");
43 receive(scenarioNotReceive);
44
45 setClientId("Sender");
46 send(scenarioNonTx);
47
48 setClientId("Client1");
49 receive(scenarioNonTx);
50 receive(scenarioNotReceive);
51 setClientId("Client2");
52 receive(scenarioNonTx);
53 receive(scenarioNotReceive);
54
55 }
56
57 AbstractJmsFunctionalTestCase.Scenario scenarioNonTx = new AbstractJmsFunctionalTestCase.AbstractScenario()
58 {
59
60 public String getOutputQueue()
61 {
62 return TOPIC_QUEUE_NAME;
63 }
64
65 public void send(Session session, MessageProducer producer) throws JMSException
66 {
67 producer.send(session.createTextMessage(DEFAULT_INPUT_MESSAGE));
68 }
69
70 public Message receive(Session session, MessageConsumer consumer) throws JMSException
71 {
72 Message message = consumer.receive(TIMEOUT);
73 assertNotNull(message);
74 return message;
75 }
76
77 };
78
79 AbstractJmsFunctionalTestCase.Scenario scenarioNotReceive = new AbstractJmsFunctionalTestCase.AbstractScenario()
80 {
81
82 public String getOutputQueue()
83 {
84 return TOPIC_QUEUE_NAME;
85 }
86
87 public Message receive(Session session, MessageConsumer consumer) throws JMSException
88 {
89 Message message = consumer.receive(SMALL_TIMEOUT);
90 assertNull(message);
91 return message;
92 }
93
94 };
95
96 public Message receive(Scenario scenario) throws Exception
97 {
98 TopicConnection connection = null;
99 try
100 {
101 TopicConnectionFactory factory = new ActiveMQConnectionFactory(scenario.getBrokerUrl());
102 connection = factory.createTopicConnection();
103 connection.setClientID(getClientId());
104 connection.start();
105 TopicSession session = null;
106 try
107 {
108 session = connection.createTopicSession(scenario.isTransacted(), scenario.getAcknowledge());
109 ActiveMQTopic destination = new ActiveMQTopic(scenario.getOutputQueue());
110 MessageConsumer consumer = null;
111 try
112 {
113 consumer = session.createDurableSubscriber(destination, getClientId());
114 return scenario.receive(session, consumer);
115 }
116 catch (Exception e)
117 {
118 throw e;
119 }
120 finally
121 {
122 if (consumer != null)
123 {
124 consumer.close();
125 }
126 }
127 }
128 catch (Exception e)
129 {
130 throw e;
131 }
132 finally
133 {
134 if (session != null)
135 {
136 session.close();
137 }
138 }
139 }
140 catch (Exception e)
141 {
142 throw e;
143 }
144 finally
145 {
146 if (connection != null)
147 {
148 connection.close();
149 }
150 }
151 }
152
153 public String getClientId()
154 {
155 return clientId;
156 }
157
158 public void setClientId(String clientId)
159 {
160 this.clientId = clientId;
161 }
162 }