KickJava   Java API By Example, From Geeks To Geeks.

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


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.Queue JavaDoc;
27 import javax.jms.QueueConnection JavaDoc;
28 import javax.jms.QueueRequestor JavaDoc;
29 import javax.jms.QueueSession JavaDoc;
30 import javax.jms.Session JavaDoc;
31 import javax.jms.TextMessage JavaDoc;
32
33 import junit.framework.TestCase;
34
35 import org.apache.activemq.ActiveMQConnectionFactory;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.springframework.context.support.AbstractApplicationContext;
39 import org.springframework.context.support.ClassPathXmlApplicationContext;
40
41 public class QueueBridgeTest extends TestCase implements MessageListener JavaDoc {
42     
43     protected static final Log log = LogFactory.getLog(QueueBridgeTest.class);
44
45     protected static final int MESSAGE_COUNT = 10;
46     protected AbstractApplicationContext context;
47     protected QueueConnection JavaDoc localConnection;
48     protected QueueConnection JavaDoc remoteConnection;
49     protected QueueRequestor JavaDoc requestor;
50     protected QueueSession JavaDoc requestServerSession;
51     protected MessageConsumer JavaDoc requestServerConsumer;
52     protected MessageProducer JavaDoc requestServerProducer;
53
54     protected void setUp() throws Exception JavaDoc {
55         super.setUp();
56         context = createApplicationContext();
57         
58         createConnections();
59         
60         requestServerSession = localConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
61         Queue JavaDoc theQueue = requestServerSession.createQueue(getClass().getName());
62         requestServerConsumer = requestServerSession.createConsumer(theQueue);
63         requestServerConsumer.setMessageListener(this);
64         requestServerProducer = requestServerSession.createProducer(null);
65         
66         QueueSession JavaDoc session = remoteConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
67         requestor = new QueueRequestor JavaDoc(session,theQueue);
68     }
69
70
71     protected void createConnections() throws JMSException JavaDoc {
72         ActiveMQConnectionFactory fac = (ActiveMQConnectionFactory) context.getBean("localFactory");
73         localConnection = fac.createQueueConnection();
74         localConnection.start();
75         
76         fac = (ActiveMQConnectionFactory) context.getBean("remoteFactory");
77         remoteConnection = fac.createQueueConnection();
78         remoteConnection.start();
79     }
80
81
82     protected AbstractApplicationContext createApplicationContext() {
83         return new ClassPathXmlApplicationContext("org/apache/activemq/network/jms/queue-config.xml");
84     }
85
86     
87     protected void tearDown() throws Exception JavaDoc {
88         localConnection.close();
89         super.tearDown();
90     }
91     
92     public void testQueueRequestorOverBridge() throws JMSException JavaDoc{
93         for (int i =0;i < MESSAGE_COUNT; i++){
94             TextMessage JavaDoc msg = requestServerSession.createTextMessage("test msg: " +i);
95             TextMessage JavaDoc result = (TextMessage JavaDoc) requestor.request(msg);
96             assertNotNull(result);
97             log.info(result.getText());
98         }
99     }
100     
101     public void onMessage(Message JavaDoc msg){
102         try{
103             TextMessage JavaDoc textMsg=(TextMessage JavaDoc) msg;
104             String JavaDoc payload="REPLY: "+textMsg.getText();
105             Destination JavaDoc replyTo;
106             replyTo=msg.getJMSReplyTo();
107             textMsg.clearBody();
108             textMsg.setText(payload);
109             requestServerProducer.send(replyTo,textMsg);
110         }catch(JMSException JavaDoc e){
111             // TODO Auto-generated catch block
112
e.printStackTrace();
113         }
114     }
115
116 }
117
Popular Tags