KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > JmsTopicRedeliverTest


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;
19
20 import javax.jms.Connection JavaDoc;
21 import javax.jms.Destination JavaDoc;
22 import javax.jms.JMSException JavaDoc;
23 import javax.jms.Message JavaDoc;
24 import javax.jms.MessageConsumer 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
30 /**
31  * @version $Revision: 1.4 $
32  */

33 public class JmsTopicRedeliverTest extends TestSupport {
34     
35     private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
36             .getLog(JmsTopicRedeliverTest.class);
37    
38     
39     protected Connection JavaDoc connection;
40     protected Session JavaDoc session;
41     protected Session JavaDoc consumeSession;
42     protected MessageConsumer JavaDoc consumer;
43     protected MessageProducer JavaDoc producer;
44     protected Destination JavaDoc consumerDestination;
45     protected Destination JavaDoc producerDestination;
46     protected boolean topic = true;
47     protected boolean durable = false;
48     protected boolean verbose = false;
49     protected long initRedeliveryDelay = 0;
50
51     protected void setUp() throws Exception JavaDoc {
52         super.setUp();
53
54         connectionFactory = createConnectionFactory();
55         connection = createConnection();
56         initRedeliveryDelay = ((ActiveMQConnection)connection).getRedeliveryPolicy().getInitialRedeliveryDelay();
57
58         if (durable) {
59             connection.setClientID(getClass().getName());
60         }
61
62         log.info("Created connection: " + connection);
63
64         session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
65         consumeSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
66
67         log.info("Created session: " + session);
68         log.info("Created consumeSession: " + consumeSession);
69         producer = session.createProducer(null);
70         //producer.setDeliveryMode(deliveryMode);
71

72         log.info("Created producer: " + producer);
73
74         if (topic) {
75             consumerDestination = session.createTopic(getConsumerSubject());
76             producerDestination = session.createTopic(getProducerSubject());
77         } else {
78             consumerDestination = session.createQueue(getConsumerSubject());
79             producerDestination = session.createQueue(getProducerSubject());
80         }
81
82         log.info("Created consumer destination: " + consumerDestination + " of type: " + consumerDestination.getClass());
83         log.info("Created producer destination: " + producerDestination + " of type: " + producerDestination.getClass());
84         consumer = createConsumer();
85         connection.start();
86
87         log.info("Created connection: " + connection);
88     }
89
90     
91     protected void tearDown() throws Exception JavaDoc {
92         if (connection != null) {
93             connection.close();
94         }
95         super.tearDown();
96     }
97
98
99     /**
100      * Returns the consumer subject.
101      *
102      * @return String - consumer subject
103      * @see org.apache.activemq.test.TestSupport#getConsumerSubject()
104      */

105     protected String JavaDoc getConsumerSubject() {
106         return "TEST";
107     }
108
109     /**
110      * Returns the producer subject.
111      *
112      * @return String - producer subject
113      * @see org.apache.activemq.test.TestSupport#getProducerSubject()
114      */

115     protected String JavaDoc getProducerSubject() {
116         return "TEST";
117     }
118
119     /**
120      * Sends and consumes the messages.
121      *
122      * @throws Exception
123      */

124     public void testRecover() throws Exception JavaDoc {
125         String JavaDoc text = "TEST";
126         Message sendMessage = session.createTextMessage(text);
127
128         if (verbose) {
129             log.info("About to send a message: " + sendMessage + " with text: " + text);
130         }
131         producer.send(producerDestination, sendMessage);
132
133         //receive but don't acknowledge
134
Message unackMessage = consumer.receive(initRedeliveryDelay + 1000);
135         assertNotNull(unackMessage);
136         String JavaDoc unackId = unackMessage.getJMSMessageID();
137         assertEquals(((TextMessage JavaDoc) unackMessage).getText(), text);
138         assertFalse(unackMessage.getJMSRedelivered());
139         //assertEquals(unackMessage.getIntProperty("JMSXDeliveryCount"),1);
140

141         //receive then acknowledge
142
consumeSession.recover();
143         Message ackMessage = consumer.receive(initRedeliveryDelay + 1000);
144         assertNotNull(ackMessage);
145         ackMessage.acknowledge();
146         String JavaDoc ackId = ackMessage.getJMSMessageID();
147         assertEquals(((TextMessage JavaDoc) ackMessage).getText(), text);
148         assertTrue(ackMessage.getJMSRedelivered());
149         //assertEquals(ackMessage.getIntProperty("JMSXDeliveryCount"),2);
150
assertEquals(unackId, ackId);
151         consumeSession.recover();
152         assertNull(consumer.receiveNoWait());
153     }
154
155     protected MessageConsumer JavaDoc createConsumer() throws JMSException JavaDoc {
156         if (durable) {
157             log.info("Creating durable consumer");
158             return consumeSession.createDurableSubscriber((Topic JavaDoc) consumerDestination, getName());
159         }
160         return consumeSession.createConsumer(consumerDestination);
161     }
162
163 }
164
Popular Tags