java 连接rabbitmq(Fanout Exchange)

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 *类说明:fanout生产者
 */
public class FanoutProducer {

    public final static String EXCHANGE_NAME = "fanout_logs";

    public static void main(String[] args) throws Exception {

        //创建连接、连接到RabbitMQ
        ConnectionFactory connectionFactory = new ConnectionFactory();

        //设置下连接工厂的连接地址(使用默认端口5672)
        connectionFactory.setHost("192.168.19.128");
        connectionFactory.setUsername("mark");
        connectionFactory.setPassword("123456");
        connectionFactory.setVirtualHost("enjoyedu");

        //创建连接
        Connection connection = connectionFactory.newConnection();

        //创建信道
        Channel channel = connection.createChannel();

        //在信道中设置交换器
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);

        //申明路由键\消息体
        String[] routeKeys ={"king","mark","james"};
        for(int i=0; i<routeKeys.length; i++){
            String routeKey = routeKeys[i];
            String message = "Hello RabbitMQ" + (i+1);
            //发布消息
            channel.basicPublish(EXCHANGE_NAME, routeKey, null, message.getBytes());
            System.out.println("send routeKey is " + routeKey + " and message is " + message);
        }

        // 关闭频道和连接
        channel.close();
        connection.close();
    }
}
import com.rabbitmq.client.*;
import java.io.IOException;

public class Consumer1 {

    public static void main(String[] argv) throws Exception{

        //创建连接、连接到RabbitMQ
        ConnectionFactory connectionFactory = new ConnectionFactory();

        //设置下连接工厂的连接地址(使用默认端口5672)
        connectionFactory.setHost("192.168.19.128");
        connectionFactory.setUsername("mark");
        connectionFactory.setPassword("123456");
        connectionFactory.setVirtualHost("enjoyedu");

        //创建连接
        Connection connection = connectionFactory.newConnection();

        //创建信道
        final Channel channel = connection.createChannel();

        //在信道中设置交换器
        channel.exchangeDeclare(FanoutProducer.EXCHANGE_NAME,
                BuiltinExchangeType.FANOUT);

        // 声明一个随机队列
        String queueName = channel.queueDeclare().getQueue();

        /*队列绑定到交换器上时,是允许绑定多个路由键的,也就是多重绑定*/
        String[] routekeys={"king","mark","james"};
        for(String routekey:routekeys){
            channel.queueBind(queueName,FanoutProducer.EXCHANGE_NAME,
                    routekey);
        }
        System.out.println(" ["+queueName+"] waiting for messages...");

        // 创建队列消费者
        final Consumer consumerA = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body)
                    throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println("receive routeKey is " + envelope.getRoutingKey()+" and message is " + message);
            }
        };
        channel.basicConsume(queueName, true, consumerA);
    }
}
import com.exchange.direct.DirectProducer;
import com.rabbitmq.client.*;

/**
 *类说明:fanout消费者--绑定一个不存在的路由键
 */
import java.io.IOException;

public class Consumer2 {

    public static void main(String[] argv) throws Exception{

        //创建连接、连接到RabbitMQ
        ConnectionFactory connectionFactory = new ConnectionFactory();

        //设置下连接工厂的连接地址(使用默认端口5672)
        connectionFactory.setHost("192.168.19.128");
        connectionFactory.setUsername("mark");
        connectionFactory.setPassword("123456");
        connectionFactory.setVirtualHost("enjoyedu");

        //创建连接
        Connection connection = connectionFactory.newConnection();

        //创建信道
        Channel channel = connection.createChannel();

        //在信道中设置交换器
        channel.exchangeDeclare(FanoutProducer.EXCHANGE_NAME, BuiltinExchangeType.FANOUT);

        // 声明一个随机队列
        String queueName = channel.queueDeclare().getQueue();

        //设置一个不存在的路由键
        String routekey="xxx";
        channel.queueBind(queueName, FanoutProducer.EXCHANGE_NAME, routekey);

        System.out.println("waiting for messages......");

        // 创建队列消费者
        final Consumer consumerB = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag,
                                       Envelope envelope,
                                       AMQP.BasicProperties properties,
                                       byte[] body)
                    throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println("receive routeKey is " + envelope.getRoutingKey()+" and message is " + message);
            }
        };

        channel.basicConsume(queueName, true, consumerB);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值