基本队列:Producer直接发送信息到Queue中,Consumer接收Queue发送过来的信息
简而言之,一个生产者发送信息,一个消费者接收信息。
获取连接工具类:
package com.example.demo.utils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class ConnectionUtil {
public static Connection getConnection() throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
// connectionFactory.setVirtualHost("");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory.newConnection();
}
}
生产者:
package com.example.demo.queue.simple;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.example.demo.utils.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
public class Producer {
// 队列名称
private static final String QUEUE_NAME="simple_queue";
public static void main(String[] args) {
Connection connection = null;
Channel channel = null;
try {
// 获取连接
connection = ConnectionUtil.getConnection();
// 创建通道
channel = connection.createChannel();
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 生产者发送的信息
String sendMsg = "msg from producer";
// 发送信息
channel.basicPublish("", QUEUE_NAME, null, sendMsg.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭通道
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
// 关闭连接
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
消费者:
package com.example.demo.queue.simple;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.example.demo.utils.ConnectionUtil;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
public class Consumer {
// 队列名称
private static final String QUEUE_NAME="simple_queue";
public static void main(String[] args) {
Connection connection = null;
Channel channel = null;
try {
// 获取连接
connection = ConnectionUtil.getConnection();
// 创建通道
channel = connection.createChannel();
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body,"UTF-8");
System.out.println("receive msg:"+msg);
}
};
// 接收信息
channel.basicConsume(QUEUE_NAME, true, consumer);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
执行方法:右键->Run as->Java Application
执行顺序:Producer.main()->Consumer.main()
执行Producer的main方法之后,打开rabbitmq的管理界面,切换到Queues
图中圈出的队列,说明,生产者(Producer)已经成功的发送信息到rabbitmq的队列之中了。
接下来,我们执行消费者(Consumer)的main方法,如果想看到消费者收到信息,可以把消费者类中的通道,连接关闭代码先注释掉,即可看到收到的信息。