KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > ee > jta > JTAJobRunShell


1
2 /*
3  * Copyright 2004-2005 OpenSymphony
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6  * use this file except in compliance with the License. You may obtain a copy
7  * of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations
15  * under the License.
16  *
17  */

18
19 /*
20  * Previously Copyright (c) 2001-2004 James House
21  */

22 package org.quartz.ee.jta;
23
24 import javax.transaction.Status JavaDoc;
25 import javax.transaction.SystemException JavaDoc;
26 import javax.transaction.UserTransaction JavaDoc;
27
28 import org.quartz.Scheduler;
29 import org.quartz.SchedulerException;
30 import org.quartz.core.JobRunShell;
31 import org.quartz.core.JobRunShellFactory;
32 import org.quartz.core.SchedulingContext;
33
34 /**
35  * <p>
36  * An extension of <code>{@link org.quartz.core.JobRunShell}</code> that
37  * begins an XA transaction before executing the Job, and commits (or
38  * rolls-back) the transaction after execution completes.
39  * </p>
40  *
41  * @see org.quartz.core.JobRunShell
42  *
43  * @author James House
44  */

45 public class JTAJobRunShell extends JobRunShell {
46
47     /*
48      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49      *
50      * Data members.
51      *
52      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53      */

54
55     private UserTransaction JavaDoc ut;
56
57     /*
58      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59      *
60      * Constructors.
61      *
62      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63      */

64
65     /**
66      * <p>
67      * Create a JTAJobRunShell instance with the given settings.
68      * </p>
69      */

70     public JTAJobRunShell(JobRunShellFactory jobRunShellFactory,
71             Scheduler scheduler, SchedulingContext schdCtxt) {
72         super(jobRunShellFactory, scheduler, schdCtxt);
73     }
74
75     /*
76      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
77      *
78      * Interface.
79      *
80      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81      */

82
83     protected void begin() throws SchedulerException {
84         // Don't get a new UserTransaction w/o making sure we cleaned up the old
85
// one. This is necessary because there are paths through JobRunShell.run()
86
// where begin() can be called multiple times w/o complete being called in
87
// between.
88
cleanupUserTransaction();
89         
90         boolean beganSuccessfully = false;
91         try {
92             getLog().debug("Looking up UserTransaction.");
93             ut = UserTransactionHelper.lookupUserTransaction();
94
95             getLog().debug("Beginning UserTransaction.");
96             ut.begin();
97             
98             beganSuccessfully = true;
99         } catch (SchedulerException se) {
100             throw se;
101         } catch (Exception JavaDoc nse) {
102
103             throw new SchedulerException(
104                     "JTAJobRunShell could not start UserTransaction.", nse);
105         } finally {
106             if (beganSuccessfully == false) {
107                 cleanupUserTransaction();
108             }
109         }
110     }
111
112     protected void complete(boolean successfulExecution)
113         throws SchedulerException {
114         if (ut == null) {
115             return;
116         }
117
118         try {
119             try {
120                 if (ut.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
121                     getLog().debug("UserTransaction marked for rollback only.");
122                     successfulExecution = false;
123                 }
124             } catch (SystemException JavaDoc e) {
125                 throw new SchedulerException(
126                         "JTAJobRunShell could not read UserTransaction status.", e);
127             }
128     
129             if (successfulExecution) {
130                 try {
131                     getLog().debug("Committing UserTransaction.");
132                     ut.commit();
133                 } catch (Exception JavaDoc nse) {
134                     throw new SchedulerException(
135                             "JTAJobRunShell could not commit UserTransaction.", nse);
136                 }
137             } else {
138                 try {
139                     getLog().debug("Rolling-back UserTransaction.");
140                     ut.rollback();
141                 } catch (Exception JavaDoc nse) {
142                     throw new SchedulerException(
143                             "JTAJobRunShell could not rollback UserTransaction.",
144                             nse);
145                 }
146             }
147         } finally {
148             cleanupUserTransaction();
149         }
150     }
151
152     /**
153      * Override passivate() to ensure we always cleanup the UserTransaction.
154      */

155     public void passivate() {
156         cleanupUserTransaction();
157         super.passivate();
158     }
159     
160     private void cleanupUserTransaction() {
161         if (ut != null) {
162             UserTransactionHelper.returnUserTransaction(ut);
163             ut = null;
164         }
165     }
166 }
Popular Tags