5.Exchange(交换机 转发器)

对比

一方面是接收生产者的消息,另一方面是向队列推送消息.

//匿名转发:""
//fanout(不处理路由键)
//声明交换机
channel.exchangeDeclare(EXCHANGE_NAME,"fanout");//分发
//发送消息
String msg="hello ps";
channel.basicPublish(EXCHANGE_NAME,"", null, msg.getBytes());

路由模式

package com.mmr.rabbit.routing;

import com.mmr.rabbit.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Send {
	
	private static final String EXCHANGE_NAME="test_exchange_direct";
	
	public static void main(String[] args) throws Exception {
		Connection connection = ConnectionUtils.getConnection();
		
		Channel channel = connection.createChannel();
		
		//exchange
		channel.exchangeDeclare(EXCHANGE_NAME, "direct");
		String msg = "hello direct";
		String routingKey="info";
		channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes());
		
		
		channel.close();
		connection.close();
	}
}


package com.mmr.rabbit.routing;

import java.io.IOException;

import com.mmr.rabbit.util.ConnectionUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Recv2 {
	
	private static final String EXCHANGE_NAME="test_exchange_direct";
	private static final String QUEUE_NAME="test_queue_direct_2";
	
	public static void main(String[] args) throws Exception {
		Connection connection = ConnectionUtils.getConnection();
		
		final Channel channel = connection.createChannel();
		
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "warning");
		
		channel.basicQos(1);
		
		//定义一个消费者
		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.err.println("[2] Recv:" + message + "");
			    try{
			    	Thread.sleep(2000);
			    }catch(Exception e){
			    	e.printStackTrace();
			    }finally{
			    	System.err.println("[2] done");
			    	channel.basicAck(envelope.getDeliveryTag(), false);
			    }
			  }
		};
		boolean autoAck = false;//自动应答
		channel.basicConsume(QUEUE_NAME,autoAck,consumer);
	}
}



package com.mmr.rabbit.routing;

import java.io.IOException;

import com.mmr.rabbit.util.ConnectionUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Recv1 {
	
	private static final String EXCHANGE_NAME="test_exchange_direct";
	private static final String QUEUE_NAME="test_queue_direct_1";
	
	public static void main(String[] args) throws Exception {
		Connection connection = ConnectionUtils.getConnection();
		
		final Channel channel = connection.createChannel();
		
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
		
		channel.basicQos(1);
		
		//定义一个消费者
		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.err.println("[1] Recv:" + message + "");
			    try{
			    	Thread.sleep(2000);
			    }catch(Exception e){
			    	e.printStackTrace();
			    }finally{
			    	System.err.println("[1] done");
			    	channel.basicAck(envelope.getDeliveryTag(), false);
			    }
			  }
		};
		boolean autoAck = false;//自动应答
		channel.basicConsume(QUEUE_NAME,autoAck,consumer);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值