1、简单生产者与消费者(1对1)
生产者代码
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory connectionFactory=new ConnectionFactory();
connectionFactory.setHost("192.168.1.103");
connectionFactory.setUsername("heima");
connectionFactory.setPassword("heima");
connectionFactory.setVirtualHost("/heima");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("hello_world",true,false,false,null);
String n="nihaoaaaa";
channel.basicPublish("","hello_world",null,n.getBytes());
channel.close();
connection.close();
}
消费者
public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory=new ConnectionFactory(); connectionFactory.setHost("192.168.1.103"); connectionFactory.setUsername("heima"); connectionFactory.setPassword("heima"); connectionFactory.setVirtualHost("/heima"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("hello_world",true,false,false,null); Consumer consumer=new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("conment=="+new String(body)); } }; channel.basicConsume("hello_world",true,consumer); }
2、竞争消费者(1对2)
代码与上边一样,就是加了个消费者
3、发布订阅模式
生产者
public static void main(String[] args) throws IOException, TimeoutException { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("192.168.1.103"); connectionFactory.setUsername("heima"); connectionFactory.setPassword("heima"); connectionFactory.setVirtualHost("/heima"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); String exchangename = "test_FANOUT"; /* String exchange, BuiltinExchangeType type, boolean durable, boolean autoDelete, boolean internal, Map<String, Object */ channel.exchangeDeclare(exchangename, BuiltinExchangeType.FANOUT, true, false, false, null); /* String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments */ String queue1 = "test_pub1"; String queue2 = "test_pub2"; channel.queueDeclare(queue1, true, false, false, null); channel.queueDeclare(queue2, true, false, false, null); /* String queue, String exchange, String routingKey, Map */ channel.queueBind(queue1, exchangename, ""); channel.queueBind(queue2, exchangename, ""); String n = "日志信息:调用了findall()方法"; /* String exchange, String routingKey, BasicProperties props, byte[] body */ channel.basicPublish(exchangename, "", null, n.getBytes()); channel.close(); connection.close(); }
消费者
public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory=new ConnectionFactory(); connectionFactory.setHost("192.168.1.103"); connectionFactory.setUsername("heima"); connectionFactory.setPassword("heima"); connectionFactory.setVirtualHost("/heima"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("test_pub1",true,false,false,null); Consumer consumer=new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("打印日志信息:info...."); } }; channel.basicConsume("test_pub1",true,consumer); }
public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory=new ConnectionFactory(); connectionFactory.setHost("192.168.1.103"); connectionFactory.setUsername("heima"); connectionFactory.setPassword("heima"); connectionFactory.setVirtualHost("/heima"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("test_pub2",true,false,false,null); Consumer consumer=new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("存入到数据库。。。。"); } }; channel.basicConsume("test_pub2",true,consumer); }
4、DIRECT方式,类似于订阅发布,但是都可以接受,可以各干相应各的
生产者
public static void main(String[] args) throws IOException, TimeoutException { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("192.168.1.103"); connectionFactory.setUsername("heima"); connectionFactory.setPassword("heima"); connectionFactory.setVirtualHost("/heima"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); String exchangename = "test_DIRECT"; /* String exchange, BuiltinExchangeType type, boolean durable, boolean autoDelete, boolean internal, Map<String, Object */ channel.exchangeDeclare(exchangename, BuiltinExchangeType.DIRECT, true, false, false, null); /* String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments */ String queue1 = "test_pub11"; String queue2 = "test_pub22"; channel.queueDeclare(queue1, true, false, false, null); channel.queueDeclare(queue2, true, false, false, null); /* String queue, String exchange, String routingKey, Map */ channel.queueBind(queue1, exchangename, "info"); channel.queueBind(queue1, exchangename, "warning"); channel.queueBind(queue2, exchangename, "error"); String n = "日志信息:调用了findall()方法"; /* String exchange, String routingKey, BasicProperties props, byte[] body */ channel.basicPublish(exchangename, "error", null, n.getBytes()); channel.close(); connection.close(); }
消费者
public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory=new ConnectionFactory(); connectionFactory.setHost("192.168.1.103"); connectionFactory.setUsername("heima"); connectionFactory.setPassword("heima"); connectionFactory.setVirtualHost("/heima"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("test_pub11",true,false,false,null); Consumer consumer=new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("打印日志信息:info...."); } }; channel.basicConsume("test_pub11",true,consumer); }
public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory=new ConnectionFactory(); connectionFactory.setHost("192.168.1.103"); connectionFactory.setUsername("heima"); connectionFactory.setPassword("heima"); connectionFactory.setVirtualHost("/heima"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("test_pub22",true,false,false,null); Consumer consumer=new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("存入到数据库。。。。"); } }; channel.basicConsume("test_pub22",true,consumer); }
5、TOPIC方式,采用通配符
生产者
public static void main(String[] args) throws IOException, TimeoutException { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("192.168.1.103"); connectionFactory.setUsername("heima"); connectionFactory.setPassword("heima"); connectionFactory.setVirtualHost("/heima"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); String exchangename = "test_DIRECT"; /* String exchange, BuiltinExchangeType type, boolean durable, boolean autoDelete, boolean internal, Map<String, Object */ channel.exchangeDeclare(exchangename, BuiltinExchangeType.TOPIC, true, false, false, null); /* String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments */ String queue1 = "test_pub11"; String queue2 = "test_pub22"; channel.queueDeclare(queue1, true, false, false, null); channel.queueDeclare(queue2, true, false, false, null); /* String queue, String exchange, String routingKey, Map */ channel.queueBind(queue1, exchangename, "#.error"); channel.queueBind(queue1, exchangename, "error.*"); channel.queueBind(queue2, exchangename, "#.#"); String n = "日志信息:调用了findall()方法"; /* String exchange, String routingKey, BasicProperties props, byte[] body */ channel.basicPublish(exchangename, "order.1", null, n.getBytes()); channel.close(); connection.close(); }
消费者
public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory=new ConnectionFactory(); connectionFactory.setHost("192.168.1.103"); connectionFactory.setUsername("heima"); connectionFactory.setPassword("heima"); connectionFactory.setVirtualHost("/heima"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("test_pub11",true,false,false,null); Consumer consumer=new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("打印日志信息:info...."); } }; channel.basicConsume("test_pub11",true,consumer); }
public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory=new ConnectionFactory(); connectionFactory.setHost("192.168.1.103"); connectionFactory.setUsername("heima"); connectionFactory.setPassword("heima"); connectionFactory.setVirtualHost("/heima"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("test_pub22",true,false,false,null); Consumer consumer=new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("存入到数据库。。。。"); } }; channel.basicConsume("test_pub22",true,consumer); }