RabbitMQ-fanout模式

1.消费者

package com.touch.fanout;
import cn.hutool.core.util.RandomUtil;
import com.rabbitmq.client.*;
import com.touch.utils.RabbitMQUtil;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeoutException;
public class TestCustomer {
	public final static String EXCHANGE_NAME="fanout_exchange";
	public static void main(String[] args) throws IOException, TimeoutException {
		//为当前消费者取随机名
		final String name = "consumer-"+ RandomUtil.randomString(5);
		//判断服务器是否启动
		RabbitMQUtil.checkServer();
		// 创建连接工厂
		ConnectionFactory factory = new ConnectionFactory();
		//设置RabbitMQ地址
		factory.setHost("localhost");
		//创建一个新的连接
		Connection connection = factory.newConnection();
		//创建一个通道
		Channel channel = connection.createChannel();
		//交换机声明(参数为:交换机名称;交换机类型)
		channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
		//获取一个临时队列
		String queueName = channel.queueDeclare().getQueue();
		//队列与交换机绑定(参数为:队列名称;交换机名称;routingKey忽略)
		channel.queueBind(queueName,EXCHANGE_NAME,"");
		System.out.println(name +" 等待接受消息");
		//DefaultConsumer类实现了Consumer接口,通过传入一个频道,
		// 告诉服务器我们需要那个频道的消息,如果频道中有消息,就会执行回调函数handleDelivery
		Consumer consumer = 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(new Date()+" "+name + " 接收到消息 '" + message + "'");
			}
		};
		//自动回复队列应答 -- RabbitMQ中的消息确认机制
		channel.basicConsume(queueName, true, consumer);
	}
}

2.生产者

package com.touch.fanout;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.touch.utils.RabbitMQUtil;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeoutException;
/**
 * 启动生产者,生产100条信息。
 * 两个消费者都能收到这100条信息。和ActiveMQ主题模式类似
 */
public class TestProducer {
	public final static String EXCHANGE_NAME="fanout_exchange";
	public static void main(String[] args) throws IOException, TimeoutException {
		RabbitMQUtil.checkServer();
		//创建连接工厂
		ConnectionFactory factory= new ConnectionFactory();
		//设置RabbitMQ相关信息
		factory.setHost("localhost");
		//创建一个新的连接
		Connection connection =factory.newConnection();
		//创建一个通道
		Channel channel = connection.createChannel();
		channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
		for (int i = 0; i <100 ; i++) {
			String message="direct 消息 " +i;
			channel.basicPublish(EXCHANGE_NAME,"",null,message.getBytes("UTF-8"));
			System.out.println(new Date()+" "+"发送消息:" + message);
		}
		//关闭通道和连接
		channel.close();
		connection.close();
	}
}

[源代码地址:https://github.com/hyghub/javanotes]
[学 习 地 址:http://how2j.cn/k/message/message-rabbitmq-type/2031.html]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值