Search in sources :

Example 21 with Session

use of javax.jms.Session in project spring-framework by spring-projects.

the class SimpleMessageListenerContainer method doShutdown.

/**
	 * Destroy the registered JMS Sessions and associated MessageConsumers.
	 */
@Override
protected void doShutdown() throws JMSException {
    synchronized (this.consumersMonitor) {
        if (this.consumers != null) {
            logger.debug("Closing JMS MessageConsumers");
            for (MessageConsumer consumer : this.consumers) {
                JmsUtils.closeMessageConsumer(consumer);
            }
            logger.debug("Closing JMS Sessions");
            for (Session session : this.sessions) {
                JmsUtils.closeSession(session);
            }
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Session(javax.jms.Session)

Example 22 with Session

use of javax.jms.Session in project spring-framework by spring-projects.

the class ConnectionFactoryUtils method doGetTransactionalSession.

/**
	 * Obtain a JMS Session that is synchronized with the current transaction, if any.
	 * @param connectionFactory the JMS ConnectionFactory to bind for
	 * (used as TransactionSynchronizationManager key)
	 * @param resourceFactory the ResourceFactory to use for extracting or creating
	 * JMS resources
	 * @param startConnection whether the underlying JMS Connection approach should be
	 * started in order to allow for receiving messages. Note that a reused Connection
	 * may already have been started before, even if this flag is {@code false}.
	 * @return the transactional Session, or {@code null} if none found
	 * @throws JMSException in case of JMS failure
	 */
public static Session doGetTransactionalSession(ConnectionFactory connectionFactory, ResourceFactory resourceFactory, boolean startConnection) throws JMSException {
    Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
    Assert.notNull(resourceFactory, "ResourceFactory must not be null");
    JmsResourceHolder resourceHolder = (JmsResourceHolder) TransactionSynchronizationManager.getResource(connectionFactory);
    if (resourceHolder != null) {
        Session session = resourceFactory.getSession(resourceHolder);
        if (session != null) {
            if (startConnection) {
                Connection con = resourceFactory.getConnection(resourceHolder);
                if (con != null) {
                    con.start();
                }
            }
            return session;
        }
        if (resourceHolder.isFrozen()) {
            return null;
        }
    }
    if (!TransactionSynchronizationManager.isSynchronizationActive()) {
        return null;
    }
    JmsResourceHolder resourceHolderToUse = resourceHolder;
    if (resourceHolderToUse == null) {
        resourceHolderToUse = new JmsResourceHolder(connectionFactory);
    }
    Connection con = resourceFactory.getConnection(resourceHolderToUse);
    Session session = null;
    try {
        boolean isExistingCon = (con != null);
        if (!isExistingCon) {
            con = resourceFactory.createConnection();
            resourceHolderToUse.addConnection(con);
        }
        session = resourceFactory.createSession(con);
        resourceHolderToUse.addSession(session, con);
        if (startConnection) {
            con.start();
        }
    } catch (JMSException ex) {
        if (session != null) {
            try {
                session.close();
            } catch (Throwable ex2) {
            // ignore
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (Throwable ex2) {
            // ignore
            }
        }
        throw ex;
    }
    if (resourceHolderToUse != resourceHolder) {
        TransactionSynchronizationManager.registerSynchronization(new JmsResourceSynchronization(resourceHolderToUse, connectionFactory, resourceFactory.isSynchedLocalTransactionAllowed()));
        resourceHolderToUse.setSynchronizedWithTransaction(true);
        TransactionSynchronizationManager.bindResource(connectionFactory, resourceHolderToUse);
    }
    return session;
}
Also used : Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) QueueConnection(javax.jms.QueueConnection) JMSException(javax.jms.JMSException) QueueSession(javax.jms.QueueSession) Session(javax.jms.Session) TopicSession(javax.jms.TopicSession)

Example 23 with Session

use of javax.jms.Session in project spring-framework by spring-projects.

the class JmsTransactionManager method doRollback.

@Override
protected void doRollback(DefaultTransactionStatus status) {
    JmsTransactionObject txObject = (JmsTransactionObject) status.getTransaction();
    Session session = txObject.getResourceHolder().getSession();
    try {
        if (status.isDebug()) {
            logger.debug("Rolling back JMS transaction on Session [" + session + "]");
        }
        session.rollback();
    } catch (JMSException ex) {
        throw new TransactionSystemException("Could not roll back JMS transaction", ex);
    }
}
Also used : JMSException(javax.jms.JMSException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) Session(javax.jms.Session)

Example 24 with Session

use of javax.jms.Session in project spring-framework by spring-projects.

the class JmsTransactionManager method doBegin.

@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
    if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
        throw new InvalidIsolationLevelException("JMS does not support an isolation level concept");
    }
    JmsTransactionObject txObject = (JmsTransactionObject) transaction;
    Connection con = null;
    Session session = null;
    try {
        con = createConnection();
        session = createSession(con);
        if (logger.isDebugEnabled()) {
            logger.debug("Created JMS transaction on Session [" + session + "] from Connection [" + con + "]");
        }
        txObject.setResourceHolder(new JmsResourceHolder(getConnectionFactory(), con, session));
        txObject.getResourceHolder().setSynchronizedWithTransaction(true);
        int timeout = determineTimeout(definition);
        if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
            txObject.getResourceHolder().setTimeoutInSeconds(timeout);
        }
        TransactionSynchronizationManager.bindResource(getConnectionFactory(), txObject.getResourceHolder());
    } catch (Throwable ex) {
        if (session != null) {
            try {
                session.close();
            } catch (Throwable ex2) {
            // ignore
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (Throwable ex2) {
            // ignore
            }
        }
        throw new CannotCreateTransactionException("Could not create JMS transaction", ex);
    }
}
Also used : CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) Connection(javax.jms.Connection) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) Session(javax.jms.Session)

Example 25 with Session

use of javax.jms.Session in project spring-framework by spring-projects.

the class JmsTemplate method executeLocal.

/**
	 * A variant of {@link #execute(SessionCallback, boolean)} that explicitly
	 * creates a non-transactional {@link Session}. The given {@link SessionCallback}
	 * does not participate in an existing transaction.
	 */
private <T> T executeLocal(SessionCallback<T> action, boolean startConnection) throws JmsException {
    Assert.notNull(action, "Callback object must not be null");
    Connection con = null;
    Session session = null;
    try {
        con = getConnectionFactory().createConnection();
        session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
        if (startConnection) {
            con.start();
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Executing callback on JMS Session: " + session);
        }
        return action.doInJms(session);
    } catch (JMSException ex) {
        throw convertJmsAccessException(ex);
    } finally {
        JmsUtils.closeSession(session);
        ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory(), startConnection);
    }
}
Also used : Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Aggregations

Session (javax.jms.Session)231 MessageProducer (javax.jms.MessageProducer)106 Test (org.junit.Test)98 Connection (javax.jms.Connection)88 Message (javax.jms.Message)73 TextMessage (javax.jms.TextMessage)69 JMSException (javax.jms.JMSException)58 MessageConsumer (javax.jms.MessageConsumer)53 Topic (javax.jms.Topic)36 ConnectionFactory (javax.jms.ConnectionFactory)31 Destination (javax.jms.Destination)27 Queue (javax.jms.Queue)26 ObjectMessage (javax.jms.ObjectMessage)24 StubTextMessage (org.springframework.jms.StubTextMessage)20 QueueSession (javax.jms.QueueSession)16 MessagingMessageListenerAdapter (org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter)16 MapMessage (javax.jms.MapMessage)12 MessageCreator (org.springframework.jms.core.MessageCreator)12 BytesMessage (javax.jms.BytesMessage)11 TopicSession (javax.jms.TopicSession)9