KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > network > jms > TopicBridgeSpringTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.network.jms;
19
20 import javax.jms.Destination JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.Message JavaDoc;
23 import javax.jms.MessageConsumer JavaDoc;
24 import javax.jms.MessageListener JavaDoc;
25 import javax.jms.MessageProducer JavaDoc;
26 import javax.jms.Session JavaDoc;
27 import javax.jms.TextMessage JavaDoc;
28 import javax.jms.Topic JavaDoc;
29 import javax.jms.TopicConnection JavaDoc;
30 import javax.jms.TopicRequestor JavaDoc;
31 import javax.jms.TopicSession JavaDoc;
32 import junit.framework.TestCase;
33
34 import org.apache.activemq.ActiveMQConnectionFactory;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.springframework.context.support.AbstractApplicationContext;
38 import org.springframework.context.support.ClassPathXmlApplicationContext;
39
40 public class TopicBridgeSpringTest extends TestCase implements MessageListener JavaDoc {
41     
42     protected static final Log log = LogFactory.getLog(TopicBridgeSpringTest.class);
43
44     protected static final int MESSAGE_COUNT = 10;
45     protected AbstractApplicationContext context;
46     protected TopicConnection JavaDoc localConnection;
47     protected TopicConnection JavaDoc remoteConnection;
48     protected TopicRequestor JavaDoc requestor;
49     protected TopicSession JavaDoc requestServerSession;
50     protected MessageConsumer JavaDoc requestServerConsumer;
51     protected MessageProducer JavaDoc requestServerProducer;
52
53     protected void setUp() throws Exception JavaDoc {
54         
55         super.setUp();
56         context = createApplicationContext();
57         ActiveMQConnectionFactory fac = (ActiveMQConnectionFactory) context.getBean("localFactory");
58         localConnection = fac.createTopicConnection();
59         localConnection.start();
60         requestServerSession = localConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
61         Topic JavaDoc theTopic = requestServerSession.createTopic(getClass().getName());
62         requestServerConsumer = requestServerSession.createConsumer(theTopic);
63         requestServerConsumer.setMessageListener(this);
64         requestServerProducer = requestServerSession.createProducer(null);
65         
66         fac = (ActiveMQConnectionFactory) context.getBean("remoteFactory");
67         remoteConnection = fac.createTopicConnection();
68         remoteConnection.start();
69         TopicSession JavaDoc session = remoteConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
70         requestor = new TopicRequestor JavaDoc(session,theTopic);
71     }
72
73
74     protected AbstractApplicationContext createApplicationContext() {
75         return new ClassPathXmlApplicationContext("org/apache/activemq/network/jms/topic-spring.xml");
76     }
77
78     
79     protected void tearDown() throws Exception JavaDoc {
80         localConnection.close();
81         super.tearDown();
82     }
83     
84     public void testTopicRequestorOverBridge() throws JMSException JavaDoc{
85         for (int i =0;i < MESSAGE_COUNT; i++){
86             TextMessage JavaDoc msg = requestServerSession.createTextMessage("test msg: " +i);
87             log.info("Making request: " + msg);
88             TextMessage JavaDoc result = (TextMessage JavaDoc) requestor.request(msg);
89             assertNotNull(result);
90             log.info("Received result: " + result.getText());
91         }
92     }
93     
94     public void onMessage(Message JavaDoc msg){
95         try{
96             TextMessage JavaDoc textMsg=(TextMessage JavaDoc) msg;
97             String JavaDoc payload="REPLY: "+textMsg.getText();
98             Destination JavaDoc replyTo;
99             replyTo=msg.getJMSReplyTo();
100             textMsg.clearBody();
101             textMsg.setText(payload);
102             log.info("Sending response: " + textMsg);
103             requestServerProducer.send(replyTo,textMsg);
104         }catch(JMSException JavaDoc e){
105             // TODO Auto-generated catch block
106
e.printStackTrace();
107         }
108     }
109
110 }
111
Popular Tags