RabbitMQ:四种ExChange用法

  • 时间:
  • 浏览:0
  • 来源:UU快3直播官网

factory.setPassword(S_RabbitMQ.QUEUE_PWD);// MQ密码

}

System.out.println("Sent msg finish");

{

//声明路由名字和类型

{

System.out.println(" Waiting for msg....");

String message = "hello world! ";

factory.setUsername(S_RabbitMQ.QUEUE_USER);// MQ用户名

{

ConnectionFactory factory = new ConnectionFactory();

channel.basicPublish(EXCHANGE_NAME, "routingkey1", null, (message+i).getBytes());

factory.setUsername(S_RabbitMQ.QUEUE_USER);// MQ用户名

message = new String(body, "UTF-8");

channel.basicConsume(QUEUE_NAME, true, consumer);

factory.setPassword(S_RabbitMQ.QUEUE_PWD);// MQ密码

当向另有一一个多 fanout发送另有一一个多 消息时,RoutingKey的设置不起作用。

{

String message = "hello world! ";

channel.close();

Connection connection = factory.newConnection();

{

Channel channel = connection.createChannel();

//声明队列

}

connection.close();

factory.setPassword(S_RabbitMQ.QUEUE_PWD);// MQ密码

另有一一个多 队列内有所有消费者(饱含什么并没办法 相应RoutingKey的消费者),将平分队列接收到的消息

factory.setPort(S_RabbitMQ.QUEUE_PORT);// MQ端口

public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)

Map<String, Object> headers = new Hashtable<String, Object>();

factory.setUsername(S_RabbitMQ.QUEUE_USER);// MQ用户名

factory.setPassword(S_RabbitMQ.QUEUE_PWD);// MQ密码

factory.setPassword(S_RabbitMQ.QUEUE_PWD);// MQ密码

Channel channel = connection.createChannel();

public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,

Channel channel = connection.createChannel();

ConnectionFactory factory = new ConnectionFactory();

headers.put("x-match", "all");

channel.basicConsume(QUEUE_NAME, true, consumer);

factory.setPort(S_RabbitMQ.QUEUE_PORT);// MQ端口

channel.exchangeDeclare(EXCHANGE_NAME, "headers", true, false, null);

----------------消息生产者----------------

//声明路由名字和类型

for(int i=0;i<1150;i++)

----------------消息生产者----------------

channel.exchangeDeclare(EXCHANGE_NAME, "topic", true, false, null);

channel.close();

channel.close();

Consumer consumer = new DefaultConsumer(channel) {

System.out.println("1 Received msg='" + message + "'");

Consumer consumer = new DefaultConsumer(channel)

{

channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);

};

};

factory.setHost(S_RabbitMQ.QUEUE_IP);// MQ主机

{

另有一一个多 队列内拥有相应RoutingKey的消费者,将平分队列接收到的消息。

String message = "hello world! ";

factory.setHost(S_RabbitMQ.QUEUE_IP);// MQ主机

Channel channel = connection.createChannel();

Consumer consumer = new DefaultConsumer(channel)

ConnectionFactory factory = new ConnectionFactory();

builder.headers(headers);

channel.basicPublish(EXCHANGE_NAME, "routingkey1", builder.build(), (message + i).getBytes());

Map<String, Object> headers = new Hashtable<String, Object>();

channel.basicPublish(EXCHANGE_NAME, "routingkey1", null, (message + i).getBytes());

}

另有一一个多 队列内有所有消费者(饱含什么并没办法 相应RoutingKey或headers的消费者),将平分队列接收到的消息

headers.put("age", 31);

headers.put("name", "jack");

String message = "";

一、fanout

e.printStackTrace();

factory.setPort(S_RabbitMQ.QUEUE_PORT);// MQ端口

ex.printStackTrace();

channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "routingkey1", null);

}

@Override

message = new String(body, "UTF-8");

}

{

factory.setPort(S_RabbitMQ.QUEUE_PORT);// MQ端口

catch (UnsupportedEncodingException e)

// 声明路由名字和类型

channel.queueDeclare(QUEUE_NAME, true, false, false, null);

channel.basicPublish(EXCHANGE_NAME, "", null, (message+i).getBytes());

{

channel.basicConsume(QUEUE_NAME, true, consumer);

factory.setHost(S_RabbitMQ.QUEUE_IP);

channel.queueDeclare(QUEUE_NAME, true, false, false, null);

System.out.println("Received msg='" + message + "'");

channel.exchangeDeclare(EXCHANGE_NAME, "headers", true, false, null);

factory.setUsername(S_RabbitMQ.QUEUE_USER);// MQ用户名

Builder builder = new Builder();

四、headers

ExChange和Queue事先是多对多的关系。

channel.close();



----------------消息消费者----------------

// 声明路由名字和类型

Connection connection = factory.newConnection();

消息会被发送给同另有一一个多 交换机下的所有队列,每个队列接收到的消息是一样的;

channel.exchangeDeclare(EXCHANGE_NAME, "fanout", true, false, null);

Connection connection = factory.newConnection();

}

}

System.out.println("1 Received msg='" + message + "'");

}

catch (Throwable ex)

另有一一个多 队列内有所有消费者(饱含什么并没办法 相应RoutingKey的消费者),将平分队列接收到的消息

// int i=101;

ConnectionFactory factory = new ConnectionFactory();

catch (Throwable ex)

System.out.println("Sent msg is '" + message + "'");

{

factory.setPassword(S_RabbitMQ.QUEUE_PWD);// MQ密码

{

factory.setUsername(S_RabbitMQ.QUEUE_USER);// MQ用户名

// 设置消息头键值对信息

factory.setPort(S_RabbitMQ.QUEUE_PORT);// MQ端口

catch (Throwable ex)

System.out.println("1 Waiting for msg....");

{

catch (UnsupportedEncodingException e)

Connection connection = factory.newConnection();

System.out.println("Received end --------------");

// 声明路由名字和类型

};

channel.exchangeDeclare(EXCHANGE_NAME, "fanout", true, false, null);

ConnectionFactory factory = new ConnectionFactory();

e.printStackTrace();

@Override

headers.put("name", "jack");

Consumer consumer = new DefaultConsumer(channel)

public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException

//声明队列

public void handleDelivery(String consumerTag, Envelope envelope,  AMQP.BasicProperties properties, byte[] body) {

{

{

ConnectionFactory factory = new ConnectionFactory();

factory.setPassword(S_RabbitMQ.QUEUE_PWD);// MQ密码

}

ConnectionFactory factory = new ConnectionFactory();

factory.setUsername(S_RabbitMQ.QUEUE_USER);// MQ用户名

// 声明路由名字和类型

ex.printStackTrace();

connection.close();

// 声明路由名字和类型

for (Entry<String, Object> entry : properties.getHeaders().entrySet())

Channel channel = connection.createChannel();

RabbitMQ 3.0事先创建ExChange时,一种生活生活生活类型可选“fanout、direct、topic、headers”。

e.printStackTrace();

channel.basicConsume(QUEUE_NAME, true, consumer);

factory.setPassword(S_RabbitMQ.QUEUE_PWD);// MQ密码

Connection connection = factory.newConnection();

factory.setUsername(S_RabbitMQ.QUEUE_USER);// MQ用户名

}

factory.setPort(S_RabbitMQ.QUEUE_PORT);// MQ端口

// all:表示所有的键值对都匹配要能接受到消息

// 声明路由名字和类型

}

channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);

String message = "";

for (int i = 0; i < 1150; i++)

当向另有一一个多 headers发送另有一一个多 消息时消息会被发送给同另有一一个多 交换机下的拥有相应RoutingKey将会headers的队列,每个队列接收到的消息是一样的;

二、direct

{

factory.setHost(S_RabbitMQ.QUEUE_IP);// MQ主机

String message = "hello world! ";

channel.queueDeclare(QUEUE_NAME, true, false, false, null);



----------------消息消费者----------------

}

System.out.println(" Waiting for msg....");

{

try

message = new String(body, "UTF-8");

for (int i = 0; i < 1150; i++)

System.out.println("msg='" + message + "'");

try

RabbitMQ发送消息时,都不 先把消息发送给ExChange(交换机),并且再整理给有相应RoutingKey(路由)关系的Queue(队列)。

// int i=101;

String message = new String(body, "UTF-8");

byte[] body)

}

Connection connection = factory.newConnection();

// 声明队列

catch (UnsupportedEncodingException e)

----------------消息生产者----------------

}

channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "routingkey1", headers);

channel.queueDeclare(QUEUE_NAME, true, false, false, null);

当向另有一一个多 direct发送另有一一个多 消息时,消息会被发送给同另有一一个多 交换机下的拥有相应RoutingKey的队列,每个队列接收到的消息是一样的;

@Override

}

//绑定路由和队列

//绑定路由和队列// 把队列绑定到路由上并指定headers

factory.setPort(S_RabbitMQ.QUEUE_PORT);// MQ端口

}

// 这里x-match一种生活生活生活类型

factory.setPort(S_RabbitMQ.QUEUE_PORT);// MQ端口

System.out.println("Sent msg is '" + message + "'");

}

// 设置消息头键值对信息

};

factory.setUsername(S_RabbitMQ.QUEUE_USER);// MQ用户名

connection.close();

ConnectionFactory factory = new ConnectionFactory();

Connection connection = factory.newConnection();

Channel channel = connection.createChannel();

System.out.println(" Waiting for msg....");

System.out.println(entry.getKey() + "=" + entry.getValue());

// 把队列绑定到路由上并指定headers

}

System.out.println("Sent msg is '" + message + "'");

channel.exchangeDeclare(EXCHANGE_NAME, "topic", true, false, null);

{

// any:表示要是有键值对匹配就能接受到消息

Channel channel = connection.createChannel();

{

headers.put("age", 150);

Connection connection = factory.newConnection();

connection.close();

factory.setHost(S_RabbitMQ.QUEUE_IP);// MQ主机

factory.setHost(S_RabbitMQ.QUEUE_IP);// MQ主机

{

----------------消息生产者----------------

//声明队列

String message = "";

try

当向另有一一个多 topic发送另有一一个多 消息时消息会被发送给同另有一一个多 交换机下的拥有相应RoutingKey的队列,每个队列接收到的消息是一样的;

Channel channel = connection.createChannel();

//绑定路由和队列

channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "routingkey1", null);

for(int i=0;i<1150;i++)

----------------消息消费者----------------

factory.setHost(S_RabbitMQ.QUEUE_IP);// MQ主机

{

三、topic

@Override

----------------消息消费者----------------

System.out.println("Received start --------------");

factory.setHost(S_RabbitMQ.QUEUE_IP);// MQ主机

channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "routkey2", null);

ex.printStackTrace();