The Fanout configuration based on the spring AMQP RABBITMQ is as follows:
Publishing side
<rabbit:connection-factory id= "Rabbitconnectionfactory" Username= "Guest" password= "Guest" host= "localhost" port= "5672"/>
<rabbit:template id= "Amqptemplate" connection-factory= "Rabbitconnectionfactory" exchange= "Fanout-mq-exchange" message-converter= "Jsonmessageconverter"/>
<rabbit:admin id= "admin" connection-factory= "rabbitconnectionfactory"/>
<bean id= "Jsonmessageconverter" class= " Org.springframework.amqp.support.converter.Jackson2JsonMessageConverter "/>
<rabbit:queue id= "Fanout_queue" name= "Fanout_queue" durable= "true" auto-delete= "false" exclusive= "false"/>
<rabbit:queue id= "fanout_queue1" name= "fanout_queue1" durable= "true" auto-delete= "false" exclusive= "false"/>
<rabbit:queue id= "fanout_queue2" name= "Fanout_queue2" durable= "true" auto-delete= "false" exclusive= "false"/>
<rabbit:fanout-exchange id= "Fanout-mq-exchange" name= "Fanout-mq-exchange" durable= "true" auto-delete= "false" >
<rabbit:bindings>
<rabbit:binding queue= "Fanout_queue"/>
<rabbit:binding queue= "Fanout_queue1"/>
<rabbit:binding queue= "Fanout_queue2"/>
</rabbit:bindings>
</rabbit:fanout-exchange>
/**
* @Title: Mqproducerimpl.java
* @Package COM.CYL.RABBITMQ
* @Description: TODO (describe what the file does in a sentence)
* @author [email protected]
* @date April 25, 2016 1:12:46
* @version V1.0
*/
Package COM.CYL.RABBITMQ;
Import Org.springframework.amqp.core.AmqpTemplate;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;
/**
* @author Zjhua
*
*/
@Service
public class Mqproducerimpl implements Mqproducer {
@Autowired
Private Amqptemplate amqptemplate;
/* (Non-javadoc)
* @see Com.cyl.rabbitmq.mqproducer#senddatatoqueue (java.lang.String, Java.lang.Object)
*/
@Override
public void Senddatatoqueue (String Queuekey, Object object) {
try {
Amqptemplate.convertandsend ("Fanout-mq-exchange", null,object);
} catch (Exception e) {
E.printstacktrace ();
}
}
}
Consumer End
<rabbit:connection-factory id= "Rabbitconnectionfactory" Username= "Guest" password= "Guest" host= "localhost" port= "5672"/>
<rabbit:template id= "Amqptemplate" connection-factory= "Rabbitconnectionfactory" exchange= "Fanout-mq-exchange" message-converter= "Jsonmessageconverter"/>
<rabbit:admin id= "admin" connection-factory= "rabbitconnectionfactory"/>
<bean id= "Jsonmessageconverter" class= " Org.springframework.amqp.support.converter.Jackson2JsonMessageConverter "/>
<!--<rabbit:queue id= "Fanout_queue" name= "Fanout_queue" durable= "true" auto-delete= "false" Exclusive= "false"/ >
<rabbit:queue id= "fanout_queue1" name= "fanout_queue1" durable= "true" auto-delete= "false" exclusive= "false"/>
--><rabbit:queue id= "fanout_queue2" name= "Fanout_queue2" durable= "true" auto-delete= "false" Exclusive= "false" />
<rabbit:fanout-exchange id= "Fanout-mq-exchange" name= "Fanout-mq-exchange" durable= "true" auto-delete= "false" >
<rabbit:bindings>
<!--<rabbit:binding queue= "Fanout_queue"/>
<rabbit:binding queue= "Fanout_queue1"/>--
<rabbit:binding queue= "Fanout_queue2"/>
</rabbit:bindings>
</rabbit:fanout-exchange>
<!--
<rabbit:topic-exchange name= "Myexchange" >
<rabbit:bindings>
<rabbit:binding queue= "Myqueue" pattern= "foo.*"/>
</rabbit:bindings>
</rabbit:topic-exchange>
<rabbit:listener-container id= "fanout1" connection-factory= "rabbitconnectionfactory" acknowledge= "Auto" >
<rabbit:listener queues= "fanout_queue1" ref= "QueueListener1"/>
</rabbit:listener-container>
-
<rabbit:listener-container id= "Fanout2" connection-factory= "rabbitconnectionfactory" acknowledge= "Auto" >
<rabbit:listener queues= "fanout_queue2" ref= "QueueListener2"/>
</rabbit:listener-container>
<!--<bean id= "Mqproducer" class= "Com.cyl.rabbitmq.MQProducerImpl" ></bean>
<bean id= "QueueListener1" class= "Com.cyl.rabbitmq.QueueListenter" ></bean>-
<bean id= "QueueListener2" class= "Com.cyl.rabbitmq.QueueListenter" ></bean>
/**
* @Title: Queuelistenter.java
* @Package COM.CYL.RABBITMQ
* @Description: TODO (describe what the file does in a sentence)
* @author [email protected]
* @date April 25, 2016 1:15:31
* @version V1.0
*/
Package COM.CYL.RABBITMQ;
Import Org.springframework.amqp.core.Message;
Import Org.springframework.amqp.core.MessageListener;
/**
* @author Zjhua
*
*/
public class Queuelistenter implements MessageListener {
/* (Non-javadoc)
* @see Org.springframework.amqp.core.messagelistener#onmessage (org.springframework.amqp.core.Message)
*/
@Override
public void OnMessage (Message msg) {
System.out.println (New String (Msg.getbody ()));
}
}
In fact, this configuration is only suitable for small and medium size, the number of servers can be pre-estimated environment, for the number of servers may not be predetermined or often changes in the environment, is not suitable for the use of the profile, but dynamic registration when the container starts. and the queue attribute should be exclusive+auto-delete.
Spring AMQP RABBITMQ fanout configuration