1
2
3
4
5
6
7
8
9
10 package org.mule.transport.jms.integration;
11
12
13 import javax.jms.DeliveryMode;
14 import javax.jms.JMSException;
15 import javax.jms.Message;
16 import javax.jms.MessageConsumer;
17 import javax.jms.MessageProducer;
18 import javax.jms.Session;
19 import javax.jms.TextMessage;
20 import javax.jms.TopicConnection;
21 import javax.jms.TopicConnectionFactory;
22 import javax.jms.TopicPublisher;
23 import javax.jms.TopicSession;
24
25 import org.apache.activemq.ActiveMQConnectionFactory;
26 import org.apache.activemq.command.ActiveMQTopic;
27
28 public class JmsMuleSideDurableTopicTestCase extends AbstractJmsFunctionalTestCase
29 {
30
31 public static final String TOPIC_QUEUE_NAME = "durable.broadcast";
32 public static final String CONNECTOR1_NAME = "jmsConnectorC1";
33
34 protected String getConfigResources()
35 {
36 return "providers/activemq/jms-muleside-durable-topic.xml";
37 }
38
39 public void testMuleDurableSubscriber() throws Exception
40 {
41 send(scenarioNoTx);
42 receive(scenarioNoTx);
43 receive(scenarioNoTx);
44 muleContext.getRegistry().lookupConnector(CONNECTOR1_NAME).stop();
45 assertEquals(muleContext.getRegistry().lookupConnector(CONNECTOR1_NAME).isStarted(), false);
46 logger.info(CONNECTOR1_NAME + " is stopped");
47 send(scenarioNoTx);
48 muleContext.getRegistry().lookupConnector(CONNECTOR1_NAME).start();
49 logger.info(CONNECTOR1_NAME + " is started");
50 receive(scenarioNoTx);
51 receive(scenarioNoTx);
52
53 }
54
55 Scenario scenarioNoTx = new NonTransactedScenario()
56 {
57 public String getInputQueue()
58 {
59 return TOPIC_QUEUE_NAME;
60 }
61
62 public void send(Session session, MessageProducer producer) throws JMSException
63 {
64
65 producer.send(session.createTextMessage(DEFAULT_INPUT_MESSAGE));
66
67 }
68
69 public Message receive(Session session, MessageConsumer consumer) throws JMSException
70 {
71 Message message = consumer.receive(TIMEOUT);
72 assertNotNull(message);
73 assertTrue(TextMessage.class.isAssignableFrom(message.getClass()));
74 assertEquals(((TextMessage) message).getText(), DEFAULT_OUTPUT_MESSAGE);
75 return message;
76 }
77
78 };
79
80
81 public void send(Scenario scenario) throws Exception
82 {
83 TopicConnection connection = null;
84 try
85 {
86 TopicConnectionFactory factory = new ActiveMQConnectionFactory(scenario.getBrokerUrl());
87 connection = factory.createTopicConnection();
88 connection.start();
89 TopicSession session = null;
90 try
91 {
92 session = connection.createTopicSession(scenario.isTransacted(), scenario.getAcknowledge());
93 ActiveMQTopic destination = new ActiveMQTopic(scenario.getInputQueue());
94 TopicPublisher publisher = null;
95 try
96 {
97 publisher = session.createPublisher(destination);
98 publisher.setDeliveryMode(DeliveryMode.PERSISTENT);
99 scenario.send(session, publisher);
100 }
101 catch (Exception e)
102 {
103 throw e;
104 }
105 finally
106 {
107 if (publisher != null)
108 {
109 publisher.close();
110 }
111 }
112 }
113 catch (Exception e)
114 {
115 throw e;
116 }
117 finally
118 {
119 if (session != null)
120 {
121 session.close();
122 }
123 }
124 }
125 catch (Exception e)
126 {
127 throw e;
128 }
129 finally
130 {
131 if (connection != null)
132 {
133 connection.close();
134 }
135 }
136 }
137
138 }