@Override public Void doInJms(Session session) throws JMSException { session.getTransacted(); return null; } });
@Override public Void doInJms(Session session) throws JMSException { session.getTransacted(); return null; } });
@Override public Void doInJms(Session session) throws JMSException { session.getTransacted(); return null; } });
private void logicalClose(Session proxy) throws JMSException { // Preserve rollback-on-close semantics. if (this.transactionOpen && this.target.getTransacted()) { this.transactionOpen = false; this.target.rollback(); } // Physically close durable subscribers at time of Session close call. for (Iterator<Map.Entry<ConsumerCacheKey, MessageConsumer>> it = this.cachedConsumers.entrySet().iterator(); it.hasNext();) { Map.Entry<ConsumerCacheKey, MessageConsumer> entry = it.next(); if (entry.getKey().subscription != null) { entry.getValue().close(); it.remove(); } } // Allow for multiple close calls... boolean returned = false; synchronized (this.sessionList) { if (!this.sessionList.contains(proxy)) { this.sessionList.addLast(proxy); returned = true; } } if (returned && logger.isTraceEnabled()) { logger.trace("Returned cached Session: " + this.target); } }
/** * Perform a rollback, handling rollback exceptions properly. * @param session the JMS Session to rollback * @param ex the thrown application exception or error * @throws javax.jms.JMSException in case of a rollback error */ protected void rollbackOnExceptionIfNecessary(Session session, Throwable ex) throws JMSException { try { if (session.getTransacted()) { if (isSessionLocallyTransacted(session)) { // Transacted session created by this container -> rollback. if (logger.isDebugEnabled()) { logger.debug("Initiating transaction rollback on application exception", ex); } JmsUtils.rollbackIfNecessary(session); } } else if (isClientAcknowledge(session)) { session.recover(); } } catch (IllegalStateException ex2) { logger.debug("Could not roll back because Session already closed", ex2); } catch (JMSException | RuntimeException | Error ex2) { logger.error("Application exception overridden by rollback error", ex); throw ex2; } }
/** * 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(); } }
/** * 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); } }
/** * Perform a rollback, if appropriate. * @param session the JMS Session to rollback * @throws javax.jms.JMSException in case of a rollback error */ protected void rollbackIfNecessary(Session session) throws JMSException { if (session.getTransacted()) { if (isSessionLocallyTransacted(session)) { // Transacted session created by this container -> rollback. JmsUtils.rollbackIfNecessary(session); } } else if (isClientAcknowledge(session)) { session.recover(); } }
if (session.getTransacted()) {
given(txSession.getTransacted()).willReturn(true); given(con.createSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession); Connection con1 = scf.createConnection(); Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE); session1.getTransacted();
@Test public void testProducerCallback() throws Exception { JmsTemplate template = createTemplate(); template.setConnectionFactory(this.connectionFactory); MessageProducer messageProducer = mock(MessageProducer.class); given(this.session.createProducer(null)).willReturn(messageProducer); given(messageProducer.getPriority()).willReturn(4); template.execute((ProducerCallback<Void>) (session1, producer) -> { session1.getTransacted(); producer.getPriority(); return null; }); verify(messageProducer).close(); verify(this.session).close(); verify(this.connection).close(); }
/** * Create the mock objects for testing. */ @Before public void setupMocks() throws Exception { this.jndiContext = mock(Context.class); this.connectionFactory = mock(ConnectionFactory.class); this.connection = mock(Connection.class); this.session = mock(Session.class); this.queue = mock(Queue.class); given(this.connectionFactory.createConnection()).willReturn(this.connection); given(this.connection.createSession(useTransactedTemplate(), Session.AUTO_ACKNOWLEDGE)).willReturn(this.session); given(this.session.getTransacted()).willReturn(useTransactedSession()); given(this.jndiContext.lookup("testDestination")).willReturn(this.queue); }
@Test public void testProducerCallbackWithIdAndTimestampDisabled() throws Exception { JmsTemplate template = createTemplate(); template.setConnectionFactory(this.connectionFactory); template.setMessageIdEnabled(false); template.setMessageTimestampEnabled(false); MessageProducer messageProducer = mock(MessageProducer.class); given(this.session.createProducer(null)).willReturn(messageProducer); given(messageProducer.getPriority()).willReturn(4); template.execute((ProducerCallback<Void>) (session1, producer) -> { session1.getTransacted(); producer.getPriority(); return null; }); verify(messageProducer).setDisableMessageID(true); verify(messageProducer).setDisableMessageTimestamp(true); verify(messageProducer).close(); verify(this.session).close(); verify(this.connection).close(); }
@Test public void testTransactionCommitWithMessageProducer() throws JMSException { Destination dest = new StubQueue(); ConnectionFactory cf = mock(ConnectionFactory.class); Connection con = mock(Connection.class); Session session = mock(Session.class); MessageProducer producer = mock(MessageProducer.class); final Message message = mock(Message.class); given(cf.createConnection()).willReturn(con); given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session); given(session.createProducer(dest)).willReturn(producer); given(session.getTransacted()).willReturn(true); JmsTransactionManager tm = new JmsTransactionManager(cf); TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition()); JmsTemplate jt = new JmsTemplate(cf); jt.send(dest, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return message; } }); tm.commit(ts); verify(producer).send(message); verify(session).commit(); verify(producer).close(); verify(session).close(); verify(con).close(); }
Connection con1 = scf.createTopicConnection(); Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE); session1.getTransacted(); session2.close(); session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE); session2.getTransacted(); session2.close(); con2.start();
given(session.getTransacted()).willReturn(false);
given(session.getTransacted()).willReturn(false);
given(session.getTransacted()).willReturn(true);
given(session.getTransacted()).willReturn(false);
given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION); given(session.getTransacted()).willReturn(false); given(session.getAcknowledgeMode()).willReturn(Session.AUTO_ACKNOWLEDGE);