99 */
1010package org .fusesource .stomp .activemq ;
1111
12- import junit .framework .TestCase ;
12+ import javax .jms .Connection ;
13+ import javax .jms .ConnectionFactory ;
14+ import javax .jms .Destination ;
15+ import javax .jms .JMSException ;
16+ import javax .jms .Message ;
17+ import javax .jms .MessageConsumer ;
18+ import javax .jms .MessageProducer ;
19+ import javax .jms .Session ;
20+ import javax .jms .TextMessage ;
21+
1322import org .apache .activemq .broker .BrokerService ;
1423import org .apache .activemq .broker .TransportConnector ;
1524import org .fusesource .stomp .jms .StompJmsConnectionFactory ;
16-
17- import javax .jms .*;
25+ import org .junit .After ;
26+ import org .junit .Assert ;
27+ import org .junit .Before ;
28+ import org .junit .Ignore ;
29+ import org .junit .Test ;
1830
1931/**
2032 * <p>
2133 * </p>
2234 *
2335 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
2436 */
25- public class ActiveMQJmsStompTest extends TestCase {
37+ public class ActiveMQJmsStompTest {
2638 BrokerService broker ;
2739 int port ;
2840
29- @ Override
30- protected void setUp () throws Exception {
31- super .setUp ();
41+ @ Before
42+ public void setUp () throws Exception {
3243 broker = new BrokerService ();
3344 broker .setPersistent (false );
3445 TransportConnector connector = broker .addConnector ("stomp://0.0.0.0:0" );
@@ -37,11 +48,10 @@ protected void setUp() throws Exception {
3748 port = connector .getConnectUri ().getPort ();
3849 }
3950
40- @ Override
41- protected void tearDown () throws Exception {
51+ @ After
52+ public void tearDown () throws Exception {
4253 broker .stop ();
4354 broker .waitUntilStopped ();
44- super .tearDown ();
4555 }
4656
4757 protected ConnectionFactory createConnectionFactory () throws Exception {
@@ -50,6 +60,7 @@ protected ConnectionFactory createConnectionFactory() throws Exception {
5060 return result ;
5161 }
5262
63+ @ Test
5364 public void testDurableSubs () throws Exception {
5465 Connection connection1 = createConnectionFactory ().createConnection ();
5566 connection1 .setClientID ("client1" );
@@ -113,9 +124,85 @@ public void testQueueSendReceiveSingleConnection() throws Exception {
113124 connection1 .close ();
114125 }
115126
116- private void assertTextMessageReceived (String expected , MessageConsumer sub ) throws JMSException {
127+ @ Test
128+ @ Ignore ("Test added as support for AMQ-4493 - chained request/replies over ActiveMQ. The test *will* fail as of 2013-08-21" )
129+ public void testChainedRequestReply () throws Exception {
130+ final String firstTopic = "mytopic" ;
131+ final String secondTopic = "secondtopic" ;
132+ // First block sending "1" and expecting "123" as reply.
133+ Connection connectionSource = createConnectionFactory ().createConnection ();
134+ connectionSource .start ();
135+ Session sessionSource = connectionSource .createSession (false , Session .CLIENT_ACKNOWLEDGE );
136+ MessageProducer producer = sessionSource .createProducer (sessionSource .createTopic (firstTopic ));
137+
138+ Destination replyToProducer = sessionSource .createTemporaryQueue ();
139+ MessageConsumer sourceReplySubscription = sessionSource .createConsumer (replyToProducer );
140+
141+ // Second block receiving "1" and using "mytopic" to append "3" after it's own "2"
142+ final Connection connectionIntermediate = createConnectionFactory ().createConnection ();
143+ connectionIntermediate .start ();
144+ final Session sessionIntermediate = connectionIntermediate .createSession (false , Session .CLIENT_ACKNOWLEDGE );
145+ final MessageConsumer consumerIntermediate = sessionIntermediate .createConsumer (sessionIntermediate .createTopic (firstTopic ));
146+ Thread intermediateThread = new Thread (new Runnable () {
147+
148+ public void run () {
149+ try {
150+ Message message = consumerIntermediate .receive ();
151+ System .out .println ("Producer -> Intermediate: " + message );
152+ Destination replyToIntermediate = sessionIntermediate .createTemporaryQueue ();
153+ MessageProducer intermediateProducer = sessionIntermediate .createProducer (sessionIntermediate .createTopic (secondTopic ));
154+ TextMessage secondMessage = sessionIntermediate .createTextMessage (((TextMessage ) message ).getText () + "2" );
155+ intermediateProducer .send (secondMessage );
156+
157+ MessageConsumer intermediateReplyConsumer = sessionIntermediate .createConsumer (replyToIntermediate );
158+ Message finalResponse = intermediateReplyConsumer .receive ();
159+ MessageProducer replyProducer = sessionIntermediate .createProducer (message .getJMSDestination ());
160+
161+ System .out .println ("Intermediate -> Producer: " + finalResponse );
162+ replyProducer .send (finalResponse );
163+ } catch (JMSException e ) {
164+ Assert .fail (e .getMessage ());
165+ }
166+ }});
167+ intermediateThread .setDaemon (true );
168+ intermediateThread .start ();
169+
170+ // Final block receiving "12" appending "3" and replying
171+ final Connection connectionFinal = createConnectionFactory ().createConnection ();
172+ connectionIntermediate .start ();
173+ final Session sessionFinal = connectionIntermediate .createSession (false , Session .CLIENT_ACKNOWLEDGE );
174+ final MessageConsumer consumerFinal = sessionIntermediate .createConsumer (sessionIntermediate .createTopic (secondTopic ));
175+ Thread finalThread = new Thread (new Runnable () {
176+
177+ public void run () {
178+ try {
179+ Message message = consumerFinal .receive ();
180+ System .out .println ("Intermediate -> Final: " + message );
181+ TextMessage finalResponse = sessionFinal .createTextMessage (((TextMessage ) message ).getText () + "3" );
182+
183+ MessageProducer replyProducer = sessionFinal .createProducer (message .getJMSDestination ());
184+ System .out .println ("Final -> Intermediate: " + finalResponse );
185+ replyProducer .send (finalResponse );
186+ } catch (JMSException e ) {
187+ Assert .fail (e .getMessage ());
188+ }
189+ }});
190+ finalThread .setDaemon (true );
191+ finalThread .start ();
192+
193+ TextMessage sourceTextMessage = sessionSource .createTextMessage ("1" );
194+ sourceTextMessage .setJMSReplyTo (replyToProducer );
195+ producer .send (sourceTextMessage );
196+
197+ assertTextMessageReceived ("123" , sourceReplySubscription );
198+ connectionSource .close ();
199+ connectionIntermediate .close ();
200+ connectionFinal .close ();
201+ }
202+
203+ private void assertTextMessageReceived (final String expected , final MessageConsumer sub ) throws JMSException {
117204 Message msg = sub .receive (1000 *5 );
118- assertNotNull ("A message was not received." , msg );
119- assertEquals (expected , ((TextMessage )msg ).getText ());
205+ Assert . assertNotNull ("A message was not received." , msg );
206+ Assert . assertEquals (expected , ((TextMessage )msg ).getText ());
120207 }
121208}
0 commit comments