Spring-rabit説明ドキュメントメモ(1)

10425 ワード

参考:http://docs.spring.io/spring-amqp/docs/1.5.0.BUILD-SNAPSHOT/reference/html/_reference.
一、工場を連結する配置:
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("somehost");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");

Connection connection = connectionFactory.createConnection();
When using XML,the configration 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.Typeically this properties file will be secured by the operating system with the appliation having read access.
複数のRabbiitMQサーバがある場合、以下の構成を使用して、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 importt to unbind the resource after use.For more information see the Java Docs of  AbstractRoutingConnectionFactory.
二、送信メッセージ異常再試行機能を追加する。
Adding Retry Capability
スターリングwith version 1.3 you can now configure the  RabbitTemplate to use a  RetryTemplate トhelp with handling problems with breoker connectivity.Refer to thespring-retry project for complettee information;the follwing is just one example that uses an exponential back off policy and the default  SimpleRetryPolicy which will make three atempts before throwing the exception to the caler.
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>
三、RabbiitMQのExchange、Que、Bindingなどの管理--」AmqpAdmin 
The AmqpAdmin interface is based on using the Spring AMQP domann 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 decleaQue()method defines a queue on the breoker whose name is auttiallygenerated.The additional properties of this at-generand queue are exclusive=true、  autoDelete=true、and  durable=false.
AmqpAdminは主に異なるキューサーバを、異なるQueと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<!--ディレクトスイッチ、aut-delete="true"は、Exchangeの接続が切断されたと宣言すると、Exchangeが削除されます。
<rabbit:direct-exchange id=「exchange.direct」
name=「exchange.direct」aut-delete=「false」durable=「true」declead-by=「rabitAdmin」
<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="/>


<!--ディレクトスイッチ、aut-delete="true"は、Exchangeの接続が切断されたと宣言すると、Exchangeが削除されます。
<rabbit:topic-exchange id=「exchange.topic」name=「exchange.topic」
aut-delete=「false」durable=「true」declead-by=「rabitAdmin」
<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="/>


<!--ディレクトスイッチ、aut-delete="true"は、Exchangeの接続が切断されたと宣言すると、Exchangeが削除されます。
<rabbit:fanout-exchange id=「exchange.fanout」
name=「exchange.fanout」aut-delete=「false」durable=「true」declead-by=「rabitAdmin」
<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="/>


五、声明列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);
   }
    
<!--aut-delete="true"は、モニターが終了すると、声明のキューが削除されます。exclusive=「true」はロックされています。声明の列は現在のオブジェクトにしか使えません。
<rabbit:queue id=「amq.test.1」name=「amq.test.1」
declared-by=「rabitAdmin」aut-delete=「false」exclusive=「false」
durable=「true」/>
<!-->
<rabbit:queue id=「amq.test.2」name=「amq.test.2」
declared-by=「rabitAdmin」aut-delete=「false」exclusive=「false」
durable=「true」/>
<!-->
<rabbit:queue id=「amq.test.3」name=「amq.test.3」
declared-by=「rabitAdmin」aut-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 Faout Exchange with no routing key.
new Binding(someQueue, someFanoutExchange)
We also provide a  BindingBuilder トfacilitate a「flunt API」style.
Binding b = BindingBuilder.bind(someQueue).to(someTopicExchange).with("foo.*");