MQ 入门【5】--发布订阅模式

1.连接

package com.utils;

import java.io.IOException;

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

public class ConnectionUtils {

	public static Connection getConnetions() throws IOException {

		ConnectionFactory factory = new ConnectionFactory();

		factory.setHost("127.0.0.1");
		factory.setPort(5672);
		factory.setVirtualHost("/vhost_zq");

		factory.setUsername("develop");
		factory.setPassword("123456");
		return factory.newConnection();

	}

}

2.生产者

package com.publish;

import java.io.IOException;

import com.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Send {

	
	/**
	 *						|===>queue1-->consumer1 
	 * producer-->exchange【交换机 转发器】
	 * 						|===>queue2-->consumer2
	 * 
	 * 解读:
	 * 1.一个生产者,多个消费者;
	 * 2.每个消费者都有自己的队列;
	 * 3.生产者没有直接把消息发给队列,而是直接发送给了交换机【又叫转发器】exchange
	 * 4.每个队列都要绑定到交换机上面;
	 * 5.生产者发送的消息经过交换机到达队列就可以实现一个生产者对应对个消费者。
	 */
	
	//定义交换机
	public static final String EXCHANGE_NAME="test_exchange_fanout";
	public static void main(String[] args) throws IOException {

		//创建连接
		Connection connetions = ConnectionUtils.getConnetions();
		
		//定义队列
		Channel channel = connetions.createChannel();
		
		//声明交换机[交换机没有存储的能力,在mq里面只有队列有存储的能力]
		channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
		//fanout 是分发的意思
		//设置每次给消费者消息的时候只给一个
		channel.basicQos(1);
		
		//定义消息
		String msg="hello zhangsan!!!";
		
		//发布消息
		channel.basicPublish(EXCHANGE_NAME, "", null, msg.getBytes());
		
		System.out.println("publish mas:"+msg);
		
		//关闭队列
		channel.close();
		
		//关闭连接
		connetions.close();
	}

}
/**
	 *						|===>queue1-->consumer1 
	 * producer-->exchange【交换机 转发器】
	 * 						|===>queue2-->consumer2
	 * 
	 * 解读:
	 * 1.一个生产者,多个消费者;
	 * 2.每个消费者都有自己的队列;
	 * 3.生产者没有直接把消息发给队列,而是直接发送给了交换机【又叫转发器】exchange
	 * 4.每个队列都要绑定到交换机上面;
	 * 5.生产者发送的消息经过交换机到达队列就可以实现一个生产者对应对个消费者。
	 */
	
	//定义交换机
	public static final String EXCHANGE_NAME="test_exchange_fanout";
	public static void main(String[] args) throws IOException {

		//创建连接
		Connection connetions = ConnectionUtils.getConnetions();
		
		//定义队列
		Channel channel = connetions.createChannel();
		
		//声明交换机[交换机没有存储的能力,在mq里面只有队列有存储的能力]
		channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
		//fanout 是分发的意思
		//设置每次给消费者消息的时候只给一个
		channel.basicQos(1);
		
		//定义消息
		String msg="hello zhangsan!!!";
		
		//发布消息
		channel.basicPublish(EXCHANGE_NAME, "", null, msg.getBytes());
		
		System.out.println("publish mas:"+msg);
		
		//关闭队列
		channel.close();
		
		//关闭连接
		connetions.close();
	}

}

3.消费者

package com.publish;

import java.io.IOException;

import com.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

public class Rec1 {

	private static final String QUEUE_NAME="test_queue_faout_email";
	
	private static final String EXCHANGE_NAME="test_exchange_fanout";
	public static void main(String[] args) throws IOException {

		//建立连接
		Connection connetions = ConnectionUtils.getConnetions();
		
		//声明队列
		final Channel channel = connetions.createChannel();
		
		channel.queueDeclare(QUEUE_NAME, true, false, false, null);
		
		//绑定交换机
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
		
		//设置每次只接收一个信息
		channel.basicQos(1);
		
		DefaultConsumer consumer=new DefaultConsumer(channel){
			
			
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {

				String msg=new String(body,"utf-8");
				
				System.out.println("[1] recive:"+msg);
				
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					
					System.out.println("[1] done!");
					//手动应答
					channel.basicAck(envelope.getDeliveryTag(), false);
					
				}
				
			}
		};
		
		//关联消费者  开始订阅  关闭自动应答
		channel.basicConsume(QUEUE_NAME, false, consumer);
			
	}

}
//绑定交换机
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
		
		//设置每次只接收一个信息
		channel.basicQos(1);
		
		DefaultConsumer consumer=new DefaultConsumer(channel){
			
			
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {

				String msg=new String(body,"utf-8");
				
				System.out.println("[1] recive:"+msg);
				
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					
					System.out.println("[1] done!");
					//手动应答
					channel.basicAck(envelope.getDeliveryTag(), false);
					
				}
				
			}
		};
		
		//关联消费者  开始订阅  关闭自动应答
		channel.basicConsume(QUEUE_NAME, false, consumer);
			
	}

}

4.消费者2

package com.publish;

import java.io.IOException;

import com.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

public class Rec2 {

	private static final String QUEUE_NAME="test_queue_faout_sms";
	
	private static final String EXCHANGE_NAME="test_exchange_fanout";
	public static void main(String[] args) throws IOException {

		//建立连接
		Connection connetions = ConnectionUtils.getConnetions();
		
		//声明队列
		final Channel channel = connetions.createChannel();
		
		channel.queueDeclare(QUEUE_NAME, true, false, false, null);
		
		//绑定交换机
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
		
		//设置每次只接收一个信息
		channel.basicQos(1);
		
		DefaultConsumer consumer=new DefaultConsumer(channel){
			
			
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {

				String msg=new String(body,"utf-8");
				
				System.out.println("[2] recive:"+msg);
				
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					
					System.out.println("[2] done!");
					//手动应答
					channel.basicAck(envelope.getDeliveryTag(), false);
					
				}
				
			}
		};
		
		//关联消费者  开始订阅  关闭自动应答
		channel.basicConsume(QUEUE_NAME, false, consumer);
			
	}

}
//绑定交换机
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
		
		//设置每次只接收一个信息
		channel.basicQos(1);
		
		DefaultConsumer consumer=new DefaultConsumer(channel){
			
			
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {

				String msg=new String(body,"utf-8");
				
				System.out.println("[2] recive:"+msg);
				
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					
					System.out.println("[2] done!");
					//手动应答
					channel.basicAck(envelope.getDeliveryTag(), false);
					
				}
				
			}
		};
		
		//关联消费者  开始订阅  关闭自动应答
		channel.basicConsume(QUEUE_NAME, false, consumer);
			
	}

}

一个生产者可以通过交换机,一条消息传递给多个消费者。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值