KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > test > rollback > DelegatingTransactionalMessageListener


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.rollback;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import javax.jms.Connection JavaDoc;
24 import javax.jms.Destination JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.Message JavaDoc;
27 import javax.jms.MessageConsumer JavaDoc;
28 import javax.jms.MessageListener JavaDoc;
29 import javax.jms.Session JavaDoc;
30
31
32 public class DelegatingTransactionalMessageListener implements MessageListener JavaDoc {
33     private static final transient Log log = LogFactory.getLog(DelegatingTransactionalMessageListener.class);
34
35     private final MessageListener JavaDoc underlyingListener;
36     private boolean transacted = true;
37     private int ackMode = Session.AUTO_ACKNOWLEDGE;
38     private Session JavaDoc session;
39
40     public DelegatingTransactionalMessageListener(MessageListener JavaDoc underlyingListener, Connection JavaDoc connection, Destination JavaDoc destination) {
41         this.underlyingListener = underlyingListener;
42
43         try {
44             session = connection.createSession(transacted, ackMode);
45             MessageConsumer JavaDoc consumer = session.createConsumer(destination);
46             consumer.setMessageListener(this);
47         }
48         catch (JMSException JavaDoc e) {
49             throw new IllegalStateException JavaDoc("Could not listen to " + destination, e);
50         }
51     }
52
53     public void onMessage(Message JavaDoc message) {
54         try {
55             underlyingListener.onMessage(message);
56             session.commit();
57         }
58         catch (Throwable JavaDoc e) {
59             rollback();
60         }
61     }
62
63     private void rollback() {
64         try {
65             session.rollback();
66         }
67         catch (JMSException JavaDoc e) {
68             log.error("Failed to rollback: " + e, e);
69         }
70     }
71
72     public Session JavaDoc getSession() {
73         return session;
74     }
75 }
76
Popular Tags