Java Code Examples for javax.jms.Session#getTransacted()
The following examples show how to use
javax.jms.Session#getTransacted() .
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: Utils.java From qpid-broker-j with Apache License 2.0 | 6 votes |
public static List<Message> sendMessages(Session session, Destination destination, int count) throws JMSException { List<Message> messages = new ArrayList<>(count); MessageProducer producer = session.createProducer(destination); for (int i = 0; i < (count); i++) { Message next = createNextMessage(session, i); producer.send(next); messages.add(next); } if (session.getTransacted()) { session.commit(); } return messages; }
Example 2
Source File: CommitRollbackTest.java From qpid-broker-j with Apache License 2.0 | 6 votes |
@Test public void testGetTransactedOnClosedSession() throws Exception { Connection connection = getConnection(); try { Session transactedSession = connection.createSession(true, Session.SESSION_TRANSACTED); transactedSession.close(); try { transactedSession.getTransacted(); fail("According to Sun TCK invocation of Session#getTransacted on closed session should throw IllegalStateException!"); } catch (IllegalStateException e) { // passed } } finally { connection.close(); } }
Example 3
Source File: AmqpManagementFacade.java From qpid-broker-j with Apache License 2.0 | 6 votes |
public void deleteEntityUsingAmqpManagement(final String name, final Session session, final String type) throws JMSException { MessageProducer producer = session.createProducer(session.createQueue(_managementAddress)); MapMessage createMessage = session.createMapMessage(); createMessage.setStringProperty("type", type); createMessage.setStringProperty("operation", "DELETE"); createMessage.setStringProperty("index", "object-path"); createMessage.setStringProperty("key", name); producer.send(createMessage); if (session.getTransacted()) { session.commit(); } }
Example 4
Source File: JmsTemplate.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * 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 5
Source File: AmqpManagementFacade.java From qpid-broker-j with Apache License 2.0 | 6 votes |
private void updateEntityUsingAmqpManagement(final String name, final String type, Map<String, Object> attributes, Destination replyToDestination, final Session session) throws JMSException { MessageProducer producer = session.createProducer(session.createQueue(_managementAddress)); MapMessage createMessage = session.createMapMessage(); createMessage.setStringProperty("type", type); createMessage.setStringProperty("operation", "UPDATE"); createMessage.setStringProperty("index", "object-path"); createMessage.setStringProperty("key", name); createMessage.setJMSReplyTo(replyToDestination); for (Map.Entry<String, Object> entry : attributes.entrySet()) { createMessage.setObject(entry.getKey(), entry.getValue()); } producer.send(createMessage); if (session.getTransacted()) { session.commit(); } producer.close(); }
Example 6
Source File: JmsTemplate.java From spring-analysis-note with MIT License | 6 votes |
/** * 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 7
Source File: SingleConnectionFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testCachingConnectionFactoryWithQueueConnectionFactoryAndJms102Usage() throws JMSException { QueueConnectionFactory cf = mock(QueueConnectionFactory.class); QueueConnection con = mock(QueueConnection.class); QueueSession txSession = mock(QueueSession.class); QueueSession nonTxSession = mock(QueueSession.class); given(cf.createQueueConnection()).willReturn(con); given(con.createQueueSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession); given(txSession.getTransacted()).willReturn(true); given(con.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession); CachingConnectionFactory scf = new CachingConnectionFactory(cf); scf.setReconnectOnException(false); Connection con1 = scf.createQueueConnection(); Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE); session1.rollback(); session1.close(); session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE); session1.close(); con1.start(); QueueConnection con2 = scf.createQueueConnection(); Session session2 = con2.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE); session2.close(); session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE); session2.getTransacted(); session2.close(); // should lead to rollback con2.start(); con1.close(); con2.close(); scf.destroy(); // should trigger actual close verify(txSession).rollback(); verify(txSession).close(); verify(nonTxSession).close(); verify(con).start(); verify(con).stop(); verify(con).close(); }
Example 8
Source File: SingleConnectionFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage() throws JMSException { TopicConnectionFactory cf = mock(TopicConnectionFactory.class); TopicConnection con = mock(TopicConnection.class); TopicSession txSession = mock(TopicSession.class); TopicSession nonTxSession = mock(TopicSession.class); given(cf.createTopicConnection()).willReturn(con); given(con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession); given(txSession.getTransacted()).willReturn(true); given(con.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession); CachingConnectionFactory scf = new CachingConnectionFactory(cf); scf.setReconnectOnException(false); Connection con1 = scf.createTopicConnection(); Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE); session1.getTransacted(); session1.close(); // should lead to rollback session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE); session1.close(); con1.start(); TopicConnection con2 = scf.createTopicConnection(); Session session2 = con2.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE); session2.close(); session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE); session2.getTransacted(); session2.close(); con2.start(); con1.close(); con2.close(); scf.destroy(); // should trigger actual close verify(txSession).close(); verify(nonTxSession).close(); verify(con).start(); verify(con).stop(); verify(con).close(); }
Example 9
Source File: BatchedJmsTemplate.java From jadira with Apache License 2.0 | 5 votes |
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 10
Source File: AbstractMessageListenerContainer.java From java-technology-stack with MIT License | 5 votes |
/** * 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 11
Source File: SingleConnectionFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testCachingConnectionFactory() throws JMSException { ConnectionFactory cf = mock(ConnectionFactory.class); Connection con = mock(Connection.class); Session txSession = mock(Session.class); Session nonTxSession = mock(Session.class); given(cf.createConnection()).willReturn(con); given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession); given(txSession.getTransacted()).willReturn(true); given(con.createSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession); CachingConnectionFactory scf = new CachingConnectionFactory(cf); scf.setReconnectOnException(false); Connection con1 = scf.createConnection(); Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE); session1.getTransacted(); session1.close(); // should lead to rollback session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE); session1.close(); con1.start(); Connection con2 = scf.createConnection(); Session session2 = con2.createSession(false, Session.CLIENT_ACKNOWLEDGE); session2.close(); session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE); session2.commit(); session2.close(); con2.start(); con1.close(); con2.close(); scf.destroy(); // should trigger actual close verify(txSession).commit(); verify(txSession).close(); verify(nonTxSession).close(); verify(con).start(); verify(con).stop(); verify(con).close(); }
Example 12
Source File: SingleConnectionFactoryTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testCachingConnectionFactory() throws JMSException { ConnectionFactory cf = mock(ConnectionFactory.class); Connection con = mock(Connection.class); Session txSession = mock(Session.class); Session nonTxSession = mock(Session.class); given(cf.createConnection()).willReturn(con); given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession); given(txSession.getTransacted()).willReturn(true); given(con.createSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession); CachingConnectionFactory scf = new CachingConnectionFactory(cf); scf.setReconnectOnException(false); Connection con1 = scf.createConnection(); Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE); session1.getTransacted(); session1.close(); // should lead to rollback session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE); session1.close(); con1.start(); Connection con2 = scf.createConnection(); Session session2 = con2.createSession(false, Session.CLIENT_ACKNOWLEDGE); session2.close(); session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE); session2.commit(); session2.close(); con2.start(); con1.close(); con2.close(); scf.destroy(); // should trigger actual close verify(txSession).commit(); verify(txSession).close(); verify(nonTxSession).close(); verify(con).start(); verify(con).stop(); verify(con).close(); }
Example 13
Source File: SingleConnectionFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testCachingConnectionFactory() throws JMSException { ConnectionFactory cf = mock(ConnectionFactory.class); Connection con = mock(Connection.class); Session txSession = mock(Session.class); Session nonTxSession = mock(Session.class); given(cf.createConnection()).willReturn(con); given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession); given(txSession.getTransacted()).willReturn(true); given(con.createSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession); CachingConnectionFactory scf = new CachingConnectionFactory(cf); scf.setReconnectOnException(false); Connection con1 = scf.createConnection(); Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE); session1.getTransacted(); session1.close(); // should lead to rollback session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE); session1.close(); con1.start(); Connection con2 = scf.createConnection(); Session session2 = con2.createSession(false, Session.CLIENT_ACKNOWLEDGE); session2.close(); session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE); session2.commit(); session2.close(); con2.start(); con1.close(); con2.close(); scf.destroy(); // should trigger actual close verify(txSession).commit(); verify(txSession).close(); verify(nonTxSession).close(); verify(con).start(); verify(con).stop(); verify(con).close(); }
Example 14
Source File: SingleConnectionFactoryTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage() throws JMSException { TopicConnectionFactory cf = mock(TopicConnectionFactory.class); TopicConnection con = mock(TopicConnection.class); TopicSession txSession = mock(TopicSession.class); TopicSession nonTxSession = mock(TopicSession.class); given(cf.createTopicConnection()).willReturn(con); given(con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession); given(txSession.getTransacted()).willReturn(true); given(con.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession); CachingConnectionFactory scf = new CachingConnectionFactory(cf); scf.setReconnectOnException(false); Connection con1 = scf.createTopicConnection(); Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE); session1.getTransacted(); session1.close(); // should lead to rollback session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE); session1.close(); con1.start(); TopicConnection con2 = scf.createTopicConnection(); Session session2 = con2.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE); session2.close(); session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE); session2.getTransacted(); session2.close(); con2.start(); con1.close(); con2.close(); scf.destroy(); // should trigger actual close verify(txSession).close(); verify(nonTxSession).close(); verify(con).start(); verify(con).stop(); verify(con).close(); }
Example 15
Source File: JmsPoolConnectionIdleEvictionsFromPoolTest.java From pooled-jms with Apache License 2.0 | 5 votes |
@Test(timeout = 60000) public void testNotIdledWhenInUse() throws Exception { cf.setConnectionIdleTimeout(10); JmsPoolConnection connection = (JmsPoolConnection) cf.createConnection(); Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // let connection to get idle TimeUnit.MILLISECONDS.sleep(20); // get a connection from pool again, it should be the same underlying connection // as before and should not be idled out since an open session exists. JmsPoolConnection connection2 = (JmsPoolConnection) cf.createConnection(); assertSame(connection.getConnection(), connection2.getConnection()); // now the session is closed even when it should not be try { // any operation on session first checks whether session is closed s.getTransacted(); } catch (javax.jms.IllegalStateException e) { assertTrue("Session should be fine, instead: " + e.getMessage(), false); } Connection original = connection.getConnection(); connection.close(); connection2.close(); // let connection to get idle TimeUnit.MILLISECONDS.sleep(40); // get a connection from pool again, it should be a new Connection instance as the // old one should have been inactive and idled out. JmsPoolConnection connection3 = (JmsPoolConnection) cf.createConnection(); assertNotSame(original, connection3.getConnection()); }
Example 16
Source File: AbstractMessageListenerContainer.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * 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 17
Source File: JmsClient.java From ats-framework with Apache License 2.0 | 5 votes |
private synchronized Session loadSession( final boolean transacted, final int acknowledgeMode ) throws JMSException { final Session s = session; if (s != null) { if ( (s.getTransacted() == transacted) && (s.getAcknowledgeMode() == acknowledgeMode)) { return s; } s.close(); } final Session newSession = loadConnection().createSession(transacted, acknowledgeMode); session = newSession; return newSession; }
Example 18
Source File: MemoryConsumptionTestClient.java From qpid-broker-j with Apache License 2.0 | 5 votes |
private void publish(int numberOfMessages, int messageSize, int numberOfProducers, int deliveryMode, Destination destination, Map<Connection, List<Session>> connectionsAndSessions) throws JMSException { byte[] messageBytes = generateMessage(messageSize); for (List<Session> sessions : connectionsAndSessions.values()) { for (Session session: sessions) { BytesMessage message = session.createBytesMessage(); if (messageSize > 0) { message.writeBytes(messageBytes); } for(int i = 0; i < numberOfProducers ; i++) { MessageProducer prod = session.createProducer(destination); for(int j = 0; j < numberOfMessages ; j++) { prod.send(message, deliveryMode, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); if(session.getTransacted()) { session.commit(); } } } } } }
Example 19
Source File: SingleConnectionFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage() throws JMSException { TopicConnectionFactory cf = mock(TopicConnectionFactory.class); TopicConnection con = mock(TopicConnection.class); TopicSession txSession = mock(TopicSession.class); TopicSession nonTxSession = mock(TopicSession.class); given(cf.createTopicConnection()).willReturn(con); given(con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession); given(txSession.getTransacted()).willReturn(true); given(con.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession); CachingConnectionFactory scf = new CachingConnectionFactory(cf); scf.setReconnectOnException(false); Connection con1 = scf.createTopicConnection(); Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE); session1.getTransacted(); session1.close(); // should lead to rollback session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE); session1.close(); con1.start(); TopicConnection con2 = scf.createTopicConnection(); Session session2 = con2.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE); session2.close(); session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE); session2.getTransacted(); session2.close(); con2.start(); con1.close(); con2.close(); scf.destroy(); // should trigger actual close verify(txSession).close(); verify(nonTxSession).close(); verify(con).start(); verify(con).stop(); verify(con).close(); }
Example 20
Source File: AmqpManagementFacade.java From qpid-broker-j with Apache License 2.0 | 4 votes |
public Map<String, Object> createEntityAndAssertResponse(final String name, final String type, final Map<String, Object> attributes, final Session session) throws JMSException { Destination replyToDestination; Destination replyConsumerDestination; if (_protocol == Protocol.AMQP_1_0) { replyToDestination = session.createTemporaryQueue(); replyConsumerDestination = replyToDestination; } else { replyToDestination = session.createQueue(AMQP_0_X_REPLY_TO_DESTINATION); replyConsumerDestination = session.createQueue(AMQP_0_X_CONSUMER_REPLY_DESTINATION); } MessageConsumer consumer = session.createConsumer(replyConsumerDestination); MessageProducer producer = session.createProducer(session.createQueue(_managementAddress)); MapMessage createMessage = session.createMapMessage(); createMessage.setStringProperty("type", type); createMessage.setStringProperty("operation", "CREATE"); createMessage.setString("name", name); createMessage.setString("object-path", name); createMessage.setJMSReplyTo(replyToDestination); for (Map.Entry<String, Object> entry : attributes.entrySet()) { createMessage.setObject(entry.getKey(), entry.getValue()); } producer.send(createMessage); if (session.getTransacted()) { session.commit(); } producer.close(); return receiveManagementResponse(consumer, replyToDestination, 201); }