List of usage examples for javax.jms Session getTransacted
boolean getTransacted() throws JMSException;
From source file:com.ccc.ccm.client.JMSTemplateAutowired.java
/** * Actually receive a JMS message.// ww w . ja v a 2 s .c om * @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:com.ccc.ccm.client.JMSTemplateAutowired.java
/** * Send the given JMS message.//from ww w. java 2s.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:org.apache.qpid.disttest.jms.ClientJmsDelegate.java
public void createSession(final CreateSessionCommand command) { try {/*from w w w .j a v a 2 s . c o m*/ final Connection connection = _testConnections.get(command.getConnectionName()); if (connection == null) { throw new DistributedTestException( "No test connection found called: " + command.getConnectionName(), command); } final boolean transacted = command.getAcknowledgeMode() == Session.SESSION_TRANSACTED; final Session newSession = connection.createSession(transacted, command.getAcknowledgeMode()); LOGGER.debug("Created session " + command.getSessionName() + " with transacted = " + newSession.getTransacted() + " and acknowledgeMode = " + newSession.getAcknowledgeMode()); addSession(command.getSessionName(), newSession); } catch (final JMSException jmse) { throw new DistributedTestException("Unable to create new session: " + command, jmse); } }
From source file:org.apache.qpid.disttest.jms.ClientJmsDelegate.java
public void commitOrAcknowledgeMessageIfNecessary(final String sessionName, final Message message) { try {/*from w ww . ja va 2 s. c o m*/ final Session session = _testSessions.get(sessionName); if (session.getTransacted()) { synchronized (session) { session.commit(); } } else if (message != null && session.getAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE) { message.acknowledge(); } } catch (final JMSException jmse) { throw new DistributedTestException("Unable to commit or acknowledge message on session: " + sessionName, jmse); } }
From source file:org.apache.qpid.disttest.jms.ClientJmsDelegate.java
public void rollbackOrRecoverIfNecessary(String sessionName) { try {/*from w ww . ja v a 2 s . c o m*/ final Session session = _testSessions.get(sessionName); synchronized (session) { if (session.getTransacted()) { session.rollback(); } else if (session.getAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE) { session.recover(); } } } catch (final JMSException jmse) { throw new DistributedTestException("Unable to rollback or recover on session: " + sessionName, jmse); } }
From source file:org.apache.qpid.test.utils.QpidBrokerTestCase.java
/** * Send messages to the given destination. * * If session is transacted then messages will be committed before returning * * @param session the session to use for sending * @param destination where to send them to * @param count no. of messages to send/* w w w . j a v a2 s. c o m*/ * * @param offset offset allows the INDEX value of the message to be adjusted. * @param batchSize the batchSize in which to commit, 0 means no batching, * but a single commit at the end * @return the sent message * * @throws Exception */ public List<Message> sendMessage(Session session, Destination destination, int count, int offset, int batchSize) throws Exception { List<Message> messages = new ArrayList<Message>(count); MessageProducer producer = session.createProducer(destination); int i = offset; for (; i < (count + offset); i++) { Message next = createNextMessage(session, i); producer.send(next); if (session.getTransacted() && batchSize > 0) { if (i % batchSize == 0) { session.commit(); } } messages.add(next); } // Ensure we commit the last messages // Commit the session if we are transacted and // we have no batchSize or // our count is not divible by batchSize. if (session.getTransacted() && ( batchSize == 0 || (i-1) % batchSize != 0)) { session.commit(); } return messages; }
From source file:org.openadaptor.auxil.connector.jms.JMSReadConnector.java
/** * Test transacted status of the JMS Session object. Wrap any JMSExceptions in a ConnectionException. * @param aSession Session to be tested. * @return Transacted or not// w w w . j a v a 2 s. c o m */ private boolean isTransactedSession(Session aSession) { try { return aSession.getTransacted(); } catch (JMSException e) { throw new ConnectionException( "Error testing transacted state of JMS Session : [" + getDestinationName() + "]", e, this); } }
From source file:org.springframework.jms.core.JmsTemplate.java
protected void doSend(Session session, Destination destination, MessageCreator messageCreator) throws JMSException { MessageProducer producer = createProducer(session, destination); Message message = messageCreator.createMessage(session); if (logger.isDebugEnabled()) { logger.debug("Sending created message [" + message + "]"); }//from w w w.j a va 2 s . c o m doSend(producer, message); if (session.getTransacted() && !TransactionSynchronizationManager.hasResource(getConnectionFactory())) { // transacted session created by this template -> commit session.commit(); } }
From source file:org.springframework.jms.core.JmsTemplate.java
protected Message doReceive(Session session, Destination destination) throws JMSException { MessageConsumer consumer = createConsumer(session, destination); try {/* w ww.jav a 2s . c o m*/ // use transaction timeout if available long timeout = getReceiveTimeout(); ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager .getResource(getConnectionFactory()); if (conHolder != null && conHolder.hasTimeout()) { timeout = conHolder.getTimeToLiveInMillis(); } Message message = (timeout >= 0) ? consumer.receive(timeout) : consumer.receive(); if (session.getTransacted()) { if (conHolder == null) { // transacted session created by this template -> commit session.commit(); } } else if (message != null && isClientAcknowledge(session)) { message.acknowledge(); } return message; } finally { JmsUtils.closeMessageConsumer(consumer); } }
From source file:org.wso2.carbon.event.output.adapter.jms.internal.util.JMSMessageSender.java
/** * Perform actual send of JMS message to the Destination selected *//*from w w w . j a v a 2 s. c o m*/ public void send(Object message, JMSEventAdapter.PublisherDetails publisherDetails, String jmsHeaders) { Map<String, String> messageProperties = publisherDetails.getMessageConfig(); Boolean jtaCommit = getBooleanProperty(messageProperties, BaseConstants.JTA_COMMIT_AFTER_SEND); Boolean rollbackOnly = getBooleanProperty(messageProperties, BaseConstants.SET_ROLLBACK_ONLY); Boolean persistent = getBooleanProperty(messageProperties, JMSConstants.JMS_DELIVERY_MODE); Integer priority = getIntegerProperty(messageProperties, JMSConstants.JMS_PRIORITY); Integer timeToLive = getIntegerProperty(messageProperties, JMSConstants.JMS_TIME_TO_LIVE); MessageProducer producer = null; Destination destination = null; Session session = null; boolean sendingSuccessful = false; JMSConnectionFactory.JMSPooledConnectionHolder pooledConnection = null; try { pooledConnection = jmsConnectionFactory.getConnectionFromPool(); producer = pooledConnection.getProducer(); session = pooledConnection.getSession(); Message jmsMessage = convertToJMSMessage(message, publisherDetails.getMessageConfig(), session); setJMSTransportHeaders(jmsMessage, jmsHeaders); // Do not commit, if message is marked for rollback if (rollbackOnly != null && rollbackOnly) { jtaCommit = Boolean.FALSE; } if (persistent != null) { try { producer.setDeliveryMode(DeliveryMode.PERSISTENT); } catch (JMSException e) { handleConnectionException("Error setting JMS Producer for PERSISTENT delivery", e); } } if (priority != null) { try { producer.setPriority(priority); } catch (JMSException e) { handleConnectionException("Error setting JMS Producer priority to : " + priority, e); } } if (timeToLive != null) { try { producer.setTimeToLive(timeToLive); } catch (JMSException e) { handleConnectionException("Error setting JMS Producer TTL to : " + timeToLive, e); } } // perform actual message sending // try { if (jmsSpec11 || isQueue == null) { producer.send(jmsMessage); } else { if (isQueue) { ((QueueSender) producer).send(jmsMessage); } else { ((TopicPublisher) producer).publish(jmsMessage); } } sendingSuccessful = true; if (log.isDebugEnabled()) { // // set the actual MessageID to the message context for use by any others down the line String msgId = null; try { msgId = jmsMessage.getJMSMessageID(); } catch (JMSException jmse) { log.error(jmse.getMessage(), jmse); } log.debug(" with JMS Message ID : " + msgId + " to destination : " + producer.getDestination()); } } catch (JMSException e) { handleConnectionException("Error sending message to destination : " + destination, e); } catch (Exception e) { log.error(e.getMessage(), e); } finally { if (jtaCommit != null) { try { if (session.getTransacted()) { if (sendingSuccessful && (rollbackOnly == null || !rollbackOnly)) { session.commit(); } else { session.rollback(); } } if (log.isDebugEnabled()) { log.debug((sendingSuccessful ? "Committed" : "Rolled back") + " local (JMS Session) Transaction"); } } catch (Exception e) { handleConnectionException("Error committing/rolling back local (i.e. session) " + "transaction after sending of message "//with MessageContext ID : " + + " to destination : " + destination, e); } } if (pooledConnection != null) { jmsConnectionFactory.returnPooledConnection(pooledConnection); } } }