@Scheduled(fixedDelay = 1000, initialDelay = 500) public void send() { System.out.println(" [x] Requesting fib(" + start + ")"); Integer response = (Integer) template.convertSendAndReceive(exchange.getName(), "rpc", start++); System.out.println(" [.] Got '" + response + "'"); }
@Override @Nullable public Object convertSendAndReceive(final String routingKey, final Object message) throws AmqpException { return convertSendAndReceive(routingKey, message, (CorrelationData) null); }
@Override @Nullable public Object convertSendAndReceive(final String routingKey, final Object message, final MessagePostProcessor messagePostProcessor) throws AmqpException { return convertSendAndReceive(routingKey, message, messagePostProcessor, null); }
@Override @Nullable public Object convertSendAndReceive(final String exchange, final String routingKey, final Object message, final MessagePostProcessor messagePostProcessor) throws AmqpException { return convertSendAndReceive(exchange, routingKey, message, messagePostProcessor, null); }
@Override @Nullable public Object convertSendAndReceive(final Object message, final MessagePostProcessor messagePostProcessor) throws AmqpException { return convertSendAndReceive(message, messagePostProcessor, null); }
@Override @Nullable public Object convertSendAndReceive(final Object message, @Nullable CorrelationData correlationData) throws AmqpException { return convertSendAndReceive(this.exchange, this.routingKey, message, null, correlationData); }
@Override @Nullable public Object convertSendAndReceive(final Object message, final MessagePostProcessor messagePostProcessor, @Nullable CorrelationData correlationData) throws AmqpException { return convertSendAndReceive(this.exchange, this.routingKey, message, messagePostProcessor, correlationData); }
@Override @Nullable public Object convertSendAndReceive(final String routingKey, final Object message, @Nullable CorrelationData correlationData) throws AmqpException { return convertSendAndReceive(this.exchange, routingKey, message, null, correlationData); }
@Override @Nullable public Object convertSendAndReceive(final String exchange, final String routingKey, final Object message, @Nullable CorrelationData correlationData) throws AmqpException { return convertSendAndReceive(exchange, routingKey, message, null, correlationData); }
/** * Sends a message to a service that upcases the String and returns as a reply * using a {@link RabbitTemplate} configured with a fixed reply queue and * reply listener, configured with JavaConfig. We expect the reply to time * out and be sent to the DLQ. * @throws Exception the exception. */ @Test public void test() throws Exception { assertNull(this.rabbitTemplate.convertSendAndReceive("foo")); assertTrue(this.deadListener.latch.await(10, TimeUnit.SECONDS)); }
@Test @DirtiesContext public void returnExceptionWithRethrowAdapter() { this.rabbitTemplate.setMessageConverter(new RemoteInvocationAwareMessageConverterAdapter()); try { this.rabbitTemplate.convertSendAndReceive("test.return.exceptions", "foo"); fail("ExpectedException"); } catch (Exception e) { assertThat(e.getCause().getMessage(), equalTo("return this")); } }
@Test public void autoDeclareAnonWitAtts() { String received = (String) rabbitTemplate.convertSendAndReceive("auto.exch", "auto.anon.atts.rk", "foo"); assertThat(received, startsWith("foo:")); org.springframework.amqp.core.Queue anonQueueWithAttributes = new org.springframework.amqp.core.Queue(received.substring(4), true, true, true); this.rabbitAdmin.declareQueue(anonQueueWithAttributes); // will fail if atts not correctly set }
@Test public void testCglibProxy() { this.rabbitTemplate.setReplyTimeout(600000); Foo foo = new Foo(); foo.field = "foo"; assertEquals("Reply: foo: AUTO.RK.TEST", this.rabbitTemplate.convertSendAndReceive("auto.exch.test", "auto.rk.test", foo)); }
@Test public void simpleDirectEndpointWithConcurrency() { String reply = (String) rabbitTemplate.convertSendAndReceive("test.simple.direct2", "foo"); assertThat(reply, startsWith("FOOfoo")); assertThat(reply, containsString("rabbitClientThread-")); // container runs on client thread assertThat(TestUtils.getPropertyValue(this.registry.getListenerContainer("directWithConcurrency"), "consumersPerQueue"), equalTo(3)); }
@Test public void testTwoWay() throws Exception { assertEquals("FOO", this.rabbitTemplate.convertSendAndReceive(this.queue1.getName(), "foo")); Listener listener = this.harness.getSpy("foo"); assertNotNull(listener); verify(listener).foo("foo"); }
@Test public void testAsyncListener() throws Exception { assertEquals("FOO", this.rabbitTemplate.convertSendAndReceive(this.queue1.getName(), "foo")); RabbitConverterFuture<Object> future = this.asyncTemplate.convertSendAndReceive(this.queue1.getName(), "foo"); assertEquals("FOO", future.get(10, TimeUnit.SECONDS)); assertEquals("FOO", this.rabbitTemplate.convertSendAndReceive(this.queue2.getName(), "foo")); }
@Test public void testTwoWay() throws Exception { assertEquals("FOO", this.rabbitTemplate.convertSendAndReceive(this.queue1.getName(), "foo")); InvocationData invocationData = this.harness.getNextInvocationDataFor("foo", 10, TimeUnit.SECONDS); assertNotNull(invocationData); assertThat((String) invocationData.getArguments()[0], equalTo("foo")); assertThat((String) invocationData.getResult(), equalTo("FOO")); }
@Test public void testTwoWay() throws Exception { assertEquals("FOO", this.rabbitTemplate.convertSendAndReceive(this.queue1.getName(), "foo")); Listener listener = this.harness.getSpy("foo"); assertNotNull(listener); verify(listener).foo("foo"); InvocationData invocationData = this.harness.getNextInvocationDataFor("foo", 10, TimeUnit.SECONDS); assertNotNull(invocationData); assertThat((String) invocationData.getArguments()[0], equalTo("foo")); assertThat((String) invocationData.getResult(), equalTo("FOO")); }
@Test public void tx() { assertTrue(AopUtils.isJdkDynamicProxy(this.txService)); Baz baz = new Baz(); baz.field = "baz"; rabbitTemplate.setReplyTimeout(600000); assertEquals("BAZ: baz: auto.rk.tx", rabbitTemplate.convertSendAndReceive("auto.exch.tx", "auto.rk.tx", baz)); }
@Test public void autoSimpleDeclareAnonymousQueue() { final SimpleMessageListenerContainer container = (SimpleMessageListenerContainer) registry .getListenerContainer("anonymousQueue575"); assertThat(container.getQueueNames(), arrayWithSize(1)); assertEquals("viaAnonymous:foo", rabbitTemplate.convertSendAndReceive(container.getQueueNames()[0], "foo")); Object messageListener = container.getMessageListener(); assertThat(TestUtils.getPropertyValue(messageListener, "retryTemplate"), notNullValue()); assertThat(TestUtils.getPropertyValue(messageListener, "recoveryCallback"), notNullValue()); }