RabbitMQ简单收发,HelloWorld
前言
接触RabbitMQ有一周多的时间了,对RabbitMQ有了自己的一些粗浅的见解,想和大家分享下,本文先讲解简单的RabbitMQ收发操作,没有做任何的处理,参考了官网的资料进行编写,并加入个人的一些见解,不对之处请多多指教。
一.定义
二.角色定义
在这简单的收发模型中有三个角色分别是生产者(producer)、消息队列(message queue)、消费者(consummer)
a、生产者,主要是发送消息给消息队列
b、消息队列,它存在于RabbitMQ中,所有的信息都会存放到消息队列中,遵循先进先出的存储方式,当接受到请求,将数据发送出去,如果接到已被消费的ACK(回执),这该消息将从队列中移除。
c、消费者,从消息队列中获取数据,并返回ACK给RabbitMQ。
这是简单的收发过程图。
三.预置条件
1.下载Erlang
http://docs.basho.com/riak/1.3.0/tutorials/installation/Installing-Erlang/
2.下载RabbitMQ http://www.rabbitmq.com/releases/rabbitmq-server/v3.3.4/rabbitmq-server-3.3.4.exe
3.下载依赖包 amqp-client-4.0.2.jar slf4j-api-1.7.21.jar slf4j-simple-1.7.22.jar
四.sending
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
* 发送消息端
* @author sibat
*
*/
public class Send {
private final static String QUEUE_NAME = "hello";
public static void main(String[] args) {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
factory.setPort(5672);
Connection connection = null;
Channel channel = null;
try {
connection = factory.newConnection();
channel= connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false,false,null);
String msg = "I am comming";
channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
System.out.println("producer send " + "'"+ msg + "'");
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}finally{
if (channel != null) {
try {
channel.close();
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
五.Receiving
有一个函数要注意的是channel.basicConsume(),第二个参数为true,意思是自动返回ACK,即queue在传递信息给consumer后就会立马收到ACK,queue不再对这个消息进行存储了。
channel.basicConsume(QUEUE_NAME, true, consumer);
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;
public class Recv {
private static final String QUEUE_NAME = "hello";
public static void main(String[] args) throws IOException, TimeoutException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
//设置MabbitMQ所在主机ip或者主机名
factory.setHost("127.0.0.1");
factory.setPort(5672);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
//创建队列消费者
Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag,
Envelope envelope, BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [x] Received '" + message + "'");
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
五.输出结果
producer send 'I am comming'
接收端:
[*] Waiting for messages. To exit press CTRL+C
[x] Received 'I am comming'