List of usage examples for org.springframework.amqp.core Address getRoutingKey
public String getRoutingKey()
From source file:de.davidbilge.spring.remoting.amqp.service.AmqpServiceMessageListener.java
private void send(Object o, Address replyToAddress) { Message m = messageConverter.toMessage(o, new MessageProperties()); getAmqpTemplate().send(replyToAddress.getExchangeName(), replyToAddress.getRoutingKey(), m); }
From source file:com.github.larsq.spring.embeddedamqp.SimpleAmqpMessageContainer.java
void route(Address address, Message message) throws IOException { Preconditions.checkNotNull(address, "Address is not specified"); LOG.debug("route message {} to address {}", message.getEnvelope(), address); /**/*from www .j a v a2 s.c o m*/ * When address type is TOPIC, these */ if (Objects.equals(address.getExchangeType(), ExchangeTypes.TOPIC)) { topicExchangeRouter.route(null, address.getRoutingKey(), message); return; } ExchangeWrapper exchangeWrapper = exchange(address.getExchangeName()) .orElseThrow(Exception.exchangeNotFound(address.getExchangeName())); exchangeWrapper.counter.incrementAndGet(); exchangeWrapper.router.route(exchangeWrapper, address.getRoutingKey(), message); }
From source file:org.kurento.rabbitmq.RabbitTemplate.java
@SuppressWarnings("unchecked") private <R, S> boolean doReceiveAndReply(final String queueName, final ReceiveAndReplyCallback<R, S> callback, final ReplyToAddressCallback<S> replyToAddressCallback) throws AmqpException { return this.execute(new ChannelCallback<Boolean>() { @Override//from w w w . ja v a 2 s. com public Boolean doInRabbit(Channel channel) throws Exception { boolean channelTransacted = RabbitTemplate.this.isChannelTransacted(); GetResponse response = channel.basicGet(queueName, !channelTransacted); // Response can be null in the case that there is no message on // the queue. if (response != null) { long deliveryTag = response.getEnvelope().getDeliveryTag(); boolean channelLocallyTransacted = RabbitTemplate.this.isChannelLocallyTransacted(channel); if (channelLocallyTransacted) { channel.basicAck(deliveryTag, false); } else if (channelTransacted) { // Not locally transacted but it is transacted so it // could be synchronized with an external transaction ConnectionFactoryUtils.registerDeliveryTag(RabbitTemplate.this.getConnectionFactory(), channel, deliveryTag); } Message receiveMessage = RabbitTemplate.this.buildMessageFromResponse(response); Object receive = receiveMessage; if (!(ReceiveAndReplyMessageCallback.class.isAssignableFrom(callback.getClass()))) { receive = RabbitTemplate.this.getRequiredMessageConverter().fromMessage(receiveMessage); } S reply; try { reply = callback.handle((R) receive); } catch (ClassCastException e) { StackTraceElement[] trace = e.getStackTrace(); if (trace[0].getMethodName().equals("handle") && trace[1].getFileName().equals("RabbitTemplate.java")) { throw new IllegalArgumentException("ReceiveAndReplyCallback '" + callback + "' can't handle received object '" + receive + "'", e); } else { throw e; } } if (reply != null) { Address replyTo = replyToAddressCallback.getReplyToAddress(receiveMessage, reply); Message replyMessage = RabbitTemplate.this.convertMessageIfNecessary(reply); MessageProperties receiveMessageProperties = receiveMessage.getMessageProperties(); MessageProperties replyMessageProperties = replyMessage.getMessageProperties(); Object correlation = RabbitTemplate.this.correlationKey == null ? receiveMessageProperties.getCorrelationId() : receiveMessageProperties.getHeaders().get(RabbitTemplate.this.correlationKey); if (RabbitTemplate.this.correlationKey == null || correlation == null) { // using standard correlationId property if (correlation == null) { String messageId = receiveMessageProperties.getMessageId(); if (messageId != null) { correlation = messageId.getBytes(RabbitTemplate.this.encoding); } } replyMessageProperties.setCorrelationId((byte[]) correlation); } else { replyMessageProperties.setHeader(RabbitTemplate.this.correlationKey, correlation); } // 'doSend()' takes care about 'channel.txCommit()'. RabbitTemplate.this.doSend(channel, replyTo.getExchangeName(), replyTo.getRoutingKey(), replyMessage, null); } else if (channelLocallyTransacted) { channel.txCommit(); } return true; } return false; } }); }
From source file:org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.java
/** * Send the given response message to the given destination. * @param channel the Rabbit channel to operate on * @param replyTo the Rabbit ReplyTo string to use when sending. Currently interpreted to be the routing key. * @param messageIn the Rabbit message to send * @throws Exception if thrown by Rabbit API methods * @see #postProcessResponse(Message, Message) *//*from w w w. j ava 2 s . c o m*/ protected void sendResponse(Channel channel, Address replyTo, Message messageIn) throws Exception { Message message; if (this.replyPostProcessor == null) { message = messageIn; } else { message = this.replyPostProcessor.postProcessMessage(messageIn); } postProcessChannel(channel, message); try { this.logger.debug("Publishing response to exchange = [" + replyTo.getExchangeName() + "], routingKey = [" + replyTo.getRoutingKey() + "]"); channel.basicPublish(replyTo.getExchangeName(), replyTo.getRoutingKey(), this.mandatoryPublish, this.messagePropertiesConverter.fromMessageProperties(message.getMessageProperties(), this.encoding), message.getBody()); } catch (Exception ex) { throw RabbitExceptionTranslator.convertRabbitAccessException(ex); } }
From source file:org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.java
/** * Send the given response message to the given destination. * @param channel the Rabbit channel to operate on * @param replyTo the Rabbit ReplyTo string to use when sending. Currently interpreted to be the routing key. * @param message the Rabbit message to send * @throws Exception if thrown by Rabbit API methods * @see #postProcessResponse(Message, Message) *//*from www . j a v a 2 s . c o m*/ protected void sendResponse(Channel channel, Address replyTo, Message message) throws Exception { postProcessChannel(channel, message); try { logger.debug("Publishing response to exchanage = [" + replyTo.getExchangeName() + "], routingKey = [" + replyTo.getRoutingKey() + "]"); channel.basicPublish(replyTo.getExchangeName(), replyTo.getRoutingKey(), this.mandatoryPublish, this.immediatePublish, this.messagePropertiesConverter.fromMessageProperties(message.getMessageProperties(), encoding), message.getBody()); } catch (Exception ex) { throw RabbitUtils.convertRabbitAccessException(ex); } }