创建rabbitMQ的连接
public class ConnectionUtil {
public static Connection getConnection() throws IOException, TimeoutException {
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务器地址
connectionFactory.setHost("127.0.0.1");
//设置端口
connectionFactory.setPort(5672);
//设置vHost 就相当于mysql的数据库名
connectionFactory.setVirtualHost("/vhost_admin");
//设置用户名
connectionFactory.setUsername("admin");
connectionFactory.setPassword("admin");
return connectionFactory.newConnection();
}
}
-
简单队列
生产者
public class Send {
public static final String QUEUE_NAME="test_simple_queue";
public static void main(String[] args) throws Exception {
//1.获取连接
Connection connection = ConnectionUtil.getConnection();
//创建频道
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
String msg="I have no money";
channel.basicPublish("",QUEUE_NAME,null,msg.getBytes());
channel.close();
connection.close();
}
}
消费者
public class Rece {
public static final String QUEUE_NAME = "test_simple_queue";
public static void main(String[] args) throws Exception {
// oldMethod();
newMethod();
}
private static void newMethod()throws Exception{
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(new String(body));
}
};
//监听队列
channel.basicConsume(QUEUE_NAME,true,defaultConsumer);
}
static void oldMethod() throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, true, queueingConsumer);
while (2 > 1) {
QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
byte[] body = delivery.getBody();
System.out.println(new String(body));
}
}
}
-
工作队列
生产者
public class Send {
//工作队列的生产者
public static final String QUEUE_NAME="test_work_queue";
public static void main(String[] args) throws Exception{
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
for (int i = 0; i < 50; i++) {
String msg="hello rabbit"+i;
channel.basicPublish("",QUEUE_NAME,null,msg.getBytes());
Thread.sleep(i*20);
}
channel.close();
connection.close();
}
}
消费者1
public class Rece1 {
public static final String QUEUE_NAME="test_work_queue";
public static void main(String[] args)throws IOException, TimeoutException {
//1.获取连接
Connection connection = ConnectionUtil.getConnection();
//2.获取频道
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare("QUEUE_NAME",false,false,false,null);
DefaultConsumer consumer = new DefaultConsumer(channel) {
//消息到达触发这个方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(new String(body));
try {
Thread.sleep(200);
}catch (InterruptedException e){
e.printStackTrace();
}
}
};
boolean ack=true;
channel.basicConsume(QUEUE_NAME,ack,consumer);
}
}
消费者2
public class Rece2 {
public static final String QUEUE_NAME = "test_work_queue";
public static void main(String[] args) throws IOException, TimeoutException {
//1.获取连接
Connection connection = ConnectionUtil.getConnection();
//2.获取频道
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare("QUEUE_NAME", false, false, false, null);
DefaultConsumer consumer = new DefaultConsumer(channel) {
//消息到达触发这个方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(new String(body));
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
//设置自动应答 指的是在接收到一条消息后的返回的应答信息
boolean ack = true;
channel.basicConsume(QUEUE_NAME, ack, consumer);
}
}
- 参数说明
autoAck=true:设置自动应答为true,表示一旦rabbitMQ将消息发送给消费者,就会在内存中删除这条消息(这种情况下,如果杀死正在执行的消费者,就会丢失正在处理的消息)
autoAck=false:这个时候如果正在处理这个消息的消费者挂掉,则rabbitMQ收不到这个回执信息,就会将这个条信息发送给下一个消费者,如果收到了应答信息,再将这条信息在内存中进行删除,默认为false。channel.queueDeclare(“QUEUE_NAME”, durable, false, false, null):
“QUEUE_NAME”:队列名
durable:数据持久化
解决方法:重新声明一个队列名,或者直接在控制台将这个队列删除即可
- 订阅模式
生产者将消息发送到交换机上,然后每个消费者都有自己的消息队列,每个消费者在自己的队列上获取信息
rabbitMQ中交换机是没有存储能力的,只有队列有存储能力,若只要生产者将信息发送到交换机,消费者消费,那么存储的
生产者
public class ExchangeSend {
private static final String EXCHANGE_NAME="test_exchange_name";
public static void main(String[] args)throws Exception {
//将数据发送到交换机上
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
//声明交换机
channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
String msg="this is exchange msg";
channel.basicPublish(EXCHANGE_NAME,"",null,msg.getBytes());
channel.close();
connection.close();
}
}
消费者1
public class ExchangeRece1 {
private static final String QUEUE_NAME="test_queue_name_email";
private static final String EXCHANGE_NAME="test_exchange_name";
public static void main(String[] args) throws Exception{
Connection connection = ConnectionUtil.getConnection();
final Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
//绑定队列到交换机
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"");
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(new String(body));
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
//自动应答设置为false
boolean ack=false;
channel.basicConsume(QUEUE_NAME,ack,consumer);
}
}
消费者2
public class ExchangeRece2 {
private static final String QUEUE_NAME="test_queue_name_sms";
private static final String EXCHANGE_NAME="test_exchange_name";
public static void main(String[] args) throws Exception{
Connection connection = ConnectionUtil.getConnection();
final Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
//绑定队列到交换机
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"");
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(new String(body));
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
//自动应答设置为false
boolean ack=false;
channel.basicConsume(QUEUE_NAME,ack,consumer);
}
}
来源:CSDN
作者:forever__@
链接:https://blog.csdn.net/qq_39289502/article/details/103800063