spring rabbitTemplate消息DeliveryMode设置

时间:2021-12-05 08:43:44

在用spring和rabbitmq整合 对message的DeliveryMode的设置有两种方式

一,在发送消息的时候设置DeliveryMode

1,生产者调用rabbimqTemplate发送消息的时候,调用接口

public Message sendAndReceive(final String exchange, final String routingKey, final Message message)
throws AmqpException {
return this.doSendAndReceive(exchange, routingKey, message);
}

2,对其中的参数Message设置MessageProperties属性

public class MessageProperties implements Serializable {

static final String DEFAULT_CONTENT_TYPE = CONTENT_TYPE_BYTES;

static final MessageDeliveryMode DEFAULT_DELIVERY_MODE = MessageDeliveryMode.PERSISTENT;//默认消息是持久化
          ......略
}

二,在spring和rabbitmq的配置文件中配置自定义MessageConverter

 */
public interface MessageConverter {

/**
* Convert a Java object to a Message.
* @param object the object to convert
* @param messageProperties The message properties.
* @return the Message
* @throws MessageConversionException in case of conversion failure
*/
Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException;

/**
* Convert from a Message to a Java object.
* @param message the message to convert
* @return the converted Java object
* @throws MessageConversionException in case of conversion failure
*/
Object fromMessage(Message message) throws MessageConversionException;

}


配置其默认实现

<bean class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
<property name="messageConverter" ref="">
</bean>

<bean class="org.springframework.amqp.support.converter.SimpleMessageConverter">
</bean

 

参考:http://docs.spring.io/spring-amqp/reference/html/amqp.html