【04】RabbitMQ的集群机制

时间:2024-10-27 11:23:23
public class DownStreamConsumer { public static void main(String[] args) throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("192.168.65.112"); factory.setPort(5672); factory.setUsername("admin"); factory.setPassword("admin"); factory.setVirtualHost("/mirror"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare("fed_exchange","direct"); channel.queueDeclare("fed_queue",true,false,false,null); channel.queueBind("fed_queue","fed_exchange","routKey"); Consumer myconsumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("========================"); String routingKey = envelope.getRoutingKey(); System.out.println("routingKey >" + routingKey); String contentType = properties.getContentType(); System.out.println("contentType >" + contentType); long deliveryTag = envelope.getDeliveryTag(); System.out.println("deliveryTag >" + deliveryTag); System.out.println("content:" + new String(body, "UTF-8")); } }; channel.basicConsume("fed_queue", true, myconsumer); } }

相关文章