rabbitMQ學習筆記(二) 簡單的發送與接收訊息 HelloWorld

來源:互聯網
上載者:User

首先要下載rabbitmq的javaClient庫,然後加入到項目中,下載地址為:http://www.rabbitmq.com/releases/rabbitmq-java-client/v3.1.5/rabbitmq-java-client-bin-3.1.5.zip


1、發送訊息

發送訊息首先要擷取與rabbitmq-server的串連,然後從渠道(chann)中指定的queue發送訊息 , 不能定義兩個queue名字相同,但屬性不同

樣本:

Sender01.java

package com.zf.rabbitmq01;import java.io.IOException;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;/** * 發送訊息 * @author zhoufeng * */public class Sender01 {public static void main(String[] args) throws IOException {ConnectionFactory connFac = new ConnectionFactory() ;//RabbitMQ-Server安裝在本機,所以直接用127.0.0.1connFac.setHost("127.0.0.1");//建立一個串連Connection conn = connFac.newConnection() ;//建立一個渠道Channel channel = conn.createChannel() ;//定義Queue名稱String queueName = "queue01" ;//為channel定義queue的屬性,queueName為Queue名稱channel.queueDeclare( queueName , false, false, false, null) ;String msg = "Hello World!";//發送訊息channel.basicPublish("", queueName , null , msg.getBytes());System.out.println("send message[" + msg + "] to "+ queueName +" success!");channel.close(); conn.close(); }}



Recv01.java

package com.zf.rabbitmq01;import java.io.IOException;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;import com.rabbitmq.client.ConsumerCancelledException;import com.rabbitmq.client.QueueingConsumer;import com.rabbitmq.client.QueueingConsumer.Delivery;import com.rabbitmq.client.ShutdownSignalException;/** * 接收訊息 * @author zhoufeng * */public class Recv01 {public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {ConnectionFactory connFac = new ConnectionFactory() ;connFac.setHost("127.0.0.1");Connection conn = connFac.newConnection() ;Channel channel = conn.createChannel() ;String queueName = "queue01";channel.queueDeclare(queueName, false, false, false, null) ;//上面的部分,與Sender01是一樣的//配置好擷取訊息的方式QueueingConsumer consumer = new QueueingConsumer(channel) ;channel.basicConsume(queueName, true, consumer) ;//迴圈擷取訊息while(true){//擷取訊息,如果沒有訊息,這一步將會一直阻塞Delivery delivery = consumer.nextDelivery() ;String msg = new String(delivery.getBody()) ;  System.out.println("received message[" + msg + "] from " + queueName);}}}




此時,無論先後啟動哪個類  ,都沒有關係 ,如果執行Sender01時  Recv01還沒有啟動 , 那麼訊息將被儲存在RabbitMQ-Server上面,直到Recv01啟動後擷取,才會被移除


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.