三、RabbitMq学习笔记

RabbitMQ原生API三种交换模式

1. Hello World

在这里没有声明交换机(exchange),也没有声明绑定(bind),RabbitMQ会使用默认的交换机(AMQP default)路由键就是队列名称

【生产者】

/**
 * 消费者
 * 
 * @author ITCloud
 */
public class Consumer {
   
	public static void main(String[] args) throws Exception {
   
		// 1.创建连接
		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost("192.168.186.130");
		connectionFactory.setPort(5672);
		connectionFactory.setUsername("admin");
		connectionFactory.setPassword("admin");
		connectionFactory.setVirtualHost("/");
		// 2.获取连接,创建channel
		Connection connection = connectionFactory.newConnection();
		Channel channel = connection.createChannel();
		
		// 3. 队列声明
		String queueName = "hello.world";
		/**
		 * (String queue, boolean durable, boolean exclusive, boolean autoDelete, Map arguments)
		 * 参数说明:
		 * 	queue:队列名称
		 * 	duable:队列是否是持久化的,rabbitmq重启之后,队列依然存在
		 * 	exclusive:独占队列,只对当前连接有效,一般都会设置成非独占队列false
		 * 	autoDelete:队列是否自动删除
		 * 	arguments:一些参数,后续介绍
		 */
		channel.queueDeclare(queueName, true, false, false, null);
		
		// 4.创建一个简单的消费者
		com.rabbitmq.client.Consumer consumer = new DefaultConsumer(channel) {
   
			//处理接收的消息
			/**
			 * body:接收消息体
			 */
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
   
				System.out.println("消费端:" + new String(body));
			}
		};
		
		//设置异步接收消息,当消费端启动后,将一直会监听消费端
		channel.basicConsume(queueName, true, consumer);
	}
}

【消费者】

/**
 * 生产者
 * @author ITCloud
 */
public class Producer {
   
	public static void main(String[] args) throws Exception {
   
		//1.创建连接工厂类
		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost("192.168.186.130");
		connectionFactory.setPort(5672);
		connectionFactory.setUsername("admin");
		connectionFactory.setPassword("admin");
		connectionFactory.setVirtualHost("/");
		//2.获取连接,创建channel
		Connection connection = connectionFactory.newConnection();
		Channel channel = connection.createChannel();
		
		//3. 发送消息
		String msg = "Hello world RabbitMQ!!";
		/**
		 * 参数详解
		 *String exchange, String routingKey, BasicProperties props, byte[] body
		 * 交换机名称:exchange 必须,如果不指定,则使用rabbitmq提供默认的exchange:AMQP default
		 * 路由键:routingKey 当交换机是Fanout时候routingKey可以不需要,如果没有明确指定,则路由到队列
		 * 发送消息带一些参数:props 非必需
		 * 要发送的消息:body
		 */
		channel.basicPublish("", "hello.world", null, msg.getBytes());
		
		//4.关闭相关连接
		channel.close();
		connection.close();
	
	}
}

2. direct交换模式

direct交换模式的特点:生产者和消费者通过routingKey来连接,消费端只有拥有相应的routingKey才能进行消费

【消费者】

/**
 * direct类型的交换机,特点:
 * 	生产者和消费者通过routingKey来连接,消费端只有拥有相应的routingKey才能进行消费
 * @author ITCloud
 *
 */
public class DirectConsumer {
   
	public static void main(String[] args) throws Exception{
   
		// 1.创建连接
		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost("192.168.186.130");
		connectionFactory.setPort(5672);
		connectionFactory.setUsername("admin");
		connectionFactory.setPassword("admin");
		connectionFactory.setVirtualHost("/");
		// 2.获取连接,创建channel
		Connection connection = connectionFactory.newConnection();
		Channel channel = connection.createChannel();
		
		//3.交换机和队列声明
		/**
		 * 参数说明:(String exchange, String type, boolean durable, boolean autoDelete,Map arguments)
		 * exchange: 交换机名称
		 * type:交换机类型:direct topic fanout hearders(几乎不用)
		 * durable: 是否是持久化的队列,
		 * autoDelete:是否自动删除
		 * arguments:一些参数,几乎不用
		 */
		channel.exchangeDeclare("direct.exchange", "direct", true, false, null);
		channel.queueDeclare("direct.queue", true, false, false, null);
		//4.队列绑定
		channel.queueBind("direct.queue", "direct.exchange", "direct.queue");
		
		Consumer consumer = new DefaultConsumer(channel) {
   
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
   
				System.out.println("消费端:" + new String(body));
			}
		};
		//true表示自动接收
		channel.basicConsume("direct.queue", true, consumer);
	}
}

【生产者】

public class DirectProducer {
   
	public static void main(String[] args) throws Exception{
   
		// 1.创建连接
		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost("192.168.186.130");
		connectionFactory.setPort(5672);
		connectionFactory.setUsername("admin");
		connectionFactory.setPassword("admin");
		connectionFactory.setVirtualHost("/");
		// 2.获取连接,创建channel
		Connection connection = connectionFactory.newConnection();
		Channel channel = connection.createChannel();
		
		//3. 发送消息
		String msg = "Send msg by RabbitMQ!! direct";
		channel.basicPublish("direct.exchange", "direct.queue", null, msg.getBytes());
		
		channel.close();
		connection.close();
	}
}

3. fanout交换模式

fanout交换模式的特点:不需要routingKey,只要绑定了交换机即可

这种模式可以用于死信队列中

【消费者】

/**
 * fanout类型的交换机,特点:
 * 	不需要routingKey,只要绑定了交换机即可
 * @author ITCloud
 *
 */
public class FanoutConsumer {
   
	public static void main(String[] args) throws Exception{
   
		// 1.创建连接
		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost("192.168.186.130");
		connectionFactory.setPort(5672);
		connectionFactory.setUsername("admin");
		connectionFactory.setPassword("admin");
		connectionFactory.setVirtualHost("/");
		// 2.获取连接,创建channel
		Connection connection = connectionFactory.newConnection();
		Channel channel = connection.createChannel();
		
		//3.交换机和队列声明
		channel.exchangeDeclare("fanout.exchange", "fanout", true, false, null);
		channel.queueDeclare("fanout.queue", true, false, false, null);
		//4.队列绑定,这里rountingKey=""; 但是不可以设置为null
		channel.queueBind("fanout.queue", "fanout.exchange", "");
		
		Consumer consumer = new DefaultConsumer(channel) {
   
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
   
				System.out.println("消费端:" + new String(body));
			}
		};
		//true表示自动接收
		channel.basicConsume("fanout.queue", true, consumer);
	}
}

【生产者】

public class FanoutProducer {
   
	public static void main(String[] args) throws Exception{
   
		// 1.创建连接
		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost("192.168.186.130");
		connectionFactory.setPort(5672);
		connectionFactory.setUsername("admin");
		connectionFactory.setPassword("admin");
		connectionFactory.setVirtualHost("/");
		// 2.获取连接,创建channel
		Connection connection = connectionFactory.newConnection();
		Channel channel = connection.createChannel();
		
		//3. 发送消息
		String msg = "Send msg by RabbitMQ!! fanout";
		channel.basicPublish("fanout.exchange", "", null, msg.getBytes());
		
		channel.close();
		connection.close();
	}
}

4. topic交换模式

【消费者】

/**
 * topic类型的交换机,特点:
 * 	通过rountingKey进行模糊匹配:
 * 		1. * 匹配一个单词
 * 		2. # 匹配多个单词
 * 例如:A.* 只可以匹配A.aab;但是不可以匹配A.aa.bb
 * @author ITCloud
 *
 */
public class TopicConsumer {
   
	public static void main(String[] args) throws Exception{
   
		// 1.创建连接
		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost("192.168.186.130");
		connectionFactory.setPort(5672);
		connectionFactory.setUsername("admin");
		connectionFactory.setPassword("admin");
		connectionFactory.setVirtualHost("/");
		// 2.获取连接,创建channel
		Connection connection = connectionFactory.newConnection();
		Channel channel = connection.createChannel();
		
		//3.交换机和队列声明
		channel.exchangeDeclare("topic.exchange", "topic", true, false, null);
		channel.queueDeclare("topic.queue", true, false, false, null);
		//4.队列绑定,这里可以设置rountingKey=""; 但是不可以设置为null
		channel.queueBind("topic.queue", "topic.exchange", "topic.#");
		
		Consumer consumer = new DefaultConsumer(channel) {
   
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
   
				System.out.println("消费端:" + new String(body));
			}
		};
		//true表示自动接收
		channel.basicConsume("topic.queue", true, consumer);
	}
}

【生产者】

public class TopicProducer {
   
	

你可能感兴趣的:(MQ)