| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.ken.service.impl; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.StringUtils; |
| 19 | |
import org.apache.commons.lang.exception.ExceptionUtils; |
| 20 | |
import org.apache.log4j.Logger; |
| 21 | |
import org.apache.ojb.broker.OptimisticLockException; |
| 22 | |
import org.kuali.rice.ken.service.ProcessingResult; |
| 23 | |
import org.springframework.dao.DataAccessException; |
| 24 | |
import org.springframework.transaction.PlatformTransactionManager; |
| 25 | |
import org.springframework.transaction.TransactionException; |
| 26 | |
import org.springframework.transaction.TransactionStatus; |
| 27 | |
import org.springframework.transaction.UnexpectedRollbackException; |
| 28 | |
import org.springframework.transaction.support.TransactionCallback; |
| 29 | |
import org.springframework.transaction.support.TransactionTemplate; |
| 30 | |
|
| 31 | |
import java.sql.SQLException; |
| 32 | |
import java.sql.Timestamp; |
| 33 | |
import java.util.ArrayList; |
| 34 | |
import java.util.Collection; |
| 35 | |
import java.util.Iterator; |
| 36 | |
import java.util.List; |
| 37 | |
import java.util.concurrent.Callable; |
| 38 | |
import java.util.concurrent.ExecutorService; |
| 39 | |
import java.util.concurrent.Future; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public abstract class ConcurrentJob<T> { |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
private static final int ORACLE_00054 = 54; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
private static final int ORACLE_00060 = 60; |
| 54 | |
|
| 55 | |
|
| 56 | 0 | protected final Logger LOG = Logger.getLogger(getClass()); |
| 57 | |
|
| 58 | |
protected ExecutorService executor; |
| 59 | |
protected PlatformTransactionManager txManager; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | 0 | public ConcurrentJob(PlatformTransactionManager txManager, ExecutorService executor) { |
| 67 | 0 | this.txManager = txManager; |
| 68 | 0 | this.executor = executor; |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
protected TransactionTemplate createNewTransaction() { |
| 77 | 0 | TransactionTemplate tt = new TransactionTemplate(txManager); |
| 78 | 0 | tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW); |
| 79 | 0 | return tt; |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
protected abstract Collection<T> takeAvailableWorkItems(); |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
protected Collection<Collection<T>> groupWorkItems(Collection<T> workItems, ProcessingResult result) { |
| 98 | 0 | Collection<Collection<T>> groupedWorkItems = new ArrayList<Collection<T>>(); |
| 99 | |
|
| 100 | 0 | if (workItems != null) { |
| 101 | 0 | for (T workItem: workItems) { |
| 102 | 0 | Collection<T> c = new ArrayList<T>(1); |
| 103 | 0 | c.add(workItem); |
| 104 | 0 | groupedWorkItems.add(c); |
| 105 | 0 | } |
| 106 | |
} |
| 107 | 0 | return groupedWorkItems; |
| 108 | |
} |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
protected abstract Collection<?> processWorkItems(Collection<T> items); |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
protected abstract void unlockWorkItem(T item); |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public ProcessingResult run() { |
| 130 | 0 | LOG.debug("[" + new Timestamp(System.currentTimeMillis()).toString() + "] STARTING RUN"); |
| 131 | |
|
| 132 | 0 | final ProcessingResult result = new ProcessingResult(); |
| 133 | |
|
| 134 | |
|
| 135 | 0 | Collection<T> items = null; |
| 136 | |
try { |
| 137 | 0 | items = (Collection<T>) |
| 138 | 0 | createNewTransaction().execute(new TransactionCallback() { |
| 139 | |
public Object doInTransaction(TransactionStatus txStatus) { |
| 140 | 0 | return takeAvailableWorkItems(); |
| 141 | |
} |
| 142 | |
}); |
| 143 | 0 | } catch (DataAccessException dae) { |
| 144 | |
|
| 145 | |
|
| 146 | 0 | if (ExceptionUtils.indexOfType(dae, OptimisticLockException.class) != -1) { |
| 147 | |
|
| 148 | 0 | LOG.info("Contention while taking work items"); |
| 149 | |
} else { |
| 150 | |
|
| 151 | 0 | LOG.error("Error taking work items", dae); |
| 152 | 0 | Throwable t = dae.getMostSpecificCause(); |
| 153 | 0 | if (t != null && t instanceof SQLException) { |
| 154 | 0 | SQLException sqle = (SQLException) t; |
| 155 | 0 | if (sqle.getErrorCode() == ORACLE_00054 && StringUtils.contains(sqle.getMessage(), "resource busy")) { |
| 156 | |
|
| 157 | 0 | LOG.warn("Select for update lock contention encountered"); |
| 158 | 0 | } else if (sqle.getErrorCode() == ORACLE_00060 && StringUtils.contains(sqle.getMessage(), "deadlock detected")) { |
| 159 | |
|
| 160 | |
|
| 161 | 0 | LOG.error("Select for update deadlock encountered!"); |
| 162 | |
} |
| 163 | |
} |
| 164 | |
} |
| 165 | 0 | return result; |
| 166 | 0 | } catch (UnexpectedRollbackException ure) { |
| 167 | |
|
| 168 | 0 | LOG.error("UnexpectedRollbackException - possibly due to Mckoi"); |
| 169 | 0 | return result; |
| 170 | 0 | } catch (TransactionException te) { |
| 171 | 0 | LOG.error("Error occurred obtaining available work items", te); |
| 172 | 0 | result.addFailure("Error occurred obtaining available work items: " + te); |
| 173 | 0 | return result; |
| 174 | 0 | } |
| 175 | |
|
| 176 | 0 | Collection<Collection<T>> groupedWorkItems = groupWorkItems(items, result); |
| 177 | |
|
| 178 | |
|
| 179 | 0 | Iterator<Collection<T>> i = groupedWorkItems.iterator(); |
| 180 | 0 | List<Future> futures = new ArrayList<Future>(); |
| 181 | 0 | while(i.hasNext()) { |
| 182 | 0 | final Collection<T> workUnit= i.next(); |
| 183 | |
|
| 184 | 0 | LOG.info("Processing work unit: " + workUnit); |
| 185 | |
|
| 186 | |
|
| 187 | 0 | futures.add(executor.submit(new Callable() { |
| 188 | |
public Object call() throws Exception { |
| 189 | 0 | ProcessingResult result = new ProcessingResult(); |
| 190 | |
try { |
| 191 | 0 | Collection<?> successes = (Collection<Object>) |
| 192 | 0 | createNewTransaction().execute(new TransactionCallback() { |
| 193 | |
public Object doInTransaction(TransactionStatus txStatus) { |
| 194 | 0 | return processWorkItems(workUnit); |
| 195 | |
} |
| 196 | |
}); |
| 197 | 0 | result.addAllSuccesses(successes); |
| 198 | 0 | } catch (Exception e) { |
| 199 | 0 | LOG.error("Error occurred processing work unit " + workUnit, e); |
| 200 | 0 | for (final T workItem: workUnit) { |
| 201 | 0 | LOG.error("Error occurred processing work item " + workItem, e); |
| 202 | 0 | result.addFailure("Error occurred processing work item " + workItem + ": " + e); |
| 203 | 0 | unlockWorkItemAtomically(workItem); |
| 204 | |
} |
| 205 | 0 | } |
| 206 | 0 | return result; |
| 207 | |
} |
| 208 | |
})); |
| 209 | 0 | } |
| 210 | |
|
| 211 | |
|
| 212 | 0 | for (Future f: futures) { |
| 213 | |
try { |
| 214 | 0 | ProcessingResult workResult = (ProcessingResult) f.get(); |
| 215 | 0 | result.add(workResult); |
| 216 | 0 | } catch (Exception e) { |
| 217 | 0 | String message = "Error obtaining work result: " + e; |
| 218 | 0 | LOG.error(message, e); |
| 219 | 0 | result.addFailure(message); |
| 220 | 0 | } |
| 221 | |
} |
| 222 | |
|
| 223 | 0 | LOG.debug("[" + new Timestamp(System.currentTimeMillis()).toString() + "] FINISHED RUN - " + result); |
| 224 | |
|
| 225 | 0 | return result; |
| 226 | |
} |
| 227 | |
|
| 228 | |
protected void unlockWorkItemAtomically(final T workItem) { |
| 229 | |
try { |
| 230 | 0 | createNewTransaction().execute(new TransactionCallback() { |
| 231 | |
public Object doInTransaction(TransactionStatus txStatus) { |
| 232 | 0 | LOG.info("Unlocking failed work item: " + workItem); |
| 233 | 0 | unlockWorkItem(workItem); |
| 234 | 0 | return null; |
| 235 | |
} |
| 236 | |
}); |
| 237 | 0 | } catch (Exception e2) { |
| 238 | 0 | LOG.error("Error unlocking failed work item " + workItem, e2); |
| 239 | 0 | } |
| 240 | 0 | } |
| 241 | |
} |