KickJava   Java API By Example, From Geeks To Geeks.

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


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.JMSException JavaDoc;
22 import javax.jms.Message JavaDoc;
23 import javax.jms.MessageConsumer JavaDoc;
24 import javax.jms.MessageProducer JavaDoc;
25 import javax.jms.Queue JavaDoc;
26 import javax.jms.Session JavaDoc;
27
28 /**
29  * @version $Revision: 1.4 $
30  */

31 public class JmsClientAckTest extends TestSupport {
32
33     private Connection JavaDoc connection;
34
35     protected void setUp() throws Exception JavaDoc {
36         super.setUp();
37         connection = createConnection();
38     }
39
40     /**
41      * @see junit.framework.TestCase#tearDown()
42      */

43     protected void tearDown() throws Exception JavaDoc {
44         if (connection != null) {
45             connection.close();
46             connection = null;
47         }
48         super.tearDown();
49     }
50
51     /**
52      * Tests if acknowledged messages are being consumed.
53      *
54      * @throws JMSException
55      */

56     public void testAckedMessageAreConsumed() throws JMSException JavaDoc {
57         connection.start();
58         Session JavaDoc session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
59         Queue JavaDoc queue = session.createQueue(getQueueName());
60         MessageProducer JavaDoc producer = session.createProducer(queue);
61         producer.send(session.createTextMessage("Hello"));
62
63         // Consume the message...
64
MessageConsumer JavaDoc consumer = session.createConsumer(queue);
65         Message msg = consumer.receive(1000);
66         assertNotNull(msg);
67         msg.acknowledge();
68
69         // Reset the session.
70
session.close();
71         session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
72
73         // Attempt to Consume the message...
74
consumer = session.createConsumer(queue);
75         msg = consumer.receive(1000);
76         assertNull(msg);
77
78         session.close();
79     }
80
81     /**
82      * Tests if acknowledged messages are being consumed.
83      *
84      * @throws JMSException
85      */

86     public void testLastMessageAcked() throws JMSException JavaDoc {
87         connection.start();
88         Session JavaDoc session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
89         Queue JavaDoc queue = session.createQueue(getQueueName());
90         MessageProducer JavaDoc producer = session.createProducer(queue);
91         producer.send(session.createTextMessage("Hello"));
92         producer.send(session.createTextMessage("Hello2"));
93         producer.send(session.createTextMessage("Hello3"));
94
95         // Consume the message...
96
MessageConsumer JavaDoc consumer = session.createConsumer(queue);
97         Message msg = consumer.receive(1000);
98         assertNotNull(msg);
99         msg = consumer.receive(1000);
100         assertNotNull(msg);
101         msg = consumer.receive(1000);
102         assertNotNull(msg);
103         msg.acknowledge();
104
105         // Reset the session.
106
session.close();
107         session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
108
109         // Attempt to Consume the message...
110
consumer = session.createConsumer(queue);
111         msg = consumer.receive(1000);
112         assertNull(msg);
113
114         session.close();
115     }
116     
117     /**
118      * Tests if unacknowledged messages are being re-delivered when the consumer connects again.
119      *
120      * @throws JMSException
121      */

122     public void testUnAckedMessageAreNotConsumedOnSessionClose() throws JMSException JavaDoc {
123         connection.start();
124         Session JavaDoc session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
125         Queue JavaDoc queue = session.createQueue(getQueueName());
126         MessageProducer JavaDoc producer = session.createProducer(queue);
127         producer.send(session.createTextMessage("Hello"));
128
129         // Consume the message...
130
MessageConsumer JavaDoc consumer = session.createConsumer(queue);
131         Message msg = consumer.receive(1000);
132         assertNotNull(msg);
133         // Don't ack the message.
134

135         // Reset the session. This should cause the unacknowledged message to be re-delivered.
136
session.close();
137         session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
138                 
139         // Attempt to Consume the message...
140
consumer = session.createConsumer(queue);
141         msg = consumer.receive(2000);
142         assertNotNull(msg);
143         msg.acknowledge();
144         
145         session.close();
146     }
147
148     protected String JavaDoc getQueueName() {
149         return getClass().getName() + "." + getName();
150     }
151
152 }
153
Popular Tags