| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.spring.spi; |
| 18 |
|
|
| 19 |
|
import org.apache.camel.Exchange; |
| 20 |
|
import org.apache.camel.RuntimeCamelException; |
| 21 |
|
import org.apache.camel.processor.DelegateProcessor; |
| 22 |
|
import org.apache.commons.logging.Log; |
| 23 |
|
import org.apache.commons.logging.LogFactory; |
| 24 |
|
|
| 25 |
|
import org.springframework.transaction.TransactionDefinition; |
| 26 |
|
import org.springframework.transaction.TransactionStatus; |
| 27 |
|
import org.springframework.transaction.support.TransactionCallbackWithoutResult; |
| 28 |
|
import org.springframework.transaction.support.TransactionTemplate; |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
0 |
public class TransactionInterceptor extends DelegateProcessor { |
| 34 |
0 |
private static final transient Log LOG = LogFactory.getLog(TransactionInterceptor.class); |
| 35 |
|
private final TransactionTemplate transactionTemplate; |
| 36 |
|
|
| 37 |
0 |
public TransactionInterceptor(TransactionTemplate transactionTemplate) { |
| 38 |
0 |
this.transactionTemplate = transactionTemplate; |
| 39 |
0 |
} |
| 40 |
|
|
| 41 |
|
public void process(final Exchange exchange) { |
| 42 |
0 |
LOG.info("transaction begin"); |
| 43 |
|
|
| 44 |
0 |
transactionTemplate.execute(new TransactionCallbackWithoutResult() { |
| 45 |
0 |
protected void doInTransactionWithoutResult(TransactionStatus status) { |
| 46 |
|
try { |
| 47 |
0 |
processNext(exchange); |
| 48 |
0 |
} catch (Exception e) { |
| 49 |
0 |
throw new RuntimeCamelException(e); |
| 50 |
0 |
} |
| 51 |
0 |
} |
| 52 |
|
}); |
| 53 |
|
|
| 54 |
0 |
LOG.info("transaction commit"); |
| 55 |
0 |
} |
| 56 |
|
|
| 57 |
|
@Override |
| 58 |
|
public String toString() { |
| 59 |
0 |
return "TransactionInterceptor:" + propagationBehaviorToString(transactionTemplate.getPropagationBehavior()) + "[" + getProcessor() + "]"; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
private String propagationBehaviorToString(int propagationBehavior) { |
| 63 |
|
String rc; |
| 64 |
0 |
switch (propagationBehavior) { |
| 65 |
|
case TransactionDefinition.PROPAGATION_MANDATORY: |
| 66 |
0 |
rc = "PROPAGATION_MANDATORY"; |
| 67 |
0 |
break; |
| 68 |
|
case TransactionDefinition.PROPAGATION_NESTED: |
| 69 |
0 |
rc = "PROPAGATION_NESTED"; |
| 70 |
0 |
break; |
| 71 |
|
case TransactionDefinition.PROPAGATION_NEVER: |
| 72 |
0 |
rc = "PROPAGATION_NEVER"; |
| 73 |
0 |
break; |
| 74 |
|
case TransactionDefinition.PROPAGATION_NOT_SUPPORTED: |
| 75 |
0 |
rc = "PROPAGATION_NOT_SUPPORTED"; |
| 76 |
0 |
break; |
| 77 |
|
case TransactionDefinition.PROPAGATION_REQUIRED: |
| 78 |
0 |
rc = "PROPAGATION_REQUIRED"; |
| 79 |
0 |
break; |
| 80 |
|
case TransactionDefinition.PROPAGATION_REQUIRES_NEW: |
| 81 |
0 |
rc = "PROPAGATION_REQUIRES_NEW"; |
| 82 |
0 |
break; |
| 83 |
|
case TransactionDefinition.PROPAGATION_SUPPORTS: |
| 84 |
0 |
rc = "PROPAGATION_SUPPORTS"; |
| 85 |
0 |
break; |
| 86 |
|
default: |
| 87 |
0 |
rc = "UNKOWN"; |
| 88 |
|
} |
| 89 |
0 |
return rc; |
| 90 |
|
} |
| 91 |
|
} |