java連結rabbitmq需要的jar包連結地址

來源:互聯網
上載者:User

標籤:bit   public   oca   repo   string   port   span   res   監聽   

準備:

  1.下載rabbitmq並搭建環境(和python那篇一樣:http://www.cnblogs.com/g177w/p/8176797.html)

  2.下載支援的jar包(http://repo1.maven.org/maven2/com/rabbitmq/amqp-client)

生產者方(Productor.java):

 1 package RabbitMQTest; 2  3  4 import java.util.HashMap; 5 import java.util.Map; 6  7 import com.rabbitmq.client.AMQP.Queue; 8 import com.rabbitmq.client.Channel; 9 import com.rabbitmq.client.Connection;10 import com.rabbitmq.client.ConnectionFactory;11 12 public class Productor {13 public static String QUEUE_NAME = "STEVEN";14 public static void main(String[] args) {15     //初始化socket連結16     ConnectionFactory factory = new ConnectionFactory(); 17     //指定連結地址18     factory.setHost("localhost");19     try{20     //建立程式和rabbitmq的socket串連21     Connection connection = factory.newConnection();22     //建立管道23     Channel channel = connection.createChannel();24     //聲明隊列25     channel.queueDeclare(QUEUE_NAME,false,false,false,null);26     //讓當前線程睡眠8s,以檢驗rabbitmq的訊息輪詢27     Thread.currentThread().sleep(8000);28     String message = "hello,world";29     //發送訊息30     channel.basicPublish("", QUEUE_NAME, null, message.getBytes());31     System.out.println("[x] send the message"+message);32     channel.close();33     connection.close();34     }catch (Exception e) {35         System.out.println("程式出錯:"+e);36     }37     38 }39 }
View Code

消費者方(Consummer.java):

 1 package RabbitMQTest; 2 import com.rabbitmq.client.Connection; 3 import com.rabbitmq.client.Channel; 4 import com.rabbitmq.client.ConnectionFactory; 5 import com.rabbitmq.client.QueueingConsumer; 6 import com.rabbitmq.client.QueueingConsumer.Delivery; 7 /** 8  * 本程式為了練習rabbitmq的簡單操作 9  * 10  * @author STEVEN11  * 12  */13 public class Consumer {14     public static void main(String[] args) {15         try {16             //建立串連工廠對象17             ConnectionFactory factory = new ConnectionFactory();18             //設定工廠對象的參數,用來串連rabbitmq19             factory.setHost("localhost");20             //建立程式與rabbitmq的socket串連21             Connection connection = factory.newConnection();22             //建立管道23             Channel channel = connection.createChannel();24             //聲明隊列25             channel.queueDeclare(Productor.QUEUE_NAME, false, false, false, null);26             System.out.println(" [*] Waiting for messages. To exit press CTRL+C");27             //建立隊列消費對象28             QueueingConsumer consumer = new QueueingConsumer(channel);29             //設定參數30             channel.basicConsume(Productor.QUEUE_NAME, true, consumer);31             //建立接收對象來接收來自服務端的訊息32             Delivery delivery = null;33             //迴圈接收,相當於開啟了一個監聽34             while (true) {35                 delivery = consumer.nextDelivery();36                 String message = new String(delivery.getBody());37                 System.out.println(" [x] Received ‘" + message + "‘");38             }39         } catch (Exception e) {40             System.out.println(e);41         }42     }43 }
View Code

 

java連結rabbitmq需要的jar包連結地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.