KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > policy > DeadLetterTestSupport


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.broker.policy;
19
20 import org.apache.activemq.TestSupport;
21 import org.apache.activemq.broker.BrokerService;
22
23 import javax.jms.Connection JavaDoc;
24 import javax.jms.DeliveryMode JavaDoc;
25 import javax.jms.Destination JavaDoc;
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.Message JavaDoc;
28 import javax.jms.MessageConsumer JavaDoc;
29 import javax.jms.MessageProducer JavaDoc;
30 import javax.jms.Session JavaDoc;
31 import javax.jms.TextMessage JavaDoc;
32 import javax.jms.Topic JavaDoc;
33
34 /**
35  * @version $Revision: 426366 $
36  */

37 public abstract class DeadLetterTestSupport extends TestSupport {
38
39     protected int messageCount = 10;
40     protected long timeToLive = 0;
41     protected Connection JavaDoc connection;
42     protected Session JavaDoc session;
43     protected MessageConsumer JavaDoc consumer;
44     protected MessageProducer JavaDoc producer;
45     private Destination JavaDoc destination;
46     protected int deliveryMode = DeliveryMode.PERSISTENT;
47     protected boolean durableSubscriber = false;
48     protected Destination JavaDoc dlqDestination;
49     protected MessageConsumer JavaDoc dlqConsumer;
50     protected BrokerService broker;
51     protected boolean transactedMode = false;
52     protected int acknowledgeMode = Session.CLIENT_ACKNOWLEDGE;
53
54     protected void setUp() throws Exception JavaDoc {
55         super.setUp();
56         broker = createBroker();
57         broker.start();
58         connection = createConnection();
59         connection.setClientID(toString());
60
61         session = connection.createSession(transactedMode, acknowledgeMode);
62         connection.start();
63     }
64
65     protected void tearDown() throws Exception JavaDoc {
66         if (connection != null) {
67             connection.close();
68         }
69         if (broker != null) {
70             broker.stop();
71         }
72     }
73
74     protected abstract void doTest() throws Exception JavaDoc;
75
76     protected BrokerService createBroker() throws Exception JavaDoc {
77         BrokerService broker = new BrokerService();
78         broker.setPersistent(false);
79         return broker;
80     }
81
82     protected void makeConsumer() throws JMSException JavaDoc {
83         Destination JavaDoc destination = getDestination();
84         log.info("Consuming from: " + destination);
85         if (durableSubscriber) {
86             consumer = session.createDurableSubscriber((Topic JavaDoc) destination, destination.toString());
87         }
88         else {
89             consumer = session.createConsumer(destination);
90         }
91     }
92
93     protected void makeDlqConsumer() throws JMSException JavaDoc {
94         dlqDestination = createDlqDestination();
95
96         log.info("Consuming from dead letter on: " + dlqDestination);
97         dlqConsumer = session.createConsumer(dlqDestination);
98     }
99
100     protected void sendMessages() throws JMSException JavaDoc {
101         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
102         producer = session.createProducer(getDestination());
103         producer.setDeliveryMode(deliveryMode);
104         producer.setTimeToLive(timeToLive);
105
106         log.info("Sending " + messageCount + " messages to: " + getDestination());
107         for (int i = 0; i < messageCount; i++) {
108             Message JavaDoc message = createMessage(session, i);
109             producer.send(message);
110         }
111     }
112
113     protected TextMessage JavaDoc createMessage(Session JavaDoc session, int i) throws JMSException JavaDoc {
114         return session.createTextMessage(getMessageText(i));
115     }
116
117     protected String JavaDoc getMessageText(int i) {
118         return "message: " + i;
119     }
120
121     protected void assertMessage(Message JavaDoc message, int i) throws Exception JavaDoc {
122         log.info("Received message: " + message);
123         assertNotNull("No message received for index: " + i, message);
124         assertTrue("Should be a TextMessage not: " + message, message instanceof TextMessage JavaDoc);
125         TextMessage JavaDoc textMessage = (TextMessage JavaDoc) message;
126         assertEquals("text of message: " + i, getMessageText(i), textMessage .getText());
127     }
128     
129     protected abstract Destination JavaDoc createDlqDestination();
130
131     public void testTransientTopicMessage() throws Exception JavaDoc {
132         super.topic = true;
133         deliveryMode = DeliveryMode.NON_PERSISTENT;
134         durableSubscriber = true;
135         doTest();
136     }
137
138     public void testDurableTopicMessage() throws Exception JavaDoc {
139         super.topic = true;
140         deliveryMode = DeliveryMode.PERSISTENT;
141         durableSubscriber = true;
142         doTest();
143     }
144
145     public void testTransientQueueMessage() throws Exception JavaDoc {
146         super.topic = false;
147         deliveryMode = DeliveryMode.NON_PERSISTENT;
148         durableSubscriber = false;
149         doTest();
150     }
151
152     public void testDurableQueueMessage() throws Exception JavaDoc {
153         super.topic = false;
154         deliveryMode = DeliveryMode.PERSISTENT;
155         durableSubscriber = false;
156         doTest();
157     }
158
159     public Destination JavaDoc getDestination() {
160         if (destination == null) {
161             destination = createDestination();
162         }
163         return destination;
164     }
165 }
166
Popular Tags