KickJava   Java API By Example, From Geeks To Geeks.

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


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.Message JavaDoc;
22 import javax.jms.MessageConsumer JavaDoc;
23 import javax.jms.MessageListener 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.3 $
30  */

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

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

57     public void testAckedMessageAreConsumed() throws Exception JavaDoc {
58         connection.start();
59         Session JavaDoc session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
60         Queue JavaDoc queue = session.createQueue("test");
61         MessageProducer JavaDoc producer = session.createProducer(queue);
62         producer.send(session.createTextMessage("Hello"));
63
64         // Consume the message...
65
MessageConsumer JavaDoc consumer = session.createConsumer(queue);
66         consumer.setMessageListener(this);
67
68         Thread.sleep(10000);
69         
70         // Reset the session.
71
session.close();
72
73
74
75         session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
76         
77         // Attempt to Consume the message...
78
consumer = session.createConsumer(queue);
79         Message msg = consumer.receive(1000);
80         assertNull(msg);
81
82         session.close();
83     }
84     
85     /**
86      * Tests if unacknowleged messages are being redelivered when the consumer connects again.
87      *
88      * @throws javax.jms.JMSException
89      */

90     public void testUnAckedMessageAreNotConsumedOnSessionClose() throws Exception JavaDoc {
91         connection.start();
92         //don't aknowledge message on onMessage() call
93
dontAck=true;
94         Session JavaDoc session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
95         Queue JavaDoc queue = session.createQueue("test");
96         MessageProducer JavaDoc producer = session.createProducer(queue);
97         producer.send(session.createTextMessage("Hello"));
98
99         // Consume the message...
100
MessageConsumer JavaDoc consumer = session.createConsumer(queue);
101         consumer.setMessageListener(this);
102         // Don't ack the message.
103

104         // Reset the session. This should cause the Unacked message to be redelivered.
105
session.close();
106
107         Thread.sleep(10000);
108         session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
109         // Attempt to Consume the message...
110
consumer = session.createConsumer(queue);
111         Message msg = consumer.receive(2000);
112         assertNotNull(msg);
113         msg.acknowledge();
114         
115         session.close();
116     }
117
118
119    public void onMessage(Message message){
120
121         assertNotNull(message);
122        if(!dontAck) {
123            try {
124                 message.acknowledge();
125                }catch(Exception JavaDoc e){
126                  e.printStackTrace();
127               }
128
129        }
130
131    }
132 }
133
Popular Tags