千里水天一色,看孤鸿明灭。
——朱郭儒《好事近》
RabbitMQ入门
1. 搭建示例工程
1.1 创建工程
1.2 添加依赖
往heima-rabbitmq的pom.xml文件中添加如下依赖:
1 2 3 4 5
| <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.6.0</version> </dependency>
|
2. 编写生产者
编写消息生产者com.itheima.producer.Producer_HelloWorld;
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| package com.itheima.producer;
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException; import java.util.concurrent.TimeoutException;
public class Producer_HelloWorld { public static void main(String[] args) throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory();
factory.setHost("106.15.72.229"); factory.setPort(5672); factory.setVirtualHost("/itcast"); factory.setUsername("heima"); factory.setPassword("heima"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel();
channel.queueDeclare("hello_world", true, false, false, null);
String body = "hello rabbitmq~~~";
channel.basicPublish("", "hello_world", null, body.getBytes());
channel.close(); connection.close();
} }
|
在执行上述的消息发送之后;可以登录rabbitMQ的管理控制台,可以发现队列和其消息:
3. 编写消费者
编写消息的消费者com.itheima.consumer.Consumer_HelloWorld;
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| package com.itheima.consumer;
import com.rabbitmq.client.*;
import java.io.IOException; import java.util.concurrent.TimeoutException;
public class Consumer_HelloWorld { public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory(); factory.setHost("106.15.72.229"); factory.setPort(5672); factory.setVirtualHost("/itcast"); factory.setUsername("heima"); factory.setPassword("heima"); Connection connection = factory.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("consumerTag:"+consumerTag); System.out.println("Exchange:"+envelope.getExchange()); System.out.println("RoutingKey:"+envelope.getRoutingKey()); System.out.println("properties:"+properties); System.out.println("body:"+new String(body)); } }; channel.basicConsume("hello_world",true,consumer);
} }
|
4. 小结
抽取创建connection的工具类com.itheima.util.ConnectionUtil;
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
| package com.itheima.rabbitmq.util;
import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory;
public class ConnectionUtil {
public static Connection getConnection() throws Exception { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("106.15.72.229"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/itcast"); connectionFactory.setUsername("heima"); connectionFactory.setPassword("heima");
return connectionFactory.newConnection(); } }
|
上述的入门案例中中其实使用的是如下的简单模式:
在上图的模型中,有以下概念:
- P:生产者,也就是要发送消息的程序
- C:消费者:消息的接受者,会一直等待消息到来。
- queue:消息队列,图中红色部分。类似一个邮箱,可以缓存消息;生产者向其中投递消息,消费者从其中取出消息。
☆