KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > jms > JmsClientAcknowledgeTransaction


1 /*
2  * $Id: JmsClientAcknowledgeTransaction.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.jms;
12
13 import javax.jms.Connection JavaDoc;
14 import javax.jms.JMSException JavaDoc;
15 import javax.jms.Message JavaDoc;
16 import javax.jms.Session JavaDoc;
17
18 import org.mule.config.i18n.Messages;
19 import org.mule.transaction.AbstractSingleResourceTransaction;
20 import org.mule.transaction.IllegalTransactionStateException;
21 import org.mule.umo.TransactionException;
22
23 /**
24  * <code>JmsClientAcknowledgeTransaction</code> is a transaction implementation of
25  * performing a message acknowledgement. There is no notion of rollback with client
26  * acknowledgement, but this transaction can be useful for controlling how messages
27  * are consumed from a destination.
28  */

29 public class JmsClientAcknowledgeTransaction extends AbstractSingleResourceTransaction
30 {
31     private Message JavaDoc message;
32
33     /*
34      * (non-Javadoc)
35      *
36      * @see org.mule.transaction.AbstractSingleResourceTransaction#doBegin()
37      */

38     protected void doBegin() throws TransactionException
39     {
40         // nothing to do
41
}
42
43     /*
44      * (non-Javadoc)
45      *
46      * @see org.mule.transaction.AbstractSingleResourceTransaction#doCommit()
47      */

48     protected void doCommit() throws TransactionException
49     {
50         try
51         {
52             if (message == null)
53             {
54                 throw new IllegalTransactionStateException(new org.mule.config.i18n.Message("jms", 6));
55             }
56             message.acknowledge();
57         }
58         catch (JMSException JavaDoc e)
59         {
60             throw new IllegalTransactionStateException(new org.mule.config.i18n.Message(
61                 Messages.TX_COMMIT_FAILED), e);
62         }
63     }
64
65     /*
66      * (non-Javadoc)
67      *
68      * @see org.mule.transaction.AbstractSingleResourceTransaction#doRollback()
69      */

70     protected void doRollback() throws TransactionException
71     {
72         // If a message has been bound, rollback is forbidden
73
if (message != null)
74         {
75             throw new UnsupportedOperationException JavaDoc("Jms Client Acknowledge doesn't support rollback");
76         }
77     }
78
79     /*
80      * (non-Javadoc)
81      *
82      * @see org.mule.umo.UMOTransaction#bindResource(java.lang.Object,
83      * java.lang.Object)
84      */

85     public void bindResource(Object JavaDoc key, Object JavaDoc resource) throws TransactionException
86     {
87         if (key instanceof Message JavaDoc)
88         {
89             this.message = (Message JavaDoc)key;
90             return;
91         }
92         if (!(key instanceof Connection JavaDoc) || !(resource instanceof Session JavaDoc))
93         {
94             throw new IllegalTransactionStateException(new org.mule.config.i18n.Message(
95                 Messages.TX_CAN_ONLY_BIND_TO_X_TYPE_RESOURCES, "javax.jms.Connection/javax.jms.Session"));
96         }
97
98         Session JavaDoc session = (Session JavaDoc)resource;
99         try
100         {
101             if (session.getTransacted())
102             {
103                 throw new IllegalTransactionStateException(new org.mule.config.i18n.Message("jms", 5));
104             }
105         }
106         catch (JMSException JavaDoc e)
107         {
108             throw new IllegalTransactionStateException(new org.mule.config.i18n.Message(
109                 Messages.TX_CANT_READ_STATE), e);
110         }
111
112         super.bindResource(key, resource);
113     }
114 }
115
Popular Tags