1. 程式人生 > >java鏈接rabbitmq需要的jar包鏈接地址

java鏈接rabbitmq需要的jar包鏈接地址

pan rabbit spa local 簡單 建立 tde actor 創建

準備:

  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 STEVEN
11  * 
12  */
13 public class Consumer {
14     public static void main(String[] args) {
15         try {
16             //創建連接工廠對象
17             ConnectionFactory factory = new ConnectionFactory();
18             //設置工廠對象的參數,用來連接rabbitmq
19             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包鏈接地址