ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"/*exchange类型为direct*/); channel.basicPublish(EXCHANGE_NAME, "info"/*关键词=info*/, null, message.getBytes()); channel.close(); connection.close();
ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"/*exchange类型为direct*/); // 创建匿名Queue String queueName = channel.queueDeclare().getQueue(); // 订阅某个关键词,绑定到匿名Queue中 channel.queueBind(quueName,EXCHANGE_NAME,"error"); channel.queueBind(quueName,EXCHANGE_NAME,"info"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); QueueingConsumer.Delivery delivery = consumer.nextDelivery(); // Blocking... String message = new String(delivery.getBody()); String routingKey = delivery.getEnvelope().getRoutingKey(); // 可获取路由关键词
ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"/*exchange类型*/); channel.basicPublish(EXCHANGE_NAME, "xxx.yyy"/*关键词routingKey*/, null, message.getBytes()); channel.close(); connection.close();
ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"/*exchange类型*/); // 创建匿名Queue String queueName = channel.queueDeclare().getQueue(); // 订阅某个关键词,绑定到匿名Queue中 channel.queueBind(quueName,EXCHANGE_NAME,"*.yyy"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); QueueingConsumer.Delivery delivery = consumer.nextDelivery(); // Blocking... String message = new String(delivery.getBody()); String routingKey = delivery.getEnvelope().getRoutingKey(); // 可获取路由关键词