参考:http://docs.spring.io/spring-amqp/docs/1.5.0.BUILD-SNAPSHOT/reference/html/_reference.html
一、连接工厂配置:
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("somehost");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
Connection connection = connectionFactory.createConnection();
When using XML, the configuration might look like this:
<bean id="connectionFactory"
class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<constructor-arg value="somehost"/>
<property name="username" value="guest"/>
<property name="password" value="guest"/></bean>
当使用SSL连接配置时,参考如下配置:
<rabbit:connection-factory id="rabbitConnectionFactory"
connection-factory="clientConnectionFactory"
host="${host}"
port="${port}"
virtual-host="${vhost}"
username="${username}" password="${password}" /><bean id="clientConnectionFactory"
class="org.springframework.xd.dirt.integration.rabbit.RabbitConnectionFactoryBean">
<property name="useSSL" value="true" />
<property name="sslPropertiesLocation" value="file:/secrets/rabbitSSL.properties"/></bean>
When using certificate validation, the property is a Spring Resource
pointing to a properties file containing the following keys:
keyStore=file:/secret/keycert.p12
trustStore=file:/secret/trustStore
keyStore.passPhrase=secret
trustStore.passPhrase=secret
The keyStore
and truststore
are Spring Resources
pointing to the stores. Typically this properties file will be secured by the operating system with the application having read access.
如果有多个RabbitMQ服务器,可以使用下面的配置,根据vhost发送队列消息
<bean id="connectionFactory"
class="org.springframework.amqp.rabbit.connection.SimpleRoutingConnectionFactory">
<property name="targetConnectionFactories">
<map>
<entry key="#{connectionFactory1.virtualHost}" ref="connectionFactory1"/>
<entry key="#{connectionFactory2.virtualHost}" ref="connectionFactory2"/>
</map>
</property></bean><rabbit:template id="template" connection-factory="connectionFactory" />
public class MyService {
@Autowired
private RabbitTemplate rabbitTemplate;
public void service(String vHost, String payload) {
SimpleResourceHolder.bind(rabbitTemplate.getConnectionFactory(), vHost);
rabbitTemplate.convertAndSend(payload);
SimpleResourceHolder.unbind(rabbitTemplate.getConnectionFactory());
}
}
It is important to unbind the resource after use. For more information see the JavaDocs of AbstractRoutingConnectionFactory
.
二、添加发送消息异常重试功能
Adding Retry Capabilities
Starting with version 1.3 you can now configure the RabbitTemplate
to use a RetryTemplate
to help with handling problems with broker connectivity. Refer to thespring-retry project for complete information; the following is just one example that uses an exponential back off policy and the default SimpleRetryPolicy
which will make three attempts before throwing the exception to the caller.
Using the XML namespace:
<rabbit:template id="template" connection-factory="connectionFactory" 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>
三、RabbitMQ的Exchange、Queue、Binding等的管理--》AmqpAdmin
The AmqpAdmin interface is based on using the Spring AMQP domain abstractions and is shown below:
public interface AmqpAdmin { // Exchange Operations
void declareExchange(Exchange exchange); void deleteExchange(String exchangeName); // Queue Operations
Queue declareQueue();
String declareQueue(Queue queue); void deleteQueue(String queueName); void deleteQueue(String queueName, boolean unused, boolean empty); void purgeQueue(String queueName, boolean noWait); // Binding Operations
void declareBinding(Binding binding); void removeBinding(Binding binding);
Properties getQueueProperties(String queueName);
}
The no-arg declareQueue() method defines a queue on the broker whose name is automatically generated. The additional properties of this auto-generated queue areexclusive=true
, autoDelete=true
, and durable=false
.
AmqpAdmin主要用于将不同的队列服务器,与不同的Queue 和 Exchange连接直来
<rabbit:admin id="admin1" connection-factory="CF1" />
<rabbit:admin id="admin2" connection-factory="CF2" />
<rabbit:queue id="declaredByBothAdminsImplicitly" />
<rabbit:queue id="declaredByBothAdmins" declared-by="admin1, admin2" />
<rabbit:queue id="declaredByAdmin1Only" declared-by="admin1" />
<rabbit:queue id="notDeclaredByAny" auto-declare="false" />
<rabbit:direct-exchange name="direct" declared-by="admin1, admin2">
<rabbit:bindings>
<rabbit:binding key="foo" queue="bar"/>
</rabbit:bindings>
</rabbit:direct-exchange>
四、声明交换器 Exchange
public interface Exchange {
String getName();
String getExchangeType();
boolean isDurable();
boolean isAutoDelete(); //为true,当声明Exchange的连接断开时,Exchange会被删除掉
Map<String, Object> getArguments();
}
exchangeType有以下几种类型:
Direct
, Topic
, Fanout
, and Headers
<!-- direct 交换器 ,auto-delete="true"时,当声明Exchange的连接断开时,Exchange会被删除掉 -->
<rabbit:direct-exchange id="exchange.direct"
name="exchange.direct" auto-delete="false" durable="true" declared-by="rabbitAdmin">
<rabbit:bindings>
<rabbit:binding key="amq.test.1" queue="amq.test.1"
exchange="" />
<rabbit:binding key="amq.test.2" queue="amq.test.2"
exchange="" />
<rabbit:binding key="amq.test.3" queue="amq.test.3"
exchange="" />
</rabbit:bindings>
</rabbit:direct-exchange>
<!-- direct 交换器 ,auto-delete="true"时,当声明Exchange的连接断开时,Exchange会被删除掉 -->
<rabbit:topic-exchange id="exchange.topic" name="exchange.topic"
auto-delete="false" durable="true" declared-by="rabbitAdmin">
<rabbit:bindings>
<rabbit:binding key="amq.test.1" queue="amq.test.1"
exchange="" />
<rabbit:binding key="amq.test.2" queue="amq.test.2"
exchange="" />
<rabbit:binding key="amq.test.3" queue="amq.test.3"
exchange="" />
</rabbit:bindings>
</rabbit:topic-exchange>
<!-- direct 交换器 ,auto-delete="true"时,当声明Exchange的连接断开时,Exchange会被删除掉 -->
<rabbit:fanout-exchange id="exchange.fanout"
name="exchange.fanout" auto-delete="false" durable="true" declared-by="rabbitAdmin">
<rabbit:bindings>
<rabbit:binding key="amq.test.1" queue="amq.test.1"
exchange="" />
<rabbit:binding key="amq.test.2" queue="amq.test.2"
exchange="" />
<rabbit:binding key="amq.test.3" queue="amq.test.3"
exchange="" />
</rabbit:bindings>
</rabbit:fanout-exchange>
五、声明队列 Queue
public class Queue {
private final String name;
private volatile boolean durable; //是否持久化
private volatile boolean exclusive; //排他性
private volatile boolean autoDelete; //自动删除
private volatile Map<String, Object> arguments; /**
* The queue is durable, non-exclusive and non auto-delete.
*
* @param name the name of the queue.
*/
public Queue(String name) { this(name, true, false, false);
}
<!-- auto-delete="true",在监听器退出时,声明的队列将会被删除;exclusive="true",排他锁,声明的队列只能被当前对象使用 -->
<rabbit:queue id="amq.test.1" name="amq.test.1"
declared-by="rabbitAdmin" auto-delete="false" exclusive="false"
durable="true" />
<!-- -->
<rabbit:queue id="amq.test.2" name="amq.test.2"
declared-by="rabbitAdmin" auto-delete="false" exclusive="false"
durable="true" />
<!-- -->
<rabbit:queue id="amq.test.3" name="amq.test.3"
declared-by="rabbitAdmin" auto-delete="false" exclusive="false"
durable="true" />
六、声明 交换器 与 队列的绑定 Binding
You can bind a Queue to a DirectExchange with a fixed routing key.
new Binding(someQueue, someDirectExchange, "foo.bar")
You can bind a Queue to a TopicExchange with a routing pattern.
new Binding(someQueue, someTopicExchange, "foo.*")
You can bind a Queue to a FanoutExchange with no routing key.
new Binding(someQueue, someFanoutExchange)
We also provide a BindingBuilder
to facilitate a "fluent API" style.
Binding b = BindingBuilder.bind(someQueue).to(someTopicExchange).with("foo.*");