Example usage for org.springframework.jms.core SessionCallback doInJms

List of usage examples for org.springframework.jms.core SessionCallback doInJms

Introduction

In this page you can find the example usage for org.springframework.jms.core SessionCallback doInJms.

Prototype

@Nullable
T doInJms(Session session) throws JMSException;

Source Link

Document

Execute any number of operations against the supplied JMS Session , possibly returning a result.

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)/*from  w  w  w . j  a  v  a2  s  . co  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);
    }
}

From source file:org.springframework.jms.core.JmsTemplate.java

/**
 * Execute the action specified by the given action object within a
 * JMS Session. Generalized version of execute(SessionCallback),
 * allowing to start the JMS Connection on the fly.
 * <p>Use execute(SessionCallback) for the general case. Starting
 * the JMS Connection is just necessary for receiving messages,
 * which is preferably achieve through the <code>receive</code> methods.
 * @param action callback object that exposes the session
 * @return the result object from working with the session
 * @throws JmsException if there is any problem
 * @see #execute(SessionCallback)/*ww w  .j a v  a2  s.  c o m*/
 * @see #receive
 */
public Object execute(SessionCallback action, boolean startConnection) throws JmsException {
    Connection con = null;
    Session session = null;
    try {
        Connection conToUse = null;
        Session sessionToUse = null;
        ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager
                .getResource(getConnectionFactory());
        if (conHolder != null) {
            conToUse = conHolder.getConnection();
            if (startConnection) {
                conToUse.start();
            }
            sessionToUse = conHolder.getSession();
        } else {
            //connection
            con = createConnection();
            if (startConnection) {
                con.start();
            }
            //session
            session = createSession(con);
            conToUse = con;
            sessionToUse = session;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Executing callback on JMS session [" + sessionToUse + "] from connection [" + conToUse
                    + "]");
        }
        //
        return action.doInJms(sessionToUse);
    } catch (JMSException ex) {
        throw convertJmsAccessException(ex);
    } finally {
        JmsUtils.closeSession(session);
        JmsUtils.closeConnection(con);
    }
}