KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > virtual > CompositeQueueTest


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

17 package org.apache.activemq.broker.virtual;
18
19 import org.apache.activemq.EmbeddedBrokerTestSupport;
20 import org.apache.activemq.broker.BrokerService;
21 import org.apache.activemq.command.ActiveMQQueue;
22 import org.apache.activemq.command.ActiveMQTopic;
23 import org.apache.activemq.spring.ConsumerBean;
24 import org.apache.activemq.xbean.XBeanBrokerFactory;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import javax.jms.Connection JavaDoc;
29 import javax.jms.Destination JavaDoc;
30 import javax.jms.MessageConsumer JavaDoc;
31 import javax.jms.MessageProducer JavaDoc;
32 import javax.jms.Session JavaDoc;
33 import javax.jms.TextMessage JavaDoc;
34 import javax.jms.JMSException JavaDoc;
35
36 import java.net.URI JavaDoc;
37
38 /**
39  *
40  * @version $Revision: 490805 $
41  */

42 public class CompositeQueueTest extends EmbeddedBrokerTestSupport {
43
44     private static final Log log = LogFactory.getLog(CompositeQueueTest.class);
45     
46     private Connection JavaDoc connection;
47
48     protected int total = 10;
49
50
51     public void testVirtualTopicCreation() throws Exception JavaDoc {
52         if (connection == null) {
53             connection = createConnection();
54         }
55         connection.start();
56
57         ConsumerBean messageList1 = new ConsumerBean();
58         ConsumerBean messageList2 = new ConsumerBean();
59         messageList1.setVerbose(true);
60         messageList2.setVerbose(true);
61
62         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
63         
64         Destination JavaDoc producerDestination = getProducerDestination();
65         Destination JavaDoc destination1 = getConsumer1Dsetination();
66         Destination JavaDoc destination2 = getConsumer2Dsetination();
67         
68         log.info("Sending to: " + producerDestination);
69         log.info("Consuming from: " + destination1 + " and " + destination2);
70         
71         MessageConsumer JavaDoc c1 = session.createConsumer(destination1);
72         MessageConsumer JavaDoc c2 = session.createConsumer(destination2);
73
74         c1.setMessageListener(messageList1);
75         c2.setMessageListener(messageList2);
76
77         // create topic producer
78
MessageProducer JavaDoc producer = session.createProducer(producerDestination);
79         assertNotNull(producer);
80
81         for (int i = 0; i < total; i++) {
82             producer.send(createMessage(session, i));
83         }
84
85         assertMessagesArrived(messageList1, messageList2);
86     }
87
88     protected void assertMessagesArrived(ConsumerBean messageList1, ConsumerBean messageList2) {
89         messageList1.assertMessagesArrived(total);
90         messageList2.assertMessagesArrived(total);
91     }
92
93     protected TextMessage JavaDoc createMessage(Session JavaDoc session, int i) throws JMSException JavaDoc {
94         TextMessage JavaDoc textMessage = session.createTextMessage("message: " + i);
95         if (i % 2 == 1) {
96             textMessage.setStringProperty("odd", "yes");
97         }
98         textMessage.setIntProperty("i", i);
99         return textMessage;
100     }
101
102     protected Destination JavaDoc getConsumer1Dsetination() {
103         return new ActiveMQQueue("FOO");
104     }
105
106     protected Destination JavaDoc getConsumer2Dsetination() {
107         return new ActiveMQTopic("BAR");
108     }
109
110     protected Destination JavaDoc getProducerDestination() {
111         return new ActiveMQQueue("MY.QUEUE");
112     }
113
114     protected void tearDown() throws Exception JavaDoc {
115         if (connection != null) {
116             connection.close();
117         }
118         super.tearDown();
119     }
120
121     protected BrokerService createBroker() throws Exception JavaDoc {
122         XBeanBrokerFactory factory = new XBeanBrokerFactory();
123         BrokerService answer = factory.createBroker(new URI JavaDoc(getBrokerConfigUri()));
124         return answer;
125     }
126
127     protected String JavaDoc getBrokerConfigUri() {
128         return "org/apache/activemq/broker/virtual/composite-queue.xml";
129     }
130 }
131
Popular Tags