RabbitMQ有四种Exchange类型,分别是Direct 、Fanout 、Topic、Headers
A headers exchange is designed to for routing on multiple attributes that are more easily expressed as message headers than a routing key. Headers exchanges ignore the routing key attribute. Instead, the attributes used for routing are taken from the headers attribute. A message is considered matching if the value of the header equals the value specified upon binding.
It is possible to bind a queue to a headers exchange using more than one header for matching. In this case, the broker needs one more piece of information from the application developer, namely, should it consider messages with any of the headers matching, or all of them? This is what the "x-match" binding argument is for. When the "x-match" argument is set to "any", just one matching header value is sufficient. Alternatively, setting "x-match" to "all" mandates that all the values must match.
Headers exchanges can be looked upon as "direct exchanges on steroids". Because they route based on header values, they can be used as direct exchanges where the routing key does not have to be a string; it could be an integer or a hash (dictionary) for example.
有了上面的介绍,下面就直接上代码吧!!
Consumer:
package fanout;
import java.io.IOException;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
public class FanoutConsumer {
private static final String EXCHANGE_NAME = "logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
//rabbitmq监听IP
factory.setHost("192.168.249.128");
//rabbitmq监听默认端口
factory.setPort(5672);
//设置访问的用户
factory.setUsername("test");
factory.setPassword("test");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
//声明路由名字和类型
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
//获取随机队列名称
String queueName = channel.queueDeclare().getQueue();
//创建队列
channel.queueDeclare(queueName, false, false, true, null);
//把队列绑定到路由上
channel.queueBind(queueName, EXCHANGE_NAME, "");
System.out.println(" Waiting for msg....");
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("Received msg is '" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
}
private static final String EXCHANGE_NAME = "logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
//rabbitmq监听IP
factory.setHost("192.168.249.128");
//rabbitmq监听默认端口
factory.setPort(5672);
//设置访问的用户
factory.setUsername("test");
factory.setPassword("test");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
//声明路由名字和类型
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
//获取随机队列名称
String queueName = channel.queueDeclare().getQueue();
//创建队列
channel.queueDeclare(queueName, false, false, true, null);
//把队列绑定到路由上
channel.queueBind(queueName, EXCHANGE_NAME, "");
System.out.println(" Waiting for msg....");
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("Received msg is '" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
}
Producer:
package fanout;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class FanoutProducer {
private static final String EXCHANGE_NAME = "logs";
public static void main(String[] argv) throws Exception{
ConnectionFactory factory = new ConnectionFactory();
//rabbitmq监听IP
factory.setHost("192.168.249.128");
//rabbitmq监听默认端口
factory.setPort(5672);
//设置访问的用户
factory.setUsername("test");
factory.setPassword("test");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
//声明路由名字和类型
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
String message = makeMessage(argv);
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
System.out.println("Sent msg is '" + message + "'");
channel.close();
connection.close();
}
private static String makeMessage(String[] strings){
if (strings.length < 1){
return "这是默认消息!!";
}else{
StringBuffer buffer= new StringBuffer();
for (int i = 0; i < strings.length; i++) {
buffer.append(strings[i]);
}
return buffer.toString();
}
}
}
运行Consumer:
运行Producer:
有问题可以扫码向我提问哦
好了 , 就到这里~~
祝生活愉快!!!