Example usage for org.springframework.jms.connection ConnectionFactoryUtils doGetTransactionalSession

List of usage examples for org.springframework.jms.connection ConnectionFactoryUtils doGetTransactionalSession

Introduction

In this page you can find the example usage for org.springframework.jms.connection ConnectionFactoryUtils doGetTransactionalSession.

Prototype

@Nullable
public static Session doGetTransactionalSession(ConnectionFactory connectionFactory,
        ResourceFactory resourceFactory, boolean startConnection) throws JMSException 

Source Link

Document

Obtain a JMS Session that is synchronized with the current transaction, if any.

Usage

From source file:com.ccc.ccm.client.JMSTemplateAutowired.java

/**
* Execute the action specified by the given action object within a
* JMS Session. Generalized version of <code>execute(SessionCallback)</code>,
* allowing the JMS Connection to be started on the fly.
* <p>Use <code>execute(SessionCallback)</code> for the general case.
* Starting the JMS Connection is just necessary for receiving messages,
* which is preferably achieved through the <code>receive</code> methods.
* @param action callback object that exposes the Session
* @param startConnection whether to start the Connection
* @return the result object from working with the Session
* @throws JmsException if there is any problem
* @see #execute(SessionCallback)//ww w . j a  va  2 s.c o  m
* @see #receive
*/
public <T> T execute(SessionCallback<T> action, boolean startConnection) throws JmsException {
    Assert.notNull(action, "Callback object must not be null");
    Connection conToClose = null;
    Session sessionToClose = null;
    try {
        Session sessionToUse = ConnectionFactoryUtils.doGetTransactionalSession(getConnectionFactory(),
                this.transactionalResourceFactory, startConnection);
        if (sessionToUse == null) {
            conToClose = createConnection();
            sessionToClose = createSession(conToClose);
            if (startConnection) {
                conToClose.start();
            }
            sessionToUse = sessionToClose;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Executing callback on JMS Session: " + sessionToUse);
        }
        return action.doInJms(sessionToUse);
    } catch (JMSException ex) {
        throw convertJmsAccessException(ex);
    } finally {
        JmsUtils.closeSession(sessionToClose);
        ConnectionFactoryUtils.releaseConnection(conToClose, getConnectionFactory(), startConnection);
    }
}