void setConsumerSpan(Span span, MessageProperties properties) { span.name("next-message").kind(CONSUMER); maybeTag(span, RABBIT_EXCHANGE, properties.getReceivedExchange()); maybeTag(span, RABBIT_ROUTING_KEY, properties.getReceivedRoutingKey()); maybeTag(span, RABBIT_QUEUE, properties.getConsumerQueue()); if (remoteServiceName != null) span.remoteServiceName(remoteServiceName); }
private void addConsumerMetadata(MessageProperties messageProperties, Map<String, Object> headers) { String consumerTag = messageProperties.getConsumerTag(); if (consumerTag != null) { headers.put(AmqpHeaders.CONSUMER_TAG, consumerTag); } String consumerQueue = messageProperties.getConsumerQueue(); if (consumerQueue != null) { headers.put(AmqpHeaders.CONSUMER_QUEUE, consumerQueue); } }
private void addConsumerMetadata(MessageProperties messageProperties, Map<String, Object> headers) { String consumerTag = messageProperties.getConsumerTag(); if (consumerTag != null) { headers.put(AmqpHeaders.CONSUMER_TAG, consumerTag); } String consumerQueue = messageProperties.getConsumerQueue(); if (consumerQueue != null) { headers.put(AmqpHeaders.CONSUMER_QUEUE, consumerQueue); } }
/** * Determine the name of the listener method that will handle the given message. * <p> * The default implementation first consults the * {@link #setQueueOrTagToMethodName(Map) queueOrTagToMethodName} map looking for a * match on the consumer queue or consumer tag; if no match found, it simply returns * the configured default listener method, or "handleMessage" if not configured. * @param originalMessage the Rabbit request message * @param extractedMessage the converted Rabbit request message, to be passed into the * listener method as argument * @return the name of the listener method (never <code>null</code>) * @see #setDefaultListenerMethod * @see #setQueueOrTagToMethodName */ protected String getListenerMethodName(Message originalMessage, Object extractedMessage) { if (this.queueOrTagToMethodName.size() > 0) { MessageProperties props = originalMessage.getMessageProperties(); String methodName = this.queueOrTagToMethodName.get(props.getConsumerQueue()); if (methodName == null) { methodName = this.queueOrTagToMethodName.get(props.getConsumerTag()); } if (methodName != null) { return methodName; } } return getDefaultListenerMethod(); }
/** * Determine the name of the listener method that will handle the given message. * <p> * The default implementation first consults the * {@link #setQueueOrTagToMethodName(Map) queueOrTagToMethodName} map looking for a * match on the consumer queue or consumer tag; if no match found, it simply returns * the configured default listener method, or "handleMessage" if not configured. * @param originalMessage the Rabbit request message * @param extractedMessage the converted Rabbit request message, to be passed into the * listener method as argument * @return the name of the listener method (never <code>null</code>) * @see #setDefaultListenerMethod * @see #setQueueOrTagToMethodName */ protected String getListenerMethodName(Message originalMessage, Object extractedMessage) { if (this.queueOrTagToMethodName.size() > 0) { MessageProperties props = originalMessage.getMessageProperties(); String methodName = this.queueOrTagToMethodName.get(props.getConsumerQueue()); if (methodName == null) { methodName = this.queueOrTagToMethodName.get(props.getConsumerTag()); } if (methodName != null) { return methodName; } } return getDefaultListenerMethod(); }
@Override public boolean isFatal(Throwable t) { if (t instanceof ListenerExecutionFailedException) { ListenerExecutionFailedException lefe = (ListenerExecutionFailedException) t; logger.error("Failed to process inbound message from queue " + lefe.getFailedMessage().getMessageProperties().getConsumerQueue() + "; failed message: " + lefe.getFailedMessage(), t); } return super.isFatal(t); }
this.routingKey != null ? this.routingKey : messageProperties.getConsumerQueue(), amqpMessage);
this.routingKey != null ? this.routingKey : messageProperties.getConsumerQueue(), amqpMessage);
@Override public void recover(final Message message, final Throwable cause) { Map<String, Object> headers = message.getMessageProperties().getHeaders(); headers.put("x-exception-stacktrace", ExceptionUtils.getFullStackTrace(cause)); headers.put("x-exception-message", ExceptionUtils.getMessage(cause)); headers.put("x-exception-root-cause-message", ExceptionUtils.getRootCauseMessage(cause)); headers.put("x-original-exchange", message.getMessageProperties().getReceivedExchange()); headers.put("x-original-routingKey", message.getMessageProperties().getReceivedRoutingKey()); headers.put("x-original-queue", message.getMessageProperties().getConsumerQueue()); headers.put("x-recover-time", new Date().toString()); String deadLetterExchangeName = rabbitmqProperties.getDeadLetterConfig().getDeadLetterExchange().getName(); String deadLetterRoutingKey = rabbitmqProperties.getDeadLetterConfig().createDeadLetterQueueName(message.getMessageProperties().getConsumerQueue()); headers.put("x-dead-letter-exchange", deadLetterExchangeName); headers.put("x-dead-letter-queue", deadLetterRoutingKey); if(headers.containsKey("correlation-id")) { message.getMessageProperties().setCorrelationIdString((String) headers.get("correlation-id")); } headers.putAll(loadAdditionalHeaders(message, cause)); for (MessageExceptionHandler messageExceptionHandler : messageExceptionHandlers) { try { messageExceptionHandler.handle(message, cause); } catch (Exception e) { // To catch any exception in the MessageExceptionHandler to avoid the interruption in other MessageExceptionHandlers log.error("Exception occurred while processing '{}' message exception handler.", messageExceptionHandler, e); } } this.errorTemplate.send(deadLetterExchangeName, deadLetterRoutingKey, message); log.warn("Republishing failed message to exchange '{}', routing key '{}', message {{}} , cause {}", deadLetterExchangeName, deadLetterRoutingKey, message, cause); }
headers.put(AmqpHeaders.CONSUMER_TAG, consumerTag); String consumerQueue = amqpMessageProperties.getConsumerQueue(); if (StringUtils.hasText(consumerQueue)) { headers.put(AmqpHeaders.CONSUMER_QUEUE, consumerQueue);
@Override public void onMessage(Message message, Channel channel) throws Exception { System.out.println(message.getMessageProperties().getConsumerQueue()); try { // 解析RabbitMQ消息体 String messageBody = new String(message.getBody()); MailMessageModel mailMessageModel = JSONObject.toJavaObject(JSONObject.parseObject(messageBody), MailMessageModel.class); // 发送邮件 String to = mailMessageModel.getTo(); String subject = mailMessageModel.getSubject(); String text = mailMessageModel.getText(); sendHtmlMail(to, subject, text); // 手动ACK channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); }catch (Exception e){ e.printStackTrace(); } }
void onReceive(MessageProperties messageProperties, Span span) { Tags.COMPONENT.set(span, RabbitMqTracingTags.RABBITMQ); RabbitMqTracingTags.EXCHANGE.set(span, messageProperties.getReceivedExchange()); RabbitMqTracingTags.MESSAGE_ID.set(span, messageProperties.getMessageId()); RabbitMqTracingTags.ROUTING_KEY.set(span, messageProperties.getReceivedRoutingKey()); RabbitMqTracingTags.CONSUMER_QUEUE.set(span, messageProperties.getConsumerQueue()); }
@Test public void testMessage1ArgDirect() throws Exception { this.latch.set(new CountDownLatch(1)); ListenableFuture<Message> future1 = this.asyncDirectTemplate.sendAndReceive(getFooMessage()); ListenableFuture<Message> future2 = this.asyncDirectTemplate.sendAndReceive(getFooMessage()); this.latch.get().countDown(); Message reply1 = checkMessageResult(future1, "FOO"); assertEquals(Address.AMQ_RABBITMQ_REPLY_TO, reply1.getMessageProperties().getConsumerQueue()); Message reply2 = checkMessageResult(future2, "FOO"); assertEquals(Address.AMQ_RABBITMQ_REPLY_TO, reply2.getMessageProperties().getConsumerQueue()); this.latch.set(null); waitForZeroInUseConsumers(); assertThat(TestUtils .getPropertyValue(this.asyncDirectTemplate, "directReplyToContainer.consumerCount", Integer.class), equalTo(2)); this.asyncDirectTemplate.stop(); this.asyncDirectTemplate.start(); assertThat(TestUtils .getPropertyValue(this.asyncDirectTemplate, "directReplyToContainer.consumerCount", Integer.class), equalTo(0)); }
assertEquals(3, messages.size()); assertEquals(consumerTag, messages.get(0).getMessageProperties().getConsumerTag()); assertEquals("foobar", messages.get(0).getMessageProperties().getConsumerQueue()); Executors.newSingleThreadExecutor().execute(() -> container.stop()); consumer.get().handleCancelOk(consumerTag);