RabbitMQ入门-队列
生活随笔
收集整理的這篇文章主要介紹了
RabbitMQ入门-队列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先建工程
下一步,輸入坐標
下一步,輸入工程名
先看一下最終目錄
修改pom文件
?
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example.demo</groupId><artifactId>rebbitmq-hello</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>com.rabbitmq</groupId><artifactId>amqp-client</artifactId><version>5.1.2</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><!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>1.7.25</version></dependency></dependencies></project>?
Send類:
package com.example.demo;import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory;import java.io.IOException; import java.util.concurrent.TimeoutException;public class Send {private static final String QUEUE_NAME = "hello";public static void main(String[] args) throws IOException, TimeoutException {ConnectionFactory factory = new ConnectionFactory(); // 連接工廠factory.setHost("localhost");Connection connection = factory.newConnection(); // 獲取連接Channel channel = connection.createChannel();channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 聲明隊列,只有他不存在的時候創建String msg = "Hello World!";channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());System.out.println("Sending:" + msg);channel.close();connection.close();}}?
Receive類:
package com.example.demo;import com.rabbitmq.client.*;import java.io.IOException; import java.util.concurrent.TimeoutException;public class Receive {private static final String QUEUE_NAME = "hello";public static void main(String[] args) throws IOException, TimeoutException {ConnectionFactory factory = new ConnectionFactory(); // 連接工廠factory.setHost("localhost");Connection connection = factory.newConnection(); // 獲取連接Channel channel = connection.createChannel();channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 聲明隊列,只有他不存在的時候創建 Consumer consumer = new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {String recv = new String(body, "UTF-8");System.out.println("Receive:" + recv);}};channel.basicConsume(QUEUE_NAME, true, consumer);} }?
先啟動Send,查看控制臺
兔子管控臺
再啟動Receive
兔子管控臺:已經消費掉
?
轉載于:https://www.cnblogs.com/LUA123/p/8471715.html
總結
以上是生活随笔為你收集整理的RabbitMQ入门-队列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联发科正和英伟达合作,在高端手机芯片上整
- 下一篇: centos6.6安装hadoop-2.