rabbitmq 交换模式-Fanout

rabbitmq 交换模式
rabbitmq 交换模式-Fanout_第1张图片
使用 spring 整合 amqp 协议
fanout 模式将一个消息同时发送到 多个队列中。
使用 spring-rabbit 进行包装,添加POM到MAVEN配置中。

<dependency>
    <groupId>org.springframework.amqpgroupId>
    <artifactId>spring-rabbitartifactId>
    <version>1.7.3.RELEASEversion>
dependency>

核心代码

import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
public class Fanout {
    public static void main(String[] args) {
        CachingConnectionFactory cf = new CachingConnectionFactory("ip");
        cf.setUsername("root");
        cf.setPassword("");

        RabbitAdmin admin = new RabbitAdmin(cf);
        Queue queue = new Queue("q1");//
        Queue queue2 = new Queue("q2");

        admin.declareQueue(queue);
        admin.declareQueue(queue2);

        FanoutExchange exchange = new FanoutExchange("zdcex");
        admin.declareExchange(exchange);
        admin.declareBinding(BindingBuilder.bind(queue).to(exchange));//将队列绑定到交换机。
        admin.declareBinding(BindingBuilder.bind(queue2).to(exchange));//将队列绑定到交换机

        //封装rabbit模板
        RabbitTemplate template = new RabbitTemplate(cf);
        template.convertAndSend("zdcex", "foo.bar", "Hello, world!");


    }
}

绑定后的效果:
rabbitmq 交换模式-Fanout_第2张图片

使用 spring 配置文件配置:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    http://www.springframework.org/schema/rabbit
    http://www.springframework.org/schema/rabbit/spring-rabbit-1.7.xsd">


    <rabbit:connection-factory id="connectionFactory"
        host="ip" username="root" password="" />
    <rabbit:admin connection-factory="connectionFactory" />

    
    <rabbit:template exchange="amqpExchange" id="amqpTemplate"
        connection-factory="connectionFactory" />

    
    <rabbit:queue name="test_queue_key">rabbit:queue>

    <rabbit:queue name="test2">rabbit:queue>
    
    <rabbit:fanout-exchange name="amqpExchange">

        <rabbit:bindings>
            <rabbit:binding queue="test_queue_key">rabbit:binding>
            <rabbit:binding queue="test2">rabbit:binding>
        rabbit:bindings>
    rabbit:fanout-exchange>
    beans>

发送代码

    public static void main(String[] args) throws InterruptedException {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("rabbit.xml");
        RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
        template.convertAndSend("hello");

        Thread.sleep(1000);
        ctx.destroy();

    }

你可能感兴趣的:(rabbitmq,Java,spring)