headers.put(AmqpHeaders.MESSAGE_COUNT, messageCount); String messageId = amqpMessageProperties.getMessageId(); if (StringUtils.hasText(messageId)) { headers.put(AmqpHeaders.MESSAGE_ID, messageId);
@Test public void fromHeadersFallbackIdTimestamp() { DefaultAmqpHeaderMapper headerMapper = DefaultAmqpHeaderMapper.outboundMapper(); org.springframework.messaging.Message<?> message = new GenericMessage<>(""); MessageProperties messageProperties = new MessageProperties(); headerMapper.fromHeadersToRequest(message.getHeaders(), messageProperties); assertThat(message.getHeaders().getId().toString()).isEqualTo(messageProperties.getMessageId()); assertThat(message.getHeaders().getTimestamp()).isEqualTo(messageProperties.getTimestamp().getTime()); }
assertEquals("test.expiration", amqpProperties.getExpiration()); assertEquals(new Integer(42), amqpProperties.getMessageCount()); assertEquals("test.messageId", amqpProperties.getMessageId()); assertEquals("test.receivedExchange", amqpProperties.getReceivedExchange()); assertEquals("test.receivedRoutingKey", amqpProperties.getReceivedRoutingKey());
public MessageBuilderSupport<T> setMessageIdIfAbsent(String messageId) { if (this.properties.getMessageId() == null) { this.properties.setMessageId(messageId); } return this; }
@Override public Object getKey(Message message) { if (message.getMessageProperties().getMessageId() != null) { return message.getMessageProperties().getMessageId(); } else { return UNDEFINED_MESSAGE_KEY; } } }
@Override public Object getKey(Message message) { if (message.getMessageProperties().getMessageId() != null) { return message.getMessageProperties().getMessageId(); } else { return UNDEFINED_MESSAGE_KEY; } } }
@Override public final Message toMessage(Object object, @Nullable MessageProperties messagePropertiesArg, @Nullable Type genericType) throws MessageConversionException { MessageProperties messageProperties = messagePropertiesArg; if (messageProperties == null) { messageProperties = new MessageProperties(); } Message message = createMessage(object, messageProperties, genericType); messageProperties = message.getMessageProperties(); if (this.createMessageIds && messageProperties.getMessageId() == null) { messageProperties.setMessageId(UUID.randomUUID().toString()); } return message; }
@Override public void onMessage(Message message, Channel channel) throws Exception { MessageProperties messageProperties = message.getMessageProperties(); Long deliveryTag = messageProperties.getDeliveryTag(); Long consumerCount = redisTemplate.opsForHash().increment(MQConstants.MQ_CONSUMER_RETRY_COUNT_KEY, messageProperties.getMessageId(), 1); logger.info("收到消息,当前消息ID:{} 消费次数:{}", messageProperties.getMessageId(), consumerCount); try { receiveMessage(message); // 成功的回执 channel.basicAck(deliveryTag, false); // 如果消费成功,将Redis中统计消息消费次数的缓存删除 redisTemplate.opsForHash().delete(MQConstants.MQ_CONSUMER_RETRY_COUNT_KEY, messageProperties.getMessageId()); } catch (Exception e) { logger.error("RabbitMQ 消息消费失败," + e.getMessage(), e); if (consumerCount >= MQConstants.MAX_CONSUMER_COUNT) { // 入死信队列 channel.basicReject(deliveryTag, false); } else { // 重回到队列,重新消费, 按照2的指数级递增 Thread.sleep((long) (Math.pow(MQConstants.BASE_NUM, consumerCount)*1000)); redisTemplate.opsForHash().increment(MQConstants.MQ_CONSUMER_RETRY_COUNT_KEY, messageProperties.getMessageId(), 1); channel.basicNack(deliveryTag, false, true); } } }
void onSend(MessageProperties messageProperties, String exchange, String routingKey, Span span) { Tags.COMPONENT.set(span, RabbitMqTracingTags.RABBITMQ); RabbitMqTracingTags.EXCHANGE.set(span, exchange); RabbitMqTracingTags.MESSAGE_ID.set(span, messageProperties.getMessageId()); RabbitMqTracingTags.ROUTING_KEY.set(span, routingKey); }
public Object getId() { if (this.message == null || this.getMessageProperties() == null) { throw new IllegalStateException("No MessageProperties received"); } return this.message.getMessageProperties().getMessageId(); }
public static SpringAMQPMessage setBasicPropertiesToHeaders(SpringAMQPMessage msg, Message amqpMessage) { msg.getHeaders().put(MESSAGE_ID, amqpMessage.getMessageProperties().getMessageId()); byte[] correlationId = amqpMessage.getMessageProperties().getCorrelationId(); msg.getHeaders().put(CORRELATION_ID, correlationId == null ? null : new String(correlationId)); msg.getHeaders().put(CONTENT_ENCODING, amqpMessage.getMessageProperties().getContentEncoding()); msg.getHeaders().put(CONTENT_TYPE, amqpMessage.getMessageProperties().getContentType()); msg.getHeaders().put(EXPIRATION, amqpMessage.getMessageProperties().getExpiration()); msg.getHeaders().put(PRIORITY, amqpMessage.getMessageProperties().getPriority()); msg.getHeaders().put(REPLY_TO, amqpMessage.getMessageProperties().getReplyTo()); msg.getHeaders().put(DELIVERY_MODE, MessageDeliveryMode.toInt(amqpMessage.getMessageProperties().getDeliveryMode())); msg.getHeaders().put(TYPE, amqpMessage.getMessageProperties().getType()); return msg; }
public static SpringAMQPMessage setBasicPropertiesToHeaders(SpringAMQPMessage msg, Message amqpMessage) { msg.getHeaders().put(MESSAGE_ID, amqpMessage.getMessageProperties().getMessageId()); byte[] correlationId = amqpMessage.getMessageProperties().getCorrelationId(); msg.getHeaders().put(CORRELATION_ID, correlationId == null ? null : new String(correlationId)); msg.getHeaders().put(CONTENT_ENCODING, amqpMessage.getMessageProperties().getContentEncoding()); msg.getHeaders().put(CONTENT_TYPE, amqpMessage.getMessageProperties().getContentType()); msg.getHeaders().put(EXPIRATION, amqpMessage.getMessageProperties().getExpiration()); msg.getHeaders().put(PRIORITY, amqpMessage.getMessageProperties().getPriority()); msg.getHeaders().put(REPLY_TO, amqpMessage.getMessageProperties().getReplyTo()); msg.getHeaders().put(DELIVERY_MODE, MessageDeliveryMode.toInt(amqpMessage.getMessageProperties().getDeliveryMode())); msg.getHeaders().put(TYPE, amqpMessage.getMessageProperties().getType()); return msg; }
/** * Post-process the given response message before it will be sent. * <p> * The default implementation sets the response's correlation id to the request message's correlation id, if any; * otherwise to the request message id. * @param request the original incoming Rabbit message * @param response the outgoing Rabbit message about to be sent */ protected void postProcessResponse(Message request, Message response) { String correlation = request.getMessageProperties().getCorrelationId(); if (correlation == null) { String messageId = request.getMessageProperties().getMessageId(); if (messageId != null) { correlation = messageId; } } response.getMessageProperties().setCorrelationId(correlation); }
/** * Post-process the given response message before it will be sent. * <p> * The default implementation sets the response's correlation id to the request message's correlation id, if any; * otherwise to the request message id. * @param request the original incoming Rabbit message * @param response the outgoing Rabbit message about to be sent */ protected void postProcessResponse(Message request, Message response) { String correlation = request.getMessageProperties().getCorrelationId(); if (correlation == null) { String messageId = request.getMessageProperties().getMessageId(); if (messageId != null) { correlation = messageId; } } response.getMessageProperties().setCorrelationId(correlation); }
/** * Callback for processing a received Rabbit message. * <p>Implementors are supposed to process the given Message, * typically sending reply messages through the given Session. * @param message the received AMQP message (never <code>null</code>) * @param channel the underlying Rabbit Channel (never <code>null</code>) * @throws Exception Any. */ @Override public void onMessage(Message message, Channel channel) throws Exception { MessageProperties messageProperties = message.getMessageProperties(); // 消息体 String messageBody = new String(message.getBody()); logger.warn("dead letter message:{} | tag:{}", messageBody, message.getMessageProperties().getDeliveryTag()); /*// 入库 insertRecord(logKey, message); // 发邮件 sendEmail(logKey, messageProperties.getMessageId(), messageBody);*/ channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); redisTemplate.opsForHash().delete(MQConstants.MQ_CONSUMER_RETRY_COUNT_KEY, messageProperties.getMessageId()); }
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 noMessageIdByDefault() throws Exception { SimpleMessageConverter converter = new SimpleMessageConverter(); Message message = converter.toMessage("foo", null); assertNull(message.getMessageProperties().getMessageId()); }
@Test public void optionalMessageId() throws Exception { SimpleMessageConverter converter = new SimpleMessageConverter(); converter.setCreateMessageIds(true); Message message = converter.toMessage("foo", null); assertNotNull(message.getMessageProperties().getMessageId()); }
private void assertLower(MessageProperties properties) { assertEquals("appId", properties.getAppId()); assertEquals("clusterId", properties.getClusterId()); assertEquals("contentEncoding", properties.getContentEncoding()); assertEquals(MessageProperties.CONTENT_TYPE_TEXT_PLAIN, properties.getContentType()); assertEquals(1, properties.getContentLength()); assertEquals("correlationId", properties.getCorrelationId()); assertEquals(MessageDeliveryMode.NON_PERSISTENT, properties.getDeliveryMode()); assertEquals(2, properties.getDeliveryTag()); assertEquals("expiration", properties.getExpiration()); assertEquals("bar", properties.getHeaders().get("foo")); assertEquals("fiz", properties.getHeaders().get("qux")); assertEquals("fuz", properties.getHeaders().get("baz")); assertEquals(Integer.valueOf(3), properties.getMessageCount()); assertEquals("messageId", properties.getMessageId()); assertEquals(Integer.valueOf(4), properties.getPriority()); assertEquals("receivedExchange", properties.getReceivedExchange()); assertEquals("receivedRoutingKey", properties.getReceivedRoutingKey()); assertTrue(properties.getRedelivered()); assertTrue(properties.getTimestamp().getTime() > 0); assertEquals("type", properties.getType()); assertEquals("userId", properties.getUserId()); }
private void assertUpper(MessageProperties properties) { assertEquals("APPID", properties.getAppId()); assertEquals("CLUSTERID", properties.getClusterId()); assertEquals("CONTENTENCODING", properties.getContentEncoding()); assertEquals(MessageProperties.CONTENT_TYPE_BYTES, properties.getContentType()); assertEquals(10, properties.getContentLength()); assertEquals("CORRELATIONID", properties.getCorrelationId()); assertEquals(MessageDeliveryMode.PERSISTENT, properties.getDeliveryMode()); assertEquals(20, properties.getDeliveryTag()); assertEquals("EXPIRATION", properties.getExpiration()); assertEquals("BAR", properties.getHeaders().get("foo")); assertEquals("FIZ", properties.getHeaders().get("qux")); assertEquals("FUZ", properties.getHeaders().get("baz")); assertEquals(Integer.valueOf(30), properties.getMessageCount()); assertEquals("MESSAGEID", properties.getMessageId()); assertEquals(Integer.valueOf(40), properties.getPriority()); assertEquals("RECEIVEDEXCHANGE", properties.getReceivedExchange()); assertEquals("RECEIVEDROUTINGKEY", properties.getReceivedRoutingKey()); assertFalse(properties.getRedelivered()); assertTrue(properties.getTimestamp().getTime() == 0); assertEquals("TYPE", properties.getType()); assertEquals("USERID", properties.getUserId()); }