在将消息发布到Rabbit MQ Exchange时,很少有消息丢失。例如,发布了10条消息,只有5条消息被传送到绑定队列,其余5条消息丢失。请指教。
以下是用于连接的Spring XML配置
<bean id="cachingConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory" destroy-method="destroy">
<property name="addresses" value="example.com:8080"/>
<property name="username" value="guest" />
<property name="password" value="guest" />
<property name="virtualHost" value="guest-app" />
<property name="connectionTimeout" value="1000"/>
<property name="cacheMode" value="#{T(org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode).CONNECTION}"/>
<property name="channelCacheSize" value="6000"/>
<property name="channelCheckoutTimeout" value="60000"/>
<property name="requestedHeartBeat" value="30"/>
<property name="publisherConfirms" value="true"/>
<property name="publisherReturns" value="true"/>
</bean>
以下是Rabbit Template Config
<rabbit:template id="amqpTemplate" connection-factory="cachingConnectionFactory" mandatory="true" retry-template="retryTemplate" />
<bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
<property name="backOffPolicy">
<bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
<property name="initialInterval" value="500" />
<property name="multiplier" value="10.0" />
<property name="maxInterval" value="10000" />
</bean>
</property>
</bean>
<rabbit:admin connection-factory="cachingConnectionFactory" />
以下是发布消息
int:chain input-channel="output">
<int:header-enricher>
<int:header name="amqp_deliveryMode" value="PERSISTENT" type="org.springframework.amqp.core.MessageDeliveryMode"/>
</int:header-enricher>
<int-amqp:outbound-channel-adapter id="outboundAmqp" amqp-template="amqpTemplate" exchange-name-expression="headers['exchange']" routing-key-expression="headers['routingKey']" mapped-request-headers="*" confirm-correlation-expression="payload" confirm-ack-channel="msgResponse" confirm-nack-channel="msgResponse" return-channel="msgResponse"/>