Java Code Examples for org.springframework.jms.support.JmsUtils#commitIfNecessary()

The following examples show how to use org.springframework.jms.support.JmsUtils#commitIfNecessary() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JmsTemplate.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Send the given JMS message.
 * @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);
	}
}
 
Example 2
Source File: JmsTemplate.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Send the given JMS message.
 * @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);
	}
}
 
Example 3
Source File: JmsTemplate.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Send the given JMS message.
 * @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);
	}
}
 
Example 4
Source File: AbstractMessageListenerContainer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Invoke the specified listener as Spring SessionAwareMessageListener,
 * exposing a new JMS Session (potentially with its own transaction)
 * to the listener if demanded.
 * @param listener the Spring SessionAwareMessageListener to invoke
 * @param session the JMS Session to operate on
 * @param message the received JMS Message
 * @throws JMSException if thrown by JMS API methods
 * @see SessionAwareMessageListener
 * @see #setExposeListenerSession
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void doInvokeListener(SessionAwareMessageListener listener, Session session, Message message)
		throws JMSException {

	Connection conToClose = null;
	Session sessionToClose = null;
	try {
		Session sessionToUse = session;
		if (!isExposeListenerSession()) {
			// We need to expose a separate Session.
			conToClose = createConnection();
			sessionToClose = createSession(conToClose);
			sessionToUse = sessionToClose;
		}
		// Actually invoke the message listener...
		listener.onMessage(message, sessionToUse);
		// Clean up specially exposed Session, if any.
		if (sessionToUse != session) {
			if (sessionToUse.getTransacted() && isSessionLocallyTransacted(sessionToUse)) {
				// Transacted session created by this container -> commit.
				JmsUtils.commitIfNecessary(sessionToUse);
			}
		}
	}
	finally {
		JmsUtils.closeSession(sessionToClose);
		JmsUtils.closeConnection(conToClose);
	}
}
 
Example 5
Source File: AbstractMessageListenerContainer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Perform a commit or message acknowledgement, as appropriate.
 * @param session the JMS Session to commit
 * @param message the Message to acknowledge
 * @throws javax.jms.JMSException in case of commit failure
 */
protected void commitIfNecessary(Session session, @Nullable Message message) throws JMSException {
	// Commit session or acknowledge message.
	if (session.getTransacted()) {
		// Commit necessary - but avoid commit call within a JTA transaction.
		if (isSessionLocallyTransacted(session)) {
			// Transacted session created by this container -> commit.
			JmsUtils.commitIfNecessary(session);
		}
	}
	else if (message != null && isClientAcknowledge(session)) {
		message.acknowledge();
	}
}
 
Example 6
Source File: AbstractMessageListenerContainer.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Invoke the specified listener as Spring SessionAwareMessageListener,
 * exposing a new JMS Session (potentially with its own transaction)
 * to the listener if demanded.
 * @param listener the Spring SessionAwareMessageListener to invoke
 * @param session the JMS Session to operate on
 * @param message the received JMS Message
 * @throws JMSException if thrown by JMS API methods
 * @see SessionAwareMessageListener
 * @see #setExposeListenerSession
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void doInvokeListener(SessionAwareMessageListener listener, Session session, Message message)
		throws JMSException {

	Connection conToClose = null;
	Session sessionToClose = null;
	try {
		Session sessionToUse = session;
		if (!isExposeListenerSession()) {
			// We need to expose a separate Session.
			conToClose = createConnection();
			sessionToClose = createSession(conToClose);
			sessionToUse = sessionToClose;
		}
		// Actually invoke the message listener...
		listener.onMessage(message, sessionToUse);
		// Clean up specially exposed Session, if any.
		if (sessionToUse != session) {
			if (sessionToUse.getTransacted() && isSessionLocallyTransacted(sessionToUse)) {
				// Transacted session created by this container -> commit.
				JmsUtils.commitIfNecessary(sessionToUse);
			}
		}
	}
	finally {
		JmsUtils.closeSession(sessionToClose);
		JmsUtils.closeConnection(conToClose);
	}
}
 
Example 7
Source File: AbstractMessageListenerContainer.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Perform a commit or message acknowledgement, as appropriate.
 * @param session the JMS Session to commit
 * @param message the Message to acknowledge
 * @throws javax.jms.JMSException in case of commit failure
 */
protected void commitIfNecessary(Session session, @Nullable Message message) throws JMSException {
	// Commit session or acknowledge message.
	if (session.getTransacted()) {
		// Commit necessary - but avoid commit call within a JTA transaction.
		if (isSessionLocallyTransacted(session)) {
			// Transacted session created by this container -> commit.
			JmsUtils.commitIfNecessary(session);
		}
	}
	else if (message != null && isClientAcknowledge(session)) {
		message.acknowledge();
	}
}
 
Example 8
Source File: AbstractMessageListenerContainer.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Invoke the specified listener as Spring SessionAwareMessageListener,
 * exposing a new JMS Session (potentially with its own transaction)
 * to the listener if demanded.
 * @param listener the Spring SessionAwareMessageListener to invoke
 * @param session the JMS Session to operate on
 * @param message the received JMS Message
 * @throws JMSException if thrown by JMS API methods
 * @see SessionAwareMessageListener
 * @see #setExposeListenerSession
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void doInvokeListener(SessionAwareMessageListener listener, Session session, Message message)
		throws JMSException {

	Connection conToClose = null;
	Session sessionToClose = null;
	try {
		Session sessionToUse = session;
		if (!isExposeListenerSession()) {
			// We need to expose a separate Session.
			conToClose = createConnection();
			sessionToClose = createSession(conToClose);
			sessionToUse = sessionToClose;
		}
		// Actually invoke the message listener...
		listener.onMessage(message, sessionToUse);
		// Clean up specially exposed Session, if any.
		if (sessionToUse != session) {
			if (sessionToUse.getTransacted() && isSessionLocallyTransacted(sessionToUse)) {
				// Transacted session created by this container -> commit.
				JmsUtils.commitIfNecessary(sessionToUse);
			}
		}
	}
	finally {
		JmsUtils.closeSession(sessionToClose);
		JmsUtils.closeConnection(conToClose);
	}
}
 
Example 9
Source File: AbstractMessageListenerContainer.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Perform a commit or message acknowledgement, as appropriate.
 * @param session the JMS Session to commit
 * @param message the Message to acknowledge
 * @throws javax.jms.JMSException in case of commit failure
 */
protected void commitIfNecessary(Session session, Message message) throws JMSException {
	// Commit session or acknowledge message.
	if (session.getTransacted()) {
		// Commit necessary - but avoid commit call within a JTA transaction.
		if (isSessionLocallyTransacted(session)) {
			// Transacted session created by this container -> commit.
			JmsUtils.commitIfNecessary(session);
		}
	}
	else if (message != null && isClientAcknowledge(session)) {
		message.acknowledge();
	}
}
 
Example 10
Source File: JmsTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Actually receive a JMS message.
 * @param session the JMS Session to operate on
 * @param consumer the JMS MessageConsumer to receive with
 * @return the JMS Message received, or {@code null} 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 = Math.min(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);
	}
}
 
Example 11
Source File: MQTemplate.java    From Thunder with Apache License 2.0 5 votes vote down vote up
@Override
protected void doSend(Session session, Destination destination, MessageCreator messageCreator) throws JMSException {
    MessageProducer producer = null;
    try {
        Message message = messageCreator.createMessage(session);
        boolean async = message.getBooleanProperty(ThunderConstant.ASYNC_ATTRIBUTE_NAME);
        long timeout = message.getLongProperty(ThunderConstant.TIMEOUT_ATTRIBUTE_NAME);

        producer = createProducer(session, destination);
        // DeliveryMode.PERSISTENT:持久化模式,消息在硬盘堆积模式
        // DeliveryMode.NON_PERSISTENT:非持久化模式,消息在内存堆积模式
        if (async) {
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        } else {
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }
        producer.setTimeToLive(timeout);

        doSend(producer, message);

        if (session.getTransacted() && isSessionLocallyTransacted(session)) {
            JmsUtils.commitIfNecessary(session);
        }
    } finally {
        if (producer != null) {
            JmsUtils.closeMessageProducer(producer);
        }
    }
}
 
Example 12
Source File: BatchedJmsTemplate.java    From jadira with Apache License 2.0 5 votes vote down vote up
protected List<Message> doBatchReceive(Session session, MessageConsumer consumer, int batchSize)
        throws JMSException {

    try {
        final List<Message> result;
        long timeout = determineTimeout();

        Message message = doReceive(consumer, timeout);

        if (message == null) {
            result = new ArrayList<Message>(0);
        } else {
            result = new ArrayList<Message>(batchSize);
            result.add(message);
            for (int i = 1; i < batchSize; i++) {
                message = doReceive(consumer, RECEIVE_TIMEOUT_NO_WAIT);
                if (message == null) {
                    break;
                }
                result.add(message);
            }
        }

        if (session.getTransacted()) {
            if (isSessionLocallyTransacted(session)) {
                JmsUtils.commitIfNecessary(session);
            }
        } else if (isClientAcknowledge(session)) {
            if (message != null) {
                message.acknowledge();
            }
        }
        return result;
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
    }
}
 
Example 13
Source File: BatchedMessageListenerContainer.java    From jadira with Apache License 2.0 5 votes vote down vote up
/**
 * Variant of {@link AbstractMessageListenerContainer#commitIfNecessary(Session, Message)} that performs the activity for a batch of messages.
 * @param session the JMS Session to commit
 * @param messages the messages to acknowledge
 * @throws javax.jms.JMSException in case of commit failure
 */
protected void commitIfNecessary(Session session, List<Message> messages) throws JMSException {
    // Commit session or acknowledge message.
    if (session.getTransacted()) {
        // Commit necessary - but avoid commit call within a JTA transaction.
        if (isSessionLocallyTransacted(session)) {
            // Transacted session created by this container -> commit.
            JmsUtils.commitIfNecessary(session);
        }
    } else if (messages != null && isClientAcknowledge(session)) {
        for (Message message : messages) {
            message.acknowledge();
        }
    }
}