上一篇文章提到了rabbitMQ的體系結(jié)構(gòu)和一些核心概念阐滩,這篇文章就通過一個最簡單的Java版helloWorld實(shí)例來看實(shí)際代碼中這些概念的體現(xiàn)枫浙。
前期準(zhǔn)備
1询筏、在自己的電腦上安裝rabbitMQ server
2绵脯、下載rabbitMQ 的java API渊迁,就是一個jar包,并在工程中導(dǎo)入
introduction
上一篇說到MQ解決的主要問題是降低系統(tǒng)之間剃盾、模塊之間的耦合度腺占,在這個層面上,可以將rabbitMQ理解為一個message broker,producer將message發(fā)送給rabbitMQ痒谴,rabbitMQ做為中間方將message傳遞給consumer衰伯,從而使producer和consumer之間解耦。message在兩者之間傳遞的時候可以依據(jù)相應(yīng)的規(guī)則進(jìn)行路由积蔚,緩存甚至持久化
producer可以簡單理解為發(fā)送消息的程序意鲸,在本文的圖像中以下圖代表producer:
queue是模塊間通信的“信箱”,其生命周期只存在于rabbitMQ server中尽爆,本質(zhì)而言怎顾,queue就是一個無窮緩沖隊(duì)列,一個或者多個producer/consumer都可以無限制的連接上mq漱贱,這里說的無限制是在數(shù)量上而言槐雾,連接MQ server還是要遵循rabbitMQ各種語言的API的規(guī)定。在本文的圖像中以下圖代表queue:
consumer就是連接在MQ上等待傳送過來進(jìn)行消費(fèi)的:
note:producer consumer 和queue不一定存在于同一臺機(jī)器上幅狮,并且在大多數(shù)情況下是不在同一臺機(jī)器上的募强。對于大型系統(tǒng)而言,各個模塊很有可能部署在不同機(jī)器上彪笼,模塊和模塊之間的通信遠(yuǎn)比同一臺機(jī)器上的模塊之間通信復(fù)雜钻注,在實(shí)際生產(chǎn)環(huán)境中運(yùn)用MQ也會有各種各樣的問題與挑戰(zhàn)蚂且。
傳遞一個最簡單的HelloWorld
這部分我們不去深究rabbitMQ java api的細(xì)節(jié)配猫,僅僅實(shí)現(xiàn)一個簡單的模型
producer將一個message發(fā)送到queue中,queue將message傳遞給consumer供其消費(fèi)杏死。在本篇的程序中consumer僅僅將message打印出來泵肄。
直接上代碼
producer的代碼:
public class Sender {
private final static String QUEUE_NAME = "hello";
public static void main(String[] argv) throws Exception {
//create a connection to a server
// The connection abstracts the socket connection, and takes care of protocol version negotiation and
// authentication and so on for us. Here we connect to a broker on the local machine - hence the locallhost.
// If we wanted to connect to a broker on a different machine we'd simply specify its name or IP address here.
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
// Next we create a channel, which is where most of the API for getting things done resides.
Channel channel = connection.createChannel();
// To send, we must declare a queue for us to send to; then we can publish a message to the queue:
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!";
//publish the message to the queue
channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();
}
}
consumer的代碼:
public class Receiver {
private static final String QUEUE_NAME = "hello";
private static final String HOST_ADDRESS = "localhost";
public static void main(String[] args) throws IOException, TimeoutException {
//Setting up is the same as the sender;
// we open a connection and a channel, and declare the queue from which we're going to consume.
// Note this matches up with the queue that send publishes to.
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(HOST_ADDRESS);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// Note that we declare the queue here, as well.
// Because we might start the receiver before the sender,
// we want to make sure the queue exists before we try to consume messages from it.
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println("[*] waiting for message .To exit press CTRL+C");
// We're about to tell the server to deliver us the messages from the queue.
// Since it will push us messages asynchronously, we provide a callback in the form of an object that
// will buffer the messages until we're ready to use them. That is what a DefaultConsumer subclass does.
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);
}
}