Example usage for org.springframework.jms.support JmsUtils closeSession

List of usage examples for org.springframework.jms.support JmsUtils closeSession

Introduction

In this page you can find the example usage for org.springframework.jms.support JmsUtils closeSession.

Prototype

public static void closeSession(@Nullable Session session) 

Source Link

Document

Close the given JMS Session and ignore any thrown exception.

Usage

From source file:org.springframework.integration.jms.JmsOutboundGateway.java

private javax.jms.Message sendAndReceiveWithoutContainer(Message<?> requestMessage) throws JMSException {
    Connection connection = this.createConnection(); // NOSONAR - closed in ConnectionFactoryUtils.
    Session session = null;/* ww  w . jav  a2 s  .c o  m*/
    Destination replyTo = null;
    try {
        session = this.createSession(connection);

        // convert to JMS Message
        Object objectToSend = requestMessage;
        if (this.extractRequestPayload) {
            objectToSend = requestMessage.getPayload();
        }
        javax.jms.Message jmsRequest = this.messageConverter.toMessage(objectToSend, session);

        // map headers
        this.headerMapper.fromHeaders(requestMessage.getHeaders(), jmsRequest);

        replyTo = this.determineReplyDestination(requestMessage, session);
        jmsRequest.setJMSReplyTo(replyTo);
        connection.start();
        if (logger.isDebugEnabled()) {
            logger.debug("ReplyTo: " + replyTo);
        }

        Integer priority = new IntegrationMessageHeaderAccessor(requestMessage).getPriority();
        if (priority == null) {
            priority = this.priority;
        }
        javax.jms.Message replyMessage = null;
        Destination requestDestination = this.determineRequestDestination(requestMessage, session);
        if (this.correlationKey != null) {
            replyMessage = doSendAndReceiveWithGeneratedCorrelationId(requestDestination, jmsRequest, replyTo,
                    session, priority);
        } else if (replyTo instanceof TemporaryQueue || replyTo instanceof TemporaryTopic) {
            replyMessage = doSendAndReceiveWithTemporaryReplyToDestination(requestDestination, jmsRequest,
                    replyTo, session, priority);
        } else {
            replyMessage = doSendAndReceiveWithMessageIdCorrelation(requestDestination, jmsRequest, replyTo,
                    session, priority);
        }
        return replyMessage;
    } finally {
        JmsUtils.closeSession(session);
        this.deleteDestinationIfTemporary(replyTo);
        ConnectionFactoryUtils.releaseConnection(connection, this.connectionFactory, true);
    }
}

From source file:org.springframework.integration.jms.JmsOutboundGateway.java

private javax.jms.Message retryableReceiveReply(Session session, Destination replyTo, String messageSelector)
        throws JMSException {
    Connection consumerConnection = null; //NOSONAR
    Session consumerSession = session;//from   w w w .j a v  a2  s  .  c om
    MessageConsumer messageConsumer = null;
    JMSException exception = null;
    boolean isTemporaryReplyTo = replyTo instanceof TemporaryQueue || replyTo instanceof TemporaryTopic;
    long replyTimeout = isTemporaryReplyTo ? Long.MIN_VALUE
            : this.receiveTimeout < 0 ? Long.MAX_VALUE : System.currentTimeMillis() + this.receiveTimeout;
    try {
        do {
            try {
                messageConsumer = consumerSession.createConsumer(replyTo, messageSelector);
                javax.jms.Message reply = receiveReplyMessage(messageConsumer);
                if (reply == null) {
                    if (replyTimeout > System.currentTimeMillis()) {
                        throw new JMSException("Consumer closed before timeout");
                    }
                }
                return reply;
            } catch (JMSException e) {
                exception = e;
                if (logger.isDebugEnabled()) {
                    logger.debug("Connection lost waiting for reply, retrying: " + e.getMessage());
                }
                do {
                    try {
                        consumerConnection = createConnection();
                        consumerSession = createSession(consumerConnection);
                        break;
                    } catch (JMSException ee) {
                        exception = ee;
                        if (logger.isDebugEnabled()) {
                            logger.debug("Could not reconnect, retrying: " + ee.getMessage());
                        }
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e1) {
                            Thread.currentThread().interrupt();
                            return null;
                        }
                    }
                } while (replyTimeout > System.currentTimeMillis());
            }
        } while (replyTimeout > System.currentTimeMillis());
        if (isTemporaryReplyTo) {
            return null;
        } else {
            throw exception;
        }
    } finally {
        if (consumerSession != session) {
            JmsUtils.closeSession(consumerSession);
            JmsUtils.closeConnection(consumerConnection);
        }
        JmsUtils.closeMessageConsumer(messageConsumer);
    }
}

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  av  a  2  s  .  co 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);
    }
}