【springboot 集成rabbitMQ 使用示例】

1、配置pom文件,主要是添加spring-boot-starter-amqp的支持

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

2、配置application.properties文件

spring:
 rabbitmq:
    username: admin
    password: admin
    host: ip/域名
    port: 5672

3.配置队列

@Configuration
public class DelayedQueueConfig {
    //交换机
    public static final String DELAYED_EXCHANGE ="amp.hello";
    //队列
    public static final String DELAYED_QUEUE ="Expire.Queue";
    //routeingKey
    public static final String DELAYED_ROUTINGKEY ="delayed_routingKey";

    //声明延迟交换机
    @Bean
    public CustomExchange delayedExchange(){
        HashMap<String, Object> arguments = new HashMap<>();
        //自定义交换机的类型
        arguments.put("x-delayed-type", "direct");
        /**
         * 交换机名
         * 交换机类型
         * 持久化
         * 自动删除
         */
        return new CustomExchange(DELAYED_EXCHANGE,"x-delayed-message",true,false,arguments);
    }

    /**
     * 声明队列
     * @return
     */
    @Bean
    public Queue delayedQueue(){
        return new Queue(DELAYED_QUEUE);
    }

    //延迟交换机和队列绑定
    @Bean
    public Binding delayedQueueBindingDelayedExchange(Queue delayedQueue, CustomExchange delayedExchange){
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYED_ROUTINGKEY).noargs();
    }

}

4、发送者

@Component
public class HelloSender {
@Resource
private RabbitTemplate rabbitTemplate;
    
private void send() throws ParseException {
        try {
            Demo demo = new Demon();
            demo.setCourseId(CourseIdInfo.getCourseId());
            demo.setClassId(CourseIdInfo.getClassId());
            String content = JSONObject.toJSONString(demo);
            log.info("准备进入队列");
            rabbitTemplate.convertAndSend("amp.hello", "delayed_routingKey", content, (msg -> {
                //发送消息 并设置delayedTime
            msg.getMessageProperties().setDelay(Math.toIntExact(finalEffectiveTime));
                return msg;
            }));
        } catch (Exception e) {
            log.error("putMessageToQueue is error",e);
        }

    }
  }

5、接收者


@Service
@Slf4j
public class ExpireQueue {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = {"Expire.Queue"})
    public void demo(Message message, Channel channel) throws ParseException {
        String userCourseInfo = new String(message.getBody());
        // 接收要发送的json串转换成对象
        log.error("进入队列,延迟时间{}",effectiveTime);
       
        // 控制消费频率 1s 十条
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            log.error("消费失败", e);
        }
    }

}

6.客户端测试


@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMqHelloTest {

    @Autowired
    private HelloSender helloSender;

    @Test
    public void hello() throws Exception {
        helloSender.send();
    }
}

7.完毕

你可能感兴趣的:(●【RabbitMQ】,java-rabbitmq,rabbitmq,spring,boot)