

1.加入依赖
1 2 3 4 5
| <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <scope>3.6.5</scope> </dependency>
|
2.编码实战
2.1Productor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class Procuder { public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("94.191.24.33"); connectionFactory.setPort(15672); connectionFactory.setVirtualHost("/"); Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
String msg = "Hello RabbitMQ"; channel.basicPublish("", "test001", null, msg.getBytes()); channel.close(); connection.close(); }
|

2.2.Consumer
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
| public class Consumer {
public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("94.191.24.33"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
String queueName = "test001"; channel.queueDeclare("test001", true, false, false, null);
QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, true, queueingConsumer);
while (true) { Delivery delivery = queueingConsumer.nextDelivery(); String msg = new String(delivery.getBody()); System.out.println("消费端" + msg); } } }
|

2.3 启动程序看管控台




2.4 方法的API
3.一些基础




Author:
John Doe
Permalink:
http://yoursite.com/2018/12/05/消息队列/RabbitMQ/RabbitMQ慕课网教程/4消费生产与消费/
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
Do you believe in DESTINY?