KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.net.URI JavaDoc;
21 import java.util.Date JavaDoc;
22
23 import javax.jms.Connection JavaDoc;
24 import javax.jms.ConnectionFactory JavaDoc;
25 import javax.jms.MessageConsumer JavaDoc;
26 import javax.jms.MessageProducer JavaDoc;
27 import javax.jms.Session JavaDoc;
28 import javax.jms.TextMessage JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30
31 import org.apache.activemq.ActiveMQConnectionFactory;
32 import org.apache.activemq.EmbeddedBrokerTestSupport;
33 import org.apache.activemq.broker.BrokerFactory;
34 import org.apache.activemq.broker.BrokerService;
35 import org.apache.activemq.util.MessageIdList;
36
37 /**
38  *
39  * @version $Revision: 1.1 $
40  */

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