/** * The {@code defaultReplyTo} address with the form * <pre class="code"> * (exchange)/(routingKey) * </pre> * or * <pre class="code"> * (queueName) * </pre> * if the request message doesn't have a {@code replyTo} property. * The second form uses the default exchange ("") and the queue name as * the routing key. * @param defaultReplyTo the default {@code replyTo} address to use. * @since 4.2 * @see Address */ public void setDefaultReplyTo(String defaultReplyTo) { this.defaultReplyTo = new Address(defaultReplyTo); }
String replyToProperty = message.getMessageProperties().getReplyTo(); if (replyToProperty != null) { replyTo = new Address(replyToProperty);
public Address getReplyToAddress() { return (this.replyTo != null) ? new Address(this.replyTo) : null; }
/** * The {@code defaultReplyTo} address with the form * <pre class="code"> * (exchange)/(routingKey) * </pre> * or * <pre class="code"> * (queueName) * </pre> * if the request message doesn't have a {@code replyTo} property. * The second form uses the default exchange ("") and the queue name as * the routing key. * @param defaultReplyTo the default {@code replyTo} address to use. * @since 4.2 * @see Address */ public void setDefaultReplyTo(String defaultReplyTo) { this.defaultReplyTo = new Address(defaultReplyTo); }
@Override public <R, S> boolean receiveAndReply(final String queueName, ReceiveAndReplyCallback<R, S> callback, final String replyExchange, final String replyRoutingKey) throws AmqpException { return receiveAndReply(queueName, callback, (request, reply) -> new Address(replyExchange, replyRoutingKey)); }
@Override public <R, S> boolean receiveAndReply(final String queueName, ReceiveAndReplyCallback<R, S> callback, final String replyExchange, final String replyRoutingKey) throws AmqpException { return receiveAndReply(queueName, callback, (request, reply) -> new Address(replyExchange, replyRoutingKey)); }
@Test public void verifyLifeCycle() { Object gateway = context.getBean("autoStartFalseGateway"); assertEquals(Boolean.FALSE, TestUtils.getPropertyValue(gateway, "autoStartup")); assertEquals(123, TestUtils.getPropertyValue(gateway, "phase")); assertFalse(TestUtils.getPropertyValue(gateway, "messageListenerContainer.missingQueuesFatal", Boolean.class)); Object amqpTemplate = context.getBean("amqpTemplate"); assertSame(amqpTemplate, TestUtils.getPropertyValue(gateway, "amqpTemplate")); Address defaultReplyTo = TestUtils.getPropertyValue(gateway, "defaultReplyTo", Address.class); Address expected = new Address("fooExchange/barRoutingKey"); assertEquals(expected.getExchangeName(), defaultReplyTo.getExchangeName()); assertEquals(expected.getRoutingKey(), defaultReplyTo.getRoutingKey()); assertEquals(expected, defaultReplyTo); }
private Address evaluateReplyTo(Message request, Object source, Object result, Expression expression) { Address replyTo = null; Object value = expression.getValue(this.evalContext, new ReplyExpressionRoot(request, source, result)); Assert.state(value instanceof String || value instanceof Address, "response expression must evaluate to a String or Address"); if (value instanceof String) { replyTo = new Address((String) value); } else { replyTo = (Address) value; } return replyTo; }
private Address evaluateReplyTo(Message request, Object source, Object result, Expression expression) { Address replyTo = null; Object value = expression.getValue(this.evalContext, new ReplyExpressionRoot(request, source, result)); Assert.state(value instanceof String || value instanceof Address, "response expression must evaluate to a String or Address"); if (value instanceof String) { replyTo = new Address((String) value); } else { replyTo = (Address) value; } return replyTo; }
/** * Set the default replyTo address to use when sending response messages. * This is only used if the replyTo from the received message is null. * <p> * Response destinations are only relevant for listener methods * that return result objects, which will be wrapped in * a response message and sent to a response destination. * <p> * It can be a string surrounded by "!{...}" in which case the expression is * evaluated at runtime; see the reference manual for more information. * @param defaultReplyTo The exchange. * @since 1.6 */ public void setResponseAddress(String defaultReplyTo) { if (defaultReplyTo.startsWith(PARSER_CONTEXT.getExpressionPrefix())) { this.responseExpression = PARSER.parseExpression(defaultReplyTo, PARSER_CONTEXT); } else { this.responseAddress = new Address(defaultReplyTo); } }
/** * Set the default replyTo address to use when sending response messages. * This is only used if the replyTo from the received message is null. * <p> * Response destinations are only relevant for listener methods * that return result objects, which will be wrapped in * a response message and sent to a response destination. * <p> * It can be a string surrounded by "!{...}" in which case the expression is * evaluated at runtime; see the reference manual for more information. * @param defaultReplyTo The exchange. * @since 1.6 */ public void setResponseAddress(String defaultReplyTo) { if (defaultReplyTo.startsWith(PARSER_CONTEXT.getExpressionPrefix())) { this.responseExpression = PARSER.parseExpression(defaultReplyTo, PARSER_CONTEXT); } else { this.responseAddress = new Address(defaultReplyTo); } }
@Test public void toStringCheck() { Address address = new Address("my-exchange", "routing-key"); String replyToUri = "my-exchange/routing-key"; Assert.assertEquals(replyToUri, address.toString()); }
@Test public void parseUnstructuredWithRoutingKeyOnly() { Address address = new Address("my-routing-key"); assertEquals("my-routing-key", address.getRoutingKey()); assertEquals("/my-routing-key", address.toString()); address = new Address("/foo"); assertEquals("foo", address.getRoutingKey()); assertEquals("/foo", address.toString()); address = new Address("bar/baz"); assertEquals("bar", address.getExchangeName()); assertEquals("baz", address.getRoutingKey()); assertEquals("bar/baz", address.toString()); }
@Test public void parse() { String replyToUri = "direct://my-exchange/routing-key"; Address address = new Address(replyToUri); assertEquals("my-exchange", address.getExchangeName()); assertEquals("routing-key", address.getRoutingKey()); }
@Override public Object convertSendAndReceive(Object payload) throws AmqpException { Object[] arguments = ((RemoteInvocation) payload).getArguments(); if (arguments.length == 1 && arguments[0].equals("timeout")) { return null; } MessageConverter messageConverter = serviceExporter.getMessageConverter(); Address replyTo = new Address("fakeExchangeName", "fakeRoutingKey"); MessageProperties messageProperties = new MessageProperties(); messageProperties.setReplyToAddress(replyTo); Message message = messageConverter.toMessage(payload, messageProperties); serviceExporter.onMessage(message); Message resultMessage = sentSavingTemplate.getLastMessage(); return messageConverter.fromMessage(resultMessage); } };
@Test public void testEmpty() { Address address = new Address("/"); assertEquals("", address.getExchangeName()); assertEquals("", address.getRoutingKey()); assertEquals("/", address.toString()); }
@Test public void parseWithoutRoutingKey() { Address address = new Address("fanout://my-exchange"); assertEquals("my-exchange", address.getExchangeName()); assertEquals("", address.getRoutingKey()); assertEquals("my-exchange/", address.toString()); }
@Test public void parseWithDefaultExchangeAndRoutingKey() { Address address = new Address("direct:///routing-key"); assertEquals("", address.getExchangeName()); assertEquals("routing-key", address.getRoutingKey()); assertEquals("/routing-key", address.toString()); }
@Test public void processAndReplyUsingReplyTo() throws Exception { MessagingMessageListenerAdapter listener = createDefaultInstance(String.class); listener.setMandatoryPublish(true); String body = "echo text"; Address replyTo = new Address("replyToQueue", "myRouting"); MessageProperties properties = new MessageProperties(); properties.setReplyToAddress(replyTo); org.springframework.amqp.core.Message message = MessageTestUtils.createTextMessage(body, properties); processAndReply(listener, message, "replyToQueue", "myRouting", true, null); assertDefaultListenerMethodInvocation(); }