1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| package com.test.routing;
import com.rabbitmq.client.BuiltinExchangeType; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory;
public class Main { public static final String QUEUE_NAME_2 = "routing_queue1";
public static final String QUEUE_NAME_3 = "routing_queue2";
public static final String EXCHANGE_NAME = "routing_exchange";
public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("localhost"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/"); connectionFactory.setUsername("huya"); connectionFactory.setPassword("huya"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME_2, true, false, false, null); channel.queueDeclare(QUEUE_NAME_3, true, false, false, null);
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT, true, true, null);
channel.queueBind(QUEUE_NAME_2, EXCHANGE_NAME, "error"); channel.queueBind(QUEUE_NAME_3, EXCHANGE_NAME, "error"); channel.queueBind(QUEUE_NAME_2, EXCHANGE_NAME, "info"); channel.queueBind(QUEUE_NAME_2, EXCHANGE_NAME, "warning"); String message = "hello";
channel.basicPublish(EXCHANGE_NAME, "error", null, message.getBytes()); channel.basicPublish(EXCHANGE_NAME, "info", null, message.getBytes()); channel.basicPublish(EXCHANGE_NAME, "warning", null, message.getBytes()); channel.close(); connection.close(); } }
|