1、引入依赖
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mine</groupId>
<artifactId>rabbitmq-basic</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.5</version>
</dependency>
</dependencies>
</project>
2、工具类
MQConnectionUtils
package com.mine.utils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class MQConnectionUtils {
public static Connection newConnection() throws IOException, TimeoutException {
// 1.定义连接工厂
ConnectionFactory factory = new ConnectionFactory();
// 2.设置服务器地址
factory.setHost("127.0.0.1");
// 3.设置协议端口号
factory.setPort(5672);
// 4.设置vhost
factory.setVirtualHost("/test");
// 5.设置用户名称
factory.setUsername("test");
// 6.设置用户密码
factory.setPassword("test");
// 7.创建新的连接
Connection newConnection = factory.newConnection();
return newConnection;
}
}
3、生产者
MsgProducer
package com.mine.producer;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.mine.utils.MQConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
public class MsgProducer {
private static final String QUEUE_NAME = "test_queue";
public static void main(String[] args) throws IOException, TimeoutException {
// 1、获取连接
Connection connection = MQConnectionUtils.newConnection();
// 2、创建通道
Channel channel = connection.createChannel();
// 3、创建队列声明
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 4、发送消息
for (int i = 1; i <= 100; i++) {
String msg = "我是消息内容" + i;
System.out.println("发送消息:" + msg);
channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
}
// 5、关闭资源
channel.close();
connection.close();
}
}
4、消费者
MsgConsumer1
package com.mine.consumer;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.mine.utils.MQConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
public class MsgConsumer1 {
private static final String QUEUE_NAME = "test_queue";
public static void main(String[] args) throws IOException, TimeoutException {
System.out.println("消费者1启动。。。");
// 1、获取连接
Connection connection = MQConnectionUtils.newConnection();
// 2、创建通道
final Channel channel = connection.createChannel();
// 3、监听消息
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
// 打印接收消息
long t = System.currentTimeMillis();
System.out.println(t + "接收到消息:" + new String(body, "UTF-8"));
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
// 每次只发送一个消息
channel.basicQos(1);
channel.basicConsume(QUEUE_NAME, false, consumer);
}
}
MsgConsumer2
package com.mine.consumer;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.mine.utils.MQConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
public class MsgConsumer2 {
private static final String QUEUE_NAME = "test_queue";
public static void main(String[] args) throws IOException, TimeoutException {
System.out.println("消费者2启动。。。");
// 1、获取连接
Connection connection = MQConnectionUtils.newConnection();
// 2、创建通道
final Channel channel = connection.createChannel();
// 3、监听消息
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
// 打印接收消息
long t = System.currentTimeMillis();
System.out.println(t + "接收到消息:" + new String(body, "UTF-8"));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
// 每次只发送一个消息
channel.basicQos(1);
channel.basicConsume(QUEUE_NAME, false, consumer);
}
}