@Test public void testInbound() { @SuppressWarnings("unchecked") final Message<String> out = (Message<String>) new ObjectToJsonTransformer() .transform(new GenericMessage<Foo>(new Foo())); MessageProperties messageProperties = new MessageProperties(); DefaultAmqpHeaderMapper.outboundMapper().fromHeadersToRequest(out.getHeaders(), messageProperties); final BasicProperties props = new DefaultMessagePropertiesConverter().fromMessageProperties(messageProperties, "UTF-8"); this.rabbitTemplate.execute(channel -> { channel.basicPublish("", JSON_TESTQ, props, out.getPayload().getBytes()); return null; }); Object received = this.rabbitTemplate.receiveAndConvert(JSON_TESTQ); assertThat(received, instanceOf(Foo.class)); }
@Override @Nullable public <T> T receiveAndConvert(ParameterizedTypeReference<T> type) throws AmqpException { return receiveAndConvert(this.getRequiredQueue(), type); }
@Override @Nullable public Object receiveAndConvert() throws AmqpException { return receiveAndConvert(this.getRequiredQueue()); }
@Override @Nullable public Object receiveAndConvert(long timeoutMillis) throws AmqpException { return receiveAndConvert(this.getRequiredQueue(), timeoutMillis); }
@Test public void testReceiveInTransaction() throws Exception { template.convertAndSend(ROUTE, "message"); String result = transactionTemplate.execute(status -> (String) template.receiveAndConvert(ROUTE)); assertEquals("message", result); result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }
@Test public void testSendAndReceive() throws Exception { template.convertAndSend(ROUTE, "message"); String result = (String) template.receiveAndConvert(ROUTE); assertEquals("message", result); 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 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); }
@Test public void testReceiveFromNonExistentVirtualHost() throws Exception { connectionFactory.setVirtualHost("non-existent"); RabbitTemplate template = new RabbitTemplate(connectionFactory); // Wrong vhost is very unfriendly to client - the exception has no clue (just an EOF) exception.expect(anyOf(instanceOf(AmqpIOException.class), instanceOf(AmqpAuthenticationException.class))); template.receiveAndConvert("foo"); }
@Test public void testReceiveTimeoutRequeue() { assertNull(this.template.receiveAndConvert(ROUTE, 1)); assertEquals(0, TestUtils.getPropertyValue(this.connectionFactory, "cachedChannelsNonTransactional", List.class) .size()); }
@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"); }
private void doSunnyDayTest(CountDownLatch latch, MessageListener listener) throws Exception { container = createContainer(listener); for (int i = 0; i < messageCount; i++) { template.convertAndSend(queue.getName(), i + "foo"); } boolean waited = latch.await(Math.max(10, messageCount / 20), TimeUnit.SECONDS); assertTrue("Timed out waiting for message", waited); assertNull(template.receiveAndConvert(queue.getName())); }
@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(); }
@Test public void testSendInGlobalTransactionCommit() throws Exception { testSendInGlobalTransactionGuts(false); String result = (String) template.receiveAndConvert(ROUTE); assertEquals("message", result); assertNull(template.receive(ROUTE)); }
@Test @DirtiesContext public void simpleEndpointWithSendTo() throws InterruptedException { rabbitTemplate.convertAndSend("test.sendTo", "bar"); rabbitTemplate.setReceiveTimeout(10000); Object result = rabbitTemplate.receiveAndConvert("test.sendTo.reply"); assertNotNull(result); assertEquals("BAR", result); }
@Test @DirtiesContext public void simpleEndpointWithSendToSpel() throws InterruptedException { rabbitTemplate.convertAndSend("test.sendTo.spel", "bar"); rabbitTemplate.setReceiveTimeout(10000); Object result = rabbitTemplate.receiveAndConvert("test.sendTo.reply.spel"); assertNotNull(result); assertEquals("BARbar", result); }
@Test public void testConnect() throws Exception { RabbitAdmin admin = new RabbitAdmin(this.lqcf); Queue queue = new Queue(UUID.randomUUID().toString(), false, false, true); admin.declareQueue(queue); ConnectionFactory targetConnectionFactory = this.lqcf.getTargetConnectionFactory("[" + queue.getName() + "]"); RabbitTemplate template = new RabbitTemplate(targetConnectionFactory); template.convertAndSend("", queue.getName(), "foo"); assertEquals("foo", template.receiveAndConvert(queue.getName())); admin.deleteQueue(queue.getName()); }
@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)); }