RabbitMQ非官方教程(二)HelloWorld的Demo

这是官网教程,https://www.rabbitmq.com/tutorials/tutorial-one-java.html

RabbitMQ非官方教程(二)HelloWorld的Demo_第1张图片

用谷歌浏览器将网页翻译成中文看即可,这里就简单指一下消息传递过程

生产者 --> 交换器 --> 队列 --> 消费者

生产者生成消息,然后将消息转发到相应交换器中,通过一定规则匹配或绑定,交换器把消息转发到对应的队列中,队列也会通过部分规则将消息发送个消费者。由于生产者、队列、消费者都可以是多个存在的,加上消息在不同东西之间传递便显得复杂起来。但开发过程不一定会用到这么复杂的操作,今天先把demo写一下。

 

这是本节的demo代码地址:https://gitee.com/mjTree/javaDevelop/tree/master/testDemo

首先打开rabbitmq的服务,打开网页(不清楚可以查看教程一),之后用IDEA创建一个maven项目。

RabbitMQ非官方教程(二)HelloWorld的Demo_第2张图片

在pom.xml加入所需的相应依赖


        
            com.rabbitmq
            amqp-client
            5.7.1
        
        
            org.slf4j
            slf4j-simple
            1.7.26
        

        
            org.junit.jupiter
            junit-jupiter
            5.5.1
            test
        

        
            io.dropwizard.metrics
            metrics-core
            4.1.0
            test
        

        
            org.assertj
            assertj-core
            3.13.2
            test
        

        
            org.mockito
            mockito-core
            3.0.0
            test
        
    

    
        
            
                maven-compiler-plugin
                3.8.1
            
            
                maven-surefire-plugin
                2.22.2
            
        
    

在在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

RabbitMQ非官方教程(二)HelloWorld的Demo_第3张图片

RabbitMQ非官方教程(二)HelloWorld的Demo_第4张图片

RabbitMQ非官方教程(二)HelloWorld的Demo_第5张图片

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消费掉消息之后网页也会发生变化。

你可能感兴趣的:(rabbitmq)