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

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

Introduction

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

Prototype

public static void commitIfNecessary(Session session) throws JMSException 

Source Link

Document

Commit the Session if not within a JTA transaction.

Usage

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

/**
* Send the given JMS message./*w  w  w  . j av a2s . c  o  m*/
* @param session the JMS Session to operate on
* @param destination the JMS Destination to send to
* @param messageCreator callback to create a JMS Message
* @throws JMSException if thrown by JMS API methods
*/
protected void doSend(Session session, Destination destination, MessageCreator messageCreator)
        throws JMSException {

    Assert.notNull(messageCreator, "MessageCreator must not be null");
    MessageProducer producer = createProducer(session, destination);
    try {
        Message message = messageCreator.createMessage(session);
        if (logger.isDebugEnabled()) {
            logger.debug("Sending created message: " + message);
        }
        doSend(producer, message);
        // Check commit - avoid commit call within a JTA transaction.
        if (session.getTransacted() && isSessionLocallyTransacted(session)) {
            // Transacted session created by this template -> commit.
            JmsUtils.commitIfNecessary(session);
        }
    } finally {
        JmsUtils.closeMessageProducer(producer);
    }
}

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

/**
* Actually receive a JMS message./*  w  w  w. j  a v  a 2 s .c o m*/
* @param session the JMS Session to operate on
* @param consumer the JMS MessageConsumer to receive with
* @return the JMS Message received, or <code>null</code> if none
* @throws JMSException if thrown by JMS API methods
*/
protected Message doReceive(Session session, MessageConsumer consumer) throws JMSException {
    try {
        // Use transaction timeout (if available).
        long timeout = getReceiveTimeout();
        JmsResourceHolder resourceHolder = (JmsResourceHolder) TransactionSynchronizationManager
                .getResource(getConnectionFactory());
        if (resourceHolder != null && resourceHolder.hasTimeout()) {
            timeout = resourceHolder.getTimeToLiveInMillis();
        }
        Message message = doReceive(consumer, timeout);
        if (session.getTransacted()) {
            // Commit necessary - but avoid commit call within a JTA transaction.
            if (isSessionLocallyTransacted(session)) {
                // Transacted session created by this template -> commit.
                JmsUtils.commitIfNecessary(session);
            }
        } else if (isClientAcknowledge(session)) {
            // Manually acknowledge message, if any.
            if (message != null) {
                message.acknowledge();
            }
        }
        return message;
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
    }
}

From source file:org.springframework.cloud.stream.binder.jms.solace.SolaceQueueProvisioner.java

@Override
public Destinations provisionTopicAndConsumerGroup(String name, String... groups) {
    Destinations.Factory destinationsFactory = new Destinations.Factory();

    try {//from  w  w w  .  j  ava 2 s  .c o  m
        Topic topic = JCSMPFactory.onlyInstance().createTopic(name);
        JCSMPSession session = sessionFactory.build();
        Connection connection = connectionFactory.createConnection();
        javax.jms.Session jmsSession = connection.createSession(false, 1);

        // Using Durable... because non-durable Solace TopicEndpoints don't have names
        TopicEndpoint topicEndpoint = new DurableTopicEndpointImpl(name);
        session.provision(topicEndpoint, null, JCSMPSession.FLAG_IGNORE_ALREADY_EXISTS);
        destinationsFactory.withTopic(jmsSession.createTopic(name));

        if (ArrayUtils.isEmpty(groups)) {
            return destinationsFactory.build();
        }

        for (String group : groups) {
            destinationsFactory.addGroup(jmsSession.createQueue(group));
            doProvision(session, topic, group);
        }

        JmsUtils.commitIfNecessary(jmsSession);
        JmsUtils.closeSession(jmsSession);
        JmsUtils.closeConnection(connection);
    } catch (JCSMPErrorResponseException e) {
        if (JCSMPErrorResponseSubcodeEx.SUBSCRIPTION_ALREADY_PRESENT != e.getSubcodeEx()) {
            throw new RuntimeException(e);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return destinationsFactory.build();
}