Search in sources :

Example 1 with SessionTransactionSynchronization

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;
}
Also used : InOnlyMessageHandler(org.apache.camel.component.sjms.consumer.InOnlyMessageHandler) SessionTransactionSynchronization(org.apache.camel.component.sjms.tx.SessionTransactionSynchronization) InOutMessageHandler(org.apache.camel.component.sjms.consumer.InOutMessageHandler) BatchTransactionCommitStrategy(org.apache.camel.component.sjms.tx.BatchTransactionCommitStrategy) DefaultTransactionCommitStrategy(org.apache.camel.component.sjms.tx.DefaultTransactionCommitStrategy) DefaultTransactionCommitStrategy(org.apache.camel.component.sjms.tx.DefaultTransactionCommitStrategy) BatchTransactionCommitStrategy(org.apache.camel.component.sjms.tx.BatchTransactionCommitStrategy) SessionBatchTransactionSynchronization(org.apache.camel.component.sjms.tx.SessionBatchTransactionSynchronization) SessionTransactionSynchronization(org.apache.camel.component.sjms.tx.SessionTransactionSynchronization) Synchronization(org.apache.camel.spi.Synchronization) TimedTaskManager(org.apache.camel.component.sjms.taskmanager.TimedTaskManager) SessionBatchTransactionSynchronization(org.apache.camel.component.sjms.tx.SessionBatchTransactionSynchronization) AbstractMessageHandler(org.apache.camel.component.sjms.consumer.AbstractMessageHandler)

Example 2 with SessionTransactionSynchronization

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();
}
Also used : SessionTransactionSynchronization(org.apache.camel.component.sjms.tx.SessionTransactionSynchronization) Session(javax.jms.Session)

Aggregations

SessionTransactionSynchronization (org.apache.camel.component.sjms.tx.SessionTransactionSynchronization)2 Session (javax.jms.Session)1 AbstractMessageHandler (org.apache.camel.component.sjms.consumer.AbstractMessageHandler)1 InOnlyMessageHandler (org.apache.camel.component.sjms.consumer.InOnlyMessageHandler)1 InOutMessageHandler (org.apache.camel.component.sjms.consumer.InOutMessageHandler)1 TimedTaskManager (org.apache.camel.component.sjms.taskmanager.TimedTaskManager)1 BatchTransactionCommitStrategy (org.apache.camel.component.sjms.tx.BatchTransactionCommitStrategy)1 DefaultTransactionCommitStrategy (org.apache.camel.component.sjms.tx.DefaultTransactionCommitStrategy)1 SessionBatchTransactionSynchronization (org.apache.camel.component.sjms.tx.SessionBatchTransactionSynchronization)1 Synchronization (org.apache.camel.spi.Synchronization)1