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

The following examples show how to use org.springframework.amqp.core.MessageProperties#setExpiration() . 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: 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 2
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;
    }