public void setTemplateChannelTransacted(boolean channelTransacted) { if (this.amqpTemplate instanceof RabbitTemplate) { ((RabbitTemplate) this.amqpTemplate).setChannelTransacted(channelTransacted); } else if (logger.isInfoEnabled()) { logger.info("AmqpTemplate is not a RabbitTemplate, so configured 'channelTransacted' will be ignored."); } }
public void setTemplateChannelTransacted(boolean channelTransacted) { if (this.amqpTemplate instanceof RabbitTemplate) { ((RabbitTemplate) this.amqpTemplate).setChannelTransacted(channelTransacted); } else if (logger.isInfoEnabled()) { logger.info("AmqpTemplate is not a RabbitTemplate, so configured 'channelTransacted' will be ignored."); } }
@Bean public RabbitTemplate rabbitTemplate() throws Exception { RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory()); rabbitTemplate.setChannelTransacted(true); return rabbitTemplate; }
@Bean public RabbitTemplate rabbitTemplate() throws Exception { RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory()); rabbitTemplate.setChannelTransacted(true); return rabbitTemplate; }
@Test @Repeat(200) public void testSendAndReceiveTransacted() throws Exception { template.setChannelTransacted(true); template.convertAndSend(ROUTE, "message"); String result = (String) template.receiveAndConvert(ROUTE); assertThat(result).isEqualTo("message"); }
private void testSendInGlobalTransactionGuts(final boolean rollback) throws Exception { template.setChannelTransacted(true); new TransactionTemplate(new TestTransactionManager()).execute(status -> { template.convertAndSend(ROUTE, "message"); if (rollback) { TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { @Override public void afterCommit() { TransactionSynchronizationUtils .triggerAfterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK); } }); } return null; }); }
@Test @Repeat(200) public void testSendAndReceiveExternalTransacted() throws Exception { template.setChannelTransacted(true); new TransactionTemplate(new TestTransactionManager()).execute(status -> { template.convertAndSend(ROUTE, "message"); return null; }); template.convertAndSend(ROUTE, "message"); String result = (String) template.receiveAndConvert(ROUTE); assertThat(result).isEqualTo("message"); }
@Test public void testReceiveInExternalTransaction() throws Exception { template.convertAndSend(ROUTE, "message"); template.setChannelTransacted(true); String result = new TransactionTemplate(new TestTransactionManager()) .execute(status -> (String) template.receiveAndConvert(ROUTE)); assertEquals("message", result); result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }
@Test public void testReceiveInExternalTransactionAutoAck() throws Exception { template.convertAndSend(ROUTE, "message"); // Should just result in auto-ack (not synched with external tx) template.setChannelTransacted(true); String result = new TransactionTemplate(new TestTransactionManager()) .execute(status -> (String) template.receiveAndConvert(ROUTE)); assertEquals("message", result); result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }
@Test public void testSendInExternalTransaction() throws Exception { template.setChannelTransacted(true); new TransactionTemplate(new TestTransactionManager()).execute(status -> { template.convertAndSend(ROUTE, "message"); return null; }); String result = (String) template.receiveAndConvert(ROUTE); assertEquals("message", result); result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }
@Before public void init() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); connectionFactory.setHost("localhost"); template = new RabbitTemplate(connectionFactory); template.setChannelTransacted(true); RabbitTransactionManager transactionManager = new RabbitTransactionManager(connectionFactory); transactionManager.afterPropertiesSet(); transactionTemplate = new TransactionTemplate(transactionManager); }
@Test public void testSendInTransaction() throws Exception { template.setChannelTransacted(true); transactionTemplate.execute(status -> { template.convertAndSend(ROUTE, "message"); return null; }); String result = (String) template.receiveAndConvert(ROUTE); assertEquals("message", result); result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }
@Test public void testSendInTransactionWithRollback() throws Exception { template.setChannelTransacted(true); try { transactionTemplate.execute(status -> { template.convertAndSend(ROUTE, "message"); throw new PlannedException(); }); fail("Expected PlannedException"); } catch (PlannedException e) { // Expected } String result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }
@Test public void testSendInExternalTransactionWithRollback() throws Exception { template.setChannelTransacted(true); try { new TransactionTemplate(new TestTransactionManager()).execute(status -> { template.convertAndSend(ROUTE, "message"); throw new PlannedException(); }); fail("Expected PlannedException"); } catch (PlannedException e) { // Expected } String result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }
@Test public void testSendAndReceiveTransacted() throws Exception { template.setChannelTransacted(true); template.convertAndSend(ROUTE, "message"); String result = (String) template.receiveAndConvert(ROUTE); assertEquals("message", result); result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }
@Test public void testReceiveBlockingTx() throws Exception { this.template.convertAndSend(ROUTE, "blockTX"); this.template.setChannelTransacted(true); this.template.setReceiveTimeout(10000); String out = (String) this.template.receiveAndConvert(ROUTE); assertNotNull(out); assertEquals("blockTX", out); this.template.setReceiveTimeout(0); assertNull(this.template.receive(ROUTE)); }
@Test public void testSendToNonExistentAndThenReceive() throws Exception { // If transacted then the commit fails on send, so we get a nice synchronous // exception template.setChannelTransacted(true); try { template.convertAndSend("", "no.such.route", "message"); // fail("Expected AmqpException"); } catch (AmqpException e) { // e.printStackTrace(); } // Now send the real message, and all should be well... template.convertAndSend(ROUTE, "message"); String result = (String) template.receiveAndConvert(ROUTE); assertEquals("message", result); result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }
@Test public void testSendAndReceiveTransactedWithUncachedConnection() throws Exception { final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost"); RabbitTemplate template = new RabbitTemplate(singleConnectionFactory); template.setChannelTransacted(true); template.convertAndSend(ROUTE, "message"); String result = (String) template.receiveAndConvert(ROUTE); assertEquals("message", result); result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); singleConnectionFactory.destroy(); }
/** * @param args */ public static void main(String[] args) { SingleConnectionFactory connectionFactory = new SingleConnectionFactory("localhost"); connectionFactory.setUsername("guest"); connectionFactory.setPassword("guest"); RabbitTemplate template = new RabbitTemplate(); template.setConnectionFactory(connectionFactory); template.setChannelTransacted(true); template.afterPropertiesSet(); final String routingKey = TestConstants.ROUTING_KEY; QueueUtils.declareTestQueue(template, routingKey); // send message sendMessages(template, TestConstants.EXCHANGE_NAME, routingKey, TestConstants.NUM_MESSAGES); }
@Test public void testReceiveBlockingGlobalTx() throws Exception { template.convertAndSend(ROUTE, "blockGTXNoTO"); RabbitResourceHolder resourceHolder = ConnectionFactoryUtils .getTransactionalResourceHolder(this.template.getConnectionFactory(), true); TransactionSynchronizationManager.setActualTransactionActive(true); ConnectionFactoryUtils.bindResourceToTransaction(resourceHolder, this.template.getConnectionFactory(), true); template.setReceiveTimeout(-1); template.setChannelTransacted(true); String out = (String) template.receiveAndConvert(ROUTE); resourceHolder.commitAll(); resourceHolder.closeAll(); assertSame(resourceHolder, TransactionSynchronizationManager.unbindResource(template.getConnectionFactory())); assertNotNull(out); assertEquals("blockGTXNoTO", out); this.template.setReceiveTimeout(0); assertNull(this.template.receive(ROUTE)); }