基本隊列:Producer直接發送信息到Queue中,Consumer接收Queue發送過來的信息java
簡而言之,一個生產者發送信息,一個消費者接收信息。ide
獲取鏈接工具類:工具
package com.example.demo.utils; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class ConnectionUtil { public static Connection getConnection() throws IOException, TimeoutException { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("127.0.0.1"); connectionFactory.setPort(5672); // connectionFactory.setVirtualHost(""); connectionFactory.setUsername("guest"); connectionFactory.setPassword("guest"); return connectionFactory.newConnection(); } }
生產者:code
package com.example.demo.queue.simple; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.example.demo.utils.ConnectionUtil; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; public class Producer { // 隊列名稱 private static final String QUEUE_NAME="simple_queue"; public static void main(String[] args) { Connection connection = null; Channel channel = null; try { // 獲取鏈接 connection = ConnectionUtil.getConnection(); // 建立通道 channel = connection.createChannel(); // 聲明隊列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 生產者發送的信息 String sendMsg = "msg from producer"; // 發送信息 channel.basicPublish("", QUEUE_NAME, null, sendMsg.getBytes()); } catch (Exception e) { e.printStackTrace(); } finally { // 關閉通道 try { channel.close(); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } // 關閉鏈接 try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } }
消費者:blog
package com.example.demo.queue.simple; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.example.demo.utils.ConnectionUtil; import com.rabbitmq.client.AMQP.BasicProperties; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; public class Consumer { // 隊列名稱 private static final String QUEUE_NAME="simple_queue"; public static void main(String[] args) { Connection connection = null; Channel channel = null; try { // 獲取鏈接 connection = ConnectionUtil.getConnection(); // 建立通道 channel = connection.createChannel(); // 聲明隊列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 定義消費者 DefaultConsumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String msg = new String(body,"UTF-8"); System.out.println("receive msg:"+msg); } }; // 接收信息 channel.basicConsume(QUEUE_NAME, true, consumer); } catch (Exception e) { e.printStackTrace(); } finally { try { channel.close(); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } }
執行方法:右鍵->Run as->Java Applicationrabbitmq
執行順序:Producer.main()->Consumer.main()隊列
執行Producer的main方法以後,打開rabbitmq的管理界面,切換到Queuesget
圖中圈出的隊列,說明,生產者(Producer)已經成功的發送信息到rabbitmq的隊列之中了。it
接下來,咱們執行消費者(Consumer)的main方法,若是想看到消費者收到信息,能夠把消費者類中的通道,鏈接關閉代碼先註釋掉,便可看到收到的信息。io