KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > test > retroactive > RetroactiveConsumerWithMessageQueryTest


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.test.retroactive;
19
20 import org.apache.activemq.ActiveMQConnectionFactory;
21 import org.apache.activemq.EmbeddedBrokerTestSupport;
22 import org.apache.activemq.broker.BrokerFactory;
23 import org.apache.activemq.broker.BrokerService;
24 import org.apache.activemq.util.MessageIdList;
25 import org.apache.activemq.xbean.BrokerFactoryBean;
26 import org.springframework.core.io.ClassPathResource;
27
28 import javax.jms.Connection JavaDoc;
29 import javax.jms.ConnectionFactory 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
35 import java.net.URI JavaDoc;
36 import java.util.Date JavaDoc;
37
38 /**
39  *
40  * @version $Revision: 426366 $
41  */

42 public class RetroactiveConsumerWithMessageQueryTest extends EmbeddedBrokerTestSupport {
43     protected int messageCount = 20;
44     protected Connection JavaDoc connection;
45     protected Session JavaDoc session;
46
47     public void testConsumeAndReceiveInitialQueryBeforeUpdates() throws Exception JavaDoc {
48
49         // lets some messages
50
connection = createConnection();
51         session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
52         connection.start();
53
54         MessageConsumer JavaDoc consumer = session.createConsumer(destination);
55         MessageIdList listener = new MessageIdList();
56         listener.setVerbose(true);
57         consumer.setMessageListener(listener);
58
59         MessageProducer JavaDoc producer = session.createProducer(destination);
60         int updateMessageCount = messageCount - DummyMessageQuery.messageCount;
61         for (int i = 0; i < updateMessageCount; i++) {
62             TextMessage JavaDoc message = session.createTextMessage("Update Message: " + i + " sent at: " + new Date JavaDoc());
63             producer.send(message);
64         }
65         producer.close();
66         log.info("Sent: " + updateMessageCount + " update messages");
67
68         listener.assertMessagesReceived(messageCount);
69     }
70
71     protected void setUp() throws Exception JavaDoc {
72         useTopic = true;
73         bindAddress = "vm://localhost";
74         super.setUp();
75     }
76
77     protected void tearDown() throws Exception JavaDoc {
78         if (session != null) {
79             session.close();
80             session = null;
81         }
82         if (connection != null) {
83             connection.close();
84         }
85         super.tearDown();
86     }
87
88     protected ConnectionFactory createConnectionFactory() throws Exception JavaDoc {
89         ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory(bindAddress);
90         answer.setUseRetroactiveConsumer(true);
91         return answer;
92     }
93
94     protected BrokerService createBroker() throws Exception JavaDoc {
95         String JavaDoc uri = getBrokerXml();
96         log.info("Loading broker configuration from the classpath with URI: " + uri);
97         return BrokerFactory.createBroker(new URI JavaDoc("xbean:"+uri));
98     }
99
100     protected void startBroker() throws Exception JavaDoc {
101         // broker already started by XBean
102
}
103
104     protected String JavaDoc getBrokerXml() {
105         return "org/apache/activemq/test/retroactive/activemq-message-query.xml";
106     }
107
108 }
109
Popular Tags