spring整合rabbitmq

1.导入依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.9.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--AMQP依赖,包含RabbitMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <!--单元测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

2.编写消息生产者

package cn.com.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.concurrent.TimeoutException;



@SpringBootTest
public class RabbitPushTest {

    @Test
    public void pushmessage() throws IOException, TimeoutException {
        /*/
        - 建立连接
        - 创建Channel
        - 声明队列
        - 发送消息
        - 关闭连接和channel
         */
//        1.建立连接
        ConnectionFactory factory = new ConnectionFactory();
//        2.设置连接参数
        factory.setHost(ip);
        factory.setPort(5672);
        factory.setUsername("test");
        factory.setPassword("test");
        factory.setVirtualHost("test");
//        3.创建连接
        Connection connection = factory.newConnection();
//        4.创建信道
        Channel channel = connection.createChannel();
//        5.创建队列
        /*
             void queueDeclareNoWait(String queue, boolean durable, boolean exclusive, boolean autoDelete,
                            Map<String, Object> arguments) throws IOException;
                    参数说明
                           1.String queue : 声明一个队列名称
                           2.boolean durable : 是否持久化(是否持久化到硬盘中)
                           3.boolean exclusive : 声明独占队列
                           4.boolean autoDelete : 是否自动删除
                           5. Map<String, Object> arguments : 队列的其他属性
        */
        String queueName = "rabbitMQ";
        channel.queueDeclare(queueName,false,false,false,null);
//        6.发送消息
        String message = "hello, rabbitmq!";
        channel.basicPublish("",queueName,null,message.getBytes());
//        7.关闭连接
        channel.close();
        connection.close();

    }

}

3.编写消费者

package cn.com.rabbitmq;

import com.rabbitmq.client.*;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.concurrent.TimeoutException;


@SpringBootTest
public class consumerTest {

    @Test
    public void pullMessage() throws IOException, TimeoutException {
        /*
        - 建立连接
        - 创建Channel
        - 声明队列
        - 订阅消息
         */
//        1.建立连接
        ConnectionFactory factory = new ConnectionFactory();
//        2.设置连接参数
        factory.setHost(ip);
        factory.setPort(5672);
        factory.setUsername("test");
        factory.setPassword("test");
        factory.setVirtualHost("test");
        Connection connection = factory.newConnection();
//        3.创建channel
        Channel channel = connection.createChannel();
//        4.声明队列
        String queueName = "rabbitMQ";
        channel.queueDeclare(queueName,false,false,false,null);
//        5.持续订阅消息
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                // 5.处理消息
                String message = new String(body);
                System.out.println("接收到消息:【" + message + "】");
            }
        };
        channel.basicConsume(queueName,true,defaultConsumer);
       
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值