springboot集成rabbitmq

导入pom

	<!--rabbitMQ-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>

rabbitmq配置

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#是否确认发送的消息已经被消费
spring.rabbitmq.publisher-confirms=true
#RabbitMQ 的消 队列名称 由它发送字符串
rabbitmq.queue.msg=spring-boot-queue-msg
#Rabbi 的消息队列名称,由它发送用户对象
rabbitmq.queue.user=spring-boot-queue-user

rabbitmqService接口

package com.test.house.service;

import com.test.house.entity.User;

public interface RabbitMqService {
    //发送字符串消息
    public void sendMsg(String msg);
    //发送用户消息
    public void sendUser(User user);
}

rabbitmqService接口实现

package com.test.house.service;

import com.test.house.entity.User;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

//实现ConfirmCallback接口,这样可以实现回调
@Service
public class RabbitMqServiceImpl implements RabbitTemplate.ConfirmCallback,RabbitMqService {

    @Value("${rabbitmq.queue.msg}")
    private String msgRouting;

    @Value("${rabbitmq.queue.user}")
    private String userRouting;

    //注入spring boot自动配置的RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //发送消息
    @Override
    public void sendMsg(String msg) {
        System.out.println("发送消息:【"+msg+"】");
        //设置回调
        rabbitTemplate.setConfirmCallback(this);
        //发送消息,通过msgRouting确定队列
        rabbitTemplate.convertAndSend(msgRouting,msg);
    }

    //发送用户
    @Override
    public void sendUser(User user) {
        System.out.println("发送用户消息:【"+user+"】");
        //设置回调
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.convertAndSend(userRouting,user);
    }

    @Override
    public void confirm(@Nullable CorrelationData correlationData, boolean b, @Nullable String s) {
        if(b){
            System.out.println("消息成功消费");
        }else {
            System.out.println("消息消费失败:"+s);
        }
    }
}

消费者

package com.test.house.service;

import com.test.house.entity.User;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitMessageReceiver {
    //定义监听字符串队列名称
    //手动创建,需在RabbitMQ中手动创建myQueue1 队列,否则报错
    @RabbitListener(queues = {"${rabbitmq.queue.msg}"})
    public void receiveMsg(String msg){
        System.out.println("收到消息:【"+msg+"】");
    }

    //定义监听用户队列名称
    //自动创建队列
    @RabbitListener(queuesToDeclare = @Queue("${rabbitmq.queue.user}"))
    public void receiveUser(User user){
        System.out.println("收到用户信息:"+user+"】");
    }
}

user实体类

package com.test.house.entity;

import java.io.Serializable;

public class User implements Serializable{
    private static final long serialVersionUID = -5809782578272943999L;

    private String username;

    private String password;

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

注意点:

@RabbitListener(queues = {"${rabbitmq.queue.msg}"})该监听需要在 127.0.0.1:15672中手动创建队列,否则程序会报错:fail to declare queues
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值