栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot项目整合Ribbitmq详细教程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot项目整合Ribbitmq详细教程

首先在springboot的pom文件里引入依赖


	org.springframework.boot
	spring-boot-starter-amqp

然后application.yml:

ps:里面的虚拟host配置项不是必须的,我自己在rabbitmq服务上创建了自己的虚拟host,所以我配置了;你们不创建,就不用加这个配置项。

spring:
  application:
    name: 3d-gis
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://172.16.10.201:5432/3d_gis
    username: postgres
    password: postgres
  rabbitmq:
    host: 172.16.10.201
    port: 5672
    username: leaniot
    password: leaniot
    virtual-host: /3d_gis
    listener:
      simple:
        acknowledge-mode: manual
      direct:
        acknowledge-mode: manual

RabbitMQ配置类

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;




@Configuration
public class RabbitMQConfig implements InitializingBean {

    
    public static final String  GIS_GRAPHICS_EXCHANGE = "3d_gis_exchange";
    
    public static final String GIS_DATA_RECEIVE_REQUEUE = "gis_data_receive_queue";
    
    public static final String GIS_DATA_SEND_QUEUE = "gis_data_send_queue";

    @Bean
    public Queue gisDataReceiveQueue () {
        return new Queue(GIS_DATA_RECEIVE_REQUEUE);
    }

    @Bean
    public Queue gisDataSendQueue () {
        return new Queue(GIS_DATA_SEND_QUEUE);
    }


    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(GIS_GRAPHICS_EXCHANGE);
    }

    @Bean
    public Binding receiveBinding () {
        return BindingBuilder.bind(gisDataReceiveQueue()).to(directExchange()).withQueueName();
    }

    @Bean
    public Binding sendBinding () {
        return BindingBuilder.bind(gisDataSendQueue()).to(directExchange()).withQueueName();
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}
RabbitSender类
import java.util.UUID;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate./confirm/iCallback;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class RabbitSender {

    @Resource
    private RabbitTemplate rabbitTemplate;

    
    final ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
        
        @Override
        public void confirm(CorrelationData correlationData, boolean ack, String cause) {
            System.err.println("消息ACK结果:" + ack + ", correlationdata: " + correlationData.getId());
        }
    };

    
    public void send(Object message)  {
        CorrelationData data = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(RabbitMQConfig.GIS_GRAPHICS_EXCHANGE, RabbitMQConfig.GIS_DATA_RECEIVE_REQUEUE, message, data);
    }
}
RabbitReceiver类
import org.springblade.core.tool.utils.Charsets;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;
import java.io.IOException;

@Component
public class RabbitReceiver {

    
    @RabbitListener(queues =RabbitMQConfig.GIS_DATA_RECEIVE_REQUEUE)
    public void onMessage(Message message, Channel channel) {
        //	1. 收到消息以后进行业务端消费处理
        System.err.println("-----------------------");
        System.err.println("消费消息:" +  new String(message.getBody(), Charsets.UTF_8));

        //  2. 处理成功之后 获取deliveryTag 并进行手工的ACK操作, 因为我们配置文件里配置的是 手工签收
        Long deliveryTag = message.getMessageProperties().getDeliveryTag();
       try {
           channel.basicAck(deliveryTag, false);
       } catch (IOException e) {
           new IOException();
       }
   }

}

测试类


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.annotation.NoToken;
import org.springblade.core.tool.api.R;
import org.springblade.three.dimension.gis.rabbitmq.RabbitSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@NoToken
@RestController
@AllArgsConstructor
@RequestMapping("rabbit")
@Api(value = "消息队列", tags = "消息队列")
public class RabbitController {
    @Resource
    private RabbitSender sender;


    @ApiOperation(value = "添加 @author Tarzan Liu")
    @GetMapping("test")
    public R test(){
        sender.send("测试666");
        return R.status(true);
    }
}

测试结果

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322132.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号