1 16 package org.quartz.plugins; 17 18 import javax.transaction.Status ; 19 import javax.transaction.UserTransaction ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.quartz.Scheduler; 24 import org.quartz.SchedulerException; 25 import org.quartz.ee.jta.UserTransactionHelper; 26 import org.quartz.spi.SchedulerPlugin; 27 28 44 public abstract class SchedulerPluginWithUserTransactionSupport implements 45 SchedulerPlugin { 46 47 private String name; 48 private Scheduler scheduler; 49 private final Log log = LogFactory.getLog(getClass()); 50 51 53 private boolean wrapInUserTransaction = false; 54 55 72 protected void start(UserTransaction userTransaction) { 73 } 74 75 92 protected void shutdown(UserTransaction userTransaction) { 93 } 94 95 98 protected Log getLog() { 99 return log; 100 } 101 102 105 protected String getName() { 106 return name; 107 } 108 109 112 protected Scheduler getScheduler() { 113 return scheduler; 114 } 115 116 public void initialize(String name, Scheduler scheduler) throws SchedulerException { 117 this.name = name; 118 this.scheduler = scheduler; 119 } 120 121 125 public boolean getWrapInUserTransaction() { 126 return wrapInUserTransaction; 127 } 128 129 133 public void setWrapInUserTransaction(boolean wrapInUserTransaction) { 134 this.wrapInUserTransaction = wrapInUserTransaction; 135 } 136 137 141 public void start() { 142 UserTransaction userTransaction = startUserTransaction(); 143 try { 144 start(userTransaction); 145 } finally { 146 resolveUserTransaction(userTransaction); 147 } 148 } 149 150 154 public void shutdown() { 155 UserTransaction userTransaction = startUserTransaction(); 156 try { 157 shutdown(userTransaction); 158 } finally { 159 resolveUserTransaction(userTransaction); 160 } 161 } 162 163 168 private UserTransaction startUserTransaction() { 169 if (wrapInUserTransaction == false) { 170 return null; 171 } 172 173 UserTransaction userTransaction = null; 174 try { 175 userTransaction = UserTransactionHelper.lookupUserTransaction(); 176 userTransaction.begin(); 177 } catch (Throwable t) { 178 UserTransactionHelper.returnUserTransaction(userTransaction); 179 userTransaction = null; 180 getLog().error("Failed to start UserTransaction for plugin: " + getName(), t); 181 } 182 183 return userTransaction; 184 } 185 186 190 private void resolveUserTransaction(UserTransaction userTransaction) { 191 if (userTransaction != null) { 192 try { 193 if (userTransaction.getStatus() == Status.STATUS_MARKED_ROLLBACK) { 194 userTransaction.rollback(); 195 } else { 196 userTransaction.commit(); 197 } 198 } catch (Throwable t) { 199 getLog().error("Failed to resolve UserTransaction for plugin: " + getName(), t); 200 } finally { 201 UserTransactionHelper.returnUserTransaction(userTransaction); 202 } 203 } 204 } 205 } 206
| Popular Tags
|