RabbitMq 學習 第一課
一 RabbitMq的重要概念
- Exchanges :交換器,必須的作用類似於轉換器,可以將消息通過routeKey轉發到對應的隊列中;
- routeKey:路由鍵,類似於路牌,生產着將要發送的消息發到交換器上,並指定路由鍵,然後交換器通過路由鍵將消息發送到對應的queue中;
- Queue:消息隊列。消息的載體。
- Binding:作用就是將Exchange和Queue按照某種路由規則綁定起來。
二 如何使用rabbitmq提供的客戶端
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
- 創建一個ConnectionFactory,並設置一系列的屬性
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.0.107");//設置主機名稱
connectionFactory.setPort(5672);//端口
connectionFactory.setUsername("mqadmin");//用戶名
connectionFactory.setPassword("mqadminpassword");//密碼
connectionFactory.setConnectionTimeout(3000);//超時時間
- 通過connectionFactory創建一個連接,通過該鏈接創建一個Channel,所有的操作都是基於Channel進行的(工廠模式無處不在)
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
- 創建一個隊列,使用queueDeclareI(),至於各個參數可以自己測驗一下
/**
* Declare a queue
* @see com.rabbitmq.client.AMQP.Queue.Declare
* @see com.rabbitmq.client.AMQP.Queue.DeclareOk
* @param queue the name of the queue 隊列名稱,隨意寫
* @param durable true if we are declaring a durable queue (the queue will survive a server restart) 是否持久化,true:持久化,就是重啓服務之後隊列任然存在,但是消息不回存在。
* @param exclusive true if we are declaring an exclusive queue (restricted to this connection) 是否只有該創建這才能使用該隊列。
* @param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use) 當長時間沒有連接的時候是否自動刪除。
* @param arguments other properties (construction arguments) for the queue 其他屬性 Map對象
* @return a declaration-confirm method to indicate the queue was successfully declared
* @throws java.io.IOException if an error is encountered
*/
channel.queueDeclare("queueName",true,false,false,null);
- 創建一個Exchange 參數意思都差不多
/**
* Declare an exchange.
* @see com.rabbitmq.client.AMQP.Exchange.Declare
* @see com.rabbitmq.client.AMQP.Exchange.DeclareOk
* @param exchange the name of the exchange
* @param type the exchange type 交換器的類型,常用的topic,和direct,fanout,其他的有headers,接下來會對其作解釋,並舉例,代碼說明
* @param durable true if we are declaring a durable exchange (the exchange will survive a server restart)
* @param autoDelete true if the server should delete the exchange when it is no longer in use
* @param arguments other properties (construction arguments) for the exchange
* @return a declaration-confirm method to indicate the exchange was successfully declared
* @throws java.io.IOException if an error is encountered
*/
channel.exchangeDeclare("exchangeName","topic",true,false,null);
- exchange創建玩成之後,需要將queue與exchange綁定。
/**
* Bind a queue to an exchange, with no extra arguments.
* @see com.rabbitmq.client.AMQP.Queue.Bind
* @see com.rabbitmq.client.AMQP.Queue.BindOk
* @param queue the name of the queue
* @param exchange the name of the exchange
* @param routingKey the routing key to use for the binding
* @return a binding-confirm method if the binding was successfully created
* @throws java.io.IOException if an error is encountered
*/
channel.queueBind("queueName","exchangeName","routeKey");
- 發送消息,並不需要指明隊列是哪個,只通過交換器和routeKey來發消息
/**
* Publish a message.
*
* Publishing to a non-existent exchange will result in a channel-level
* protocol exception, which closes the channel.
*
* Invocations of <code>Channel#basicPublish</code> will eventually block if a
* <a href="http://www.rabbitmq.com/alarms.html">resource-driven alarm</a> is in effect.
*
* @see com.rabbitmq.client.AMQP.Basic.Publish
* @see <a href="http://www.rabbitmq.com/alarms.html">Resource-driven alarms</a>
* @param exchange the exchange to publish the message to
* @param routingKey the routing key
* @param props other properties for the message - routing headers etc
* @param body the message body
* @throws java.io.IOException if an error is encountered
*/
channel.basicPublish("exchange Name","routingKey", MessageProperties.TEXT_PLAIN,"hello world".getBytes());
- 消費消息 主要注意的是Consumer,你可以自己定義一個類並實現Consumer接口。 通過void handleDelivery(String consumerTag,
Envelope envelope,
AMQP.BasicProperties properties,
byte[] body)
throws IOException; 該方法對隊列的消息進行其他的操作,body 消息內容。
/**
* Start a non-nolocal, non-exclusive consumer, with
* explicit acknowledgement and a server-generated consumerTag.
* @param queue the name of the queue
* @param callback an interface to the consumer object
* @return the consumerTag generated by the server
* @throws java.io.IOException if an error is encountered
* @see com.rabbitmq.client.AMQP.Basic.Consume
* @see com.rabbitmq.client.AMQP.Basic.ConsumeOk
* @see #basicAck
* @see #basicConsume(String, boolean, String, boolean, boolean, Map, Consumer)
*/
String basicConsume(String queue, Consumer callback) throws IOException;
以上一個比較簡單的例子就完成了。接下來的文章將會對四種模式進行說明。