use of org.apache.camel.component.sjms.tx.SessionTransactionSynchronization in project camel by apache.
the class SjmsConsumer method createMessageHandler.
/**
* Helper factory method used to create a MessageListener based on the MEP
*
* @param session a session is only required if we are a transacted consumer
* @return the listener
*/
protected MessageListener createMessageHandler(Session session) {
TransactionCommitStrategy commitStrategy;
if (getTransactionCommitStrategy() != null) {
commitStrategy = getTransactionCommitStrategy();
} else if (getTransactionBatchCount() > 0) {
commitStrategy = new BatchTransactionCommitStrategy(getTransactionBatchCount());
} else {
commitStrategy = new DefaultTransactionCommitStrategy();
}
Synchronization synchronization;
if (commitStrategy instanceof BatchTransactionCommitStrategy) {
TimedTaskManager timedTaskManager = getEndpoint().getComponent().getTimedTaskManager();
synchronization = new SessionBatchTransactionSynchronization(timedTaskManager, session, commitStrategy, getTransactionBatchTimeout());
} else {
synchronization = new SessionTransactionSynchronization(session, commitStrategy);
}
AbstractMessageHandler messageHandler;
if (getEndpoint().getExchangePattern().equals(ExchangePattern.InOnly)) {
if (isTransacted()) {
messageHandler = new InOnlyMessageHandler(getEndpoint(), executor, synchronization);
} else {
messageHandler = new InOnlyMessageHandler(getEndpoint(), executor);
}
} else {
if (isTransacted()) {
messageHandler = new InOutMessageHandler(getEndpoint(), executor, synchronization);
} else {
messageHandler = new InOutMessageHandler(getEndpoint(), executor);
}
}
messageHandler.setSession(session);
messageHandler.setProcessor(getAsyncProcessor());
messageHandler.setSynchronous(isSynchronous());
messageHandler.setTransacted(isTransacted());
messageHandler.setSharedJMSSession(isSharedJMSSession());
messageHandler.setTopic(isTopic());
return messageHandler;
}
use of org.apache.camel.component.sjms.tx.SessionTransactionSynchronization in project camel by apache.
the class SjmsProducer method process.
@Override
public boolean process(final Exchange exchange, final AsyncCallback callback) {
if (log.isDebugEnabled()) {
log.debug("Processing Exchange.id:{}", exchange.getExchangeId());
}
try {
MessageProducerResources producer = null;
ReleaseProducerCallback releaseProducerCallback = null;
if (isEndpointTransacted() && isSharedJMSSession()) {
Session session = exchange.getIn().getHeader(SjmsConstants.JMS_SESSION, Session.class);
if (session != null && session.getTransacted()) {
// Join existing transacted session - Synchronization must have been added
// by the session initiator
producer = doCreateProducerModel(session);
releaseProducerCallback = new NOOPReleaseProducerCallback();
} else {
// Propagate JMS session and register Synchronization as an initiator
producer = getProducers().borrowObject();
releaseProducerCallback = new ReturnProducerCallback();
exchange.getIn().setHeader(SjmsConstants.JMS_SESSION, producer.getSession());
exchange.getUnitOfWork().addSynchronization(new SessionTransactionSynchronization(producer.getSession(), producer.getCommitStrategy()));
}
} else {
producer = getProducers().borrowObject();
releaseProducerCallback = new ReturnProducerCallback();
if (isEndpointTransacted()) {
exchange.getUnitOfWork().addSynchronization(new SessionTransactionSynchronization(producer.getSession(), producer.getCommitStrategy()));
}
}
if (producer == null) {
exchange.setException(new Exception("Unable to send message: connection not available"));
} else {
if (!isSynchronous()) {
if (log.isDebugEnabled()) {
log.debug(" Sending message asynchronously: {}", exchange.getIn().getBody());
}
final MessageProducerResources finalProducer = producer;
final ReleaseProducerCallback finalrpc = releaseProducerCallback;
getExecutor().execute(new Runnable() {
@Override
public void run() {
try {
sendMessage(exchange, callback, finalProducer, finalrpc);
} catch (Exception e) {
ObjectHelper.wrapRuntimeCamelException(e);
}
}
});
} else {
if (log.isDebugEnabled()) {
log.debug(" Sending message synchronously: {}", exchange.getIn().getBody());
}
sendMessage(exchange, callback, producer, releaseProducerCallback);
}
}
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("Processing Exchange.id:{}", exchange.getExchangeId() + " - FAILED");
}
if (log.isDebugEnabled()) {
log.trace("Exception: " + e.getLocalizedMessage(), e);
}
exchange.setException(e);
}
log.debug("Processing Exchange.id:{}", exchange.getExchangeId() + " - SUCCESS");
return isSynchronous();
}
Aggregations