這是官網教程,https://www.rabbitmq.com/tutorials/tutorial-one-java.html
用谷歌瀏覽器將網頁翻譯成中文看即可,這裏就簡單指一下消息傳遞過程
生產者 --> 交換器 --> 隊列 --> 消費者
生產者生成消息,然後將消息轉發到相應交換器中,通過一定規則匹配或綁定,交換器把消息轉發到對應的隊列中,隊列也會通過部分規則將消息發送個消費者。由於生產者、隊列、消費者都可以是多個存在的,加上消息在不同東西之間傳遞便顯得複雜起來。但開發過程不一定會用到這麼複雜的操作,今天先把demo寫一下。
這是本節的demo代碼地址:https://gitee.com/mjTree/javaDevelop/tree/master/testDemo
首先打開rabbitmq的服務,打開網頁(不清楚可以查看教程一),之後用IDEA創建一個maven項目。
在pom.xml加入所需的相應依賴
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.7.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.26</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
在在mian/java下面新建com.myTest.RabbitMQ.First的包下面建立Send.java和Recv.java完成今天的demo。
如果代碼報錯Lambda expressions are not supported at language level '5',是IDEA的配置問題,修改一下即可解決。
File > Project Structure > Project > Project SDK: Change it to Java 1.8.XX
File > Project Structure > Project > Language Level: SDK 8 (in my case SDK default was already 8)
File > Project Structure > Modules > Sources > SDK 8 (in my case SDK default was already 8)
File > Settings > Build, Execution, Deployment > Compiler > Java Compiler > Project bytecode version > 1.8
File > Settings > Build, Execution, Deployment > Compiler > Java Compiler > Per-module bytecode version > Target bytecode version > 1.8
來源:https://stackoverflow.com/questions/29921498/lambda-expressions-are-not-supported-at-this-language-level-idea/30102858
package com.mytest.rabbitMQ.First;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
public class Recv {
private final static String QUEUE_NAME = "hello";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received '" + message + "'");
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
}
}
之後兩個代碼運行沒有先後順尋,先運行Send之後可以在Rabbitmq網頁上查看到相應隊列和消息數;
之後再運行Recv消費掉消息之後網頁也會發生變化。