Java Code Examples for org.springframework.amqp.core.MessageProperties#setMessageId()

The following examples show how to use org.springframework.amqp.core.MessageProperties#setMessageId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractMqMessageConverter.java    From zxl with Apache License 2.0 6 votes vote down vote up
protected <T> Message toMessage(T object, String correlationId, boolean isOnlySend) {
	if (object == null) {
		return null;
	}
	if (object instanceof Message) {
		return (Message) object;
	}
	MessageProperties messageProperties = new MessageProperties();
	messageProperties.setMessageId(UUID.randomUUID().toString());
	messageProperties.setContentEncoding(DEFAULT_CHARSET);
	try {
		if (!isOnlySend) {
			if (correlationId == null) {
				messageProperties.setCorrelationId(UUID.randomUUID().toString().getBytes(DEFAULT_CHARSET));
			} else {
				messageProperties.setCorrelationId(correlationId.getBytes(DEFAULT_CHARSET));
			}
		}
		byte[] bytes = getBytesAndSetMessageProperties(object, messageProperties);
		return new Message(bytes, messageProperties);
	} catch (Exception warnException) {
		throw new RuntimeException("#####��Ϣת��ʧ��", warnException);
	}
}
 
Example 2
Source File: RabbitMqSendTracingAspectTest.java    From java-spring-rabbitmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testTraceRabbitSend_whenNoPropertiesHeaders() throws Throwable {
  // given
  String exchange = "opentracing.event.exchange";
  String routingKey = ROUTING_KEY;

  Span span = mockTracer.buildSpan("test").start();

  mockTracer.scopeManager().activate(span);

  TestMessage<String> myMessage = new TestMessage<>("");

  Object[] args = new Object[] {exchange, routingKey, myMessage};
  given(proceedingJoinPoint.getArgs()).willReturn(args);

  MessageProperties properties = new MessageProperties();
  properties.setReceivedExchange("exchange");
  properties.setReceivedRoutingKey("routingKey");
  properties.setMessageId("messageId");
  Message message = new Message("".getBytes(), properties);
  given(messageConverter.toMessage(any(Object.class), any(MessageProperties.class)))
      .willReturn(message);

  // when
  aspect.traceRabbitSend(proceedingJoinPoint, exchange, routingKey, myMessage);

  // then
  verify(proceedingJoinPoint).getArgs();
  verify(messageConverter).toMessage(any(Object.class), any(MessageProperties.class));
  verify(proceedingJoinPoint).proceed(args);
}
 
Example 3
Source File: RabbitMqReceiveTracingInterceptorTest.java    From java-spring-rabbitmq with Apache License 2.0 5 votes vote down vote up
private Message getMessage() {
  final MessageProperties messageProperties = new MessageProperties();
  messageProperties.setReceivedExchange("exchange");
  messageProperties.setReceivedRoutingKey("routingKey");
  messageProperties.setMessageId("messageId");
  return new Message("".getBytes(Charset.defaultCharset()), messageProperties);
}
 
Example 4
Source File: RabbitmqTemplate.java    From shine-mq with Apache License 2.0 5 votes vote down vote up
private Object send(String exchangeName, Object msg, MessageConverter messageConverter, SendTypeEnum type,
                    String routingKey, int expiration, int priority) throws Exception {
    check(exchangeName, routingKey);

    Object obj = null;
    String msgId = UUID.randomUUID().toString();
    EventMessage eventMessage = new EventMessage(exchangeName, routingKey, type.toString(), msg, null, msgId);
    MessageProperties messageProperties = new MessageProperties();
    //过期时间
    if (expiration > 0) {
        messageProperties.setExpiration(String.valueOf(expiration));
    }
    //消息优先级
    if (priority > 0) {
        messageProperties.setPriority(priority);
    }
    messageProperties.setMessageId(msgId);
    // 设置消息持久化
    messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
    Message message = messageConverter.toMessage(eventMessage, messageProperties);
    rabbitmqFactory.setCorrelationData(msgId, null, eventMessage, null);
    try {
        if (SendTypeEnum.RPC.equals(type)) {
            obj = eventAmqpTemplate.convertSendAndReceive(routingKey, message);
        } else {
            eventAmqpTemplate.send(exchangeName, routingKey, message);
        }
    } catch (AmqpException e) {
        logger.error("send event fail. Event Message : [{}]", eventMessage, e);
        throw new Exception("send event fail", e);
    }
    return obj;
}
 
Example 5
Source File: RabbitmqTemplate.java    From shine-mq with Apache License 2.0 5 votes vote down vote up
private Object sendWithEM(EventMessage eventMessage, int expiration, int priority, SendTypeEnum type) throws Exception {

        Object obj = null;
        MessageProperties messageProperties = new MessageProperties();
        //过期时间
        if (expiration > 0) {
            messageProperties.setExpiration(String.valueOf(expiration));
        }
        //消息优先级
        if (priority > 0) {
            messageProperties.setPriority(priority);
        }
        messageProperties.setMessageId(eventMessage.getMessageId());
        // 设置消息持久化
        messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        Message message = messageConverter.toMessage(eventMessage, messageProperties);
        rabbitmqFactory.setCorrelationData(eventMessage.getMessageId(), eventMessage.getCoordinator(), eventMessage, null);
        try {
            if (SendTypeEnum.RPC.equals(type)) {
                obj = eventAmqpTemplate.convertSendAndReceive(eventMessage.getRoutingKey(), message);
            } else {
                eventAmqpTemplate.send(eventMessage.getExchangeName(), eventMessage.getRoutingKey(), message);
            }
        } catch (AmqpException e) {
            logger.error("send event fail. Event Message : [{}]", eventMessage, e);
            throw new Exception("send event fail", e);
        }
        return obj;
    }