springboot整合rocketMq,简洁易用

文章目录

  • 前言
  • 一、rocketMq基本概念
  • 二、使用步骤
    • 1.引入库
    • 2.生产者整合
    • 消费者整合
  • 总结


前言

springboot整合rocket,简洁易用

一、rocketMq基本概念

参考我另一篇博客
rocketMq基本概念

二、使用步骤

1.引入库


        <dependency>
            <groupId>org.apache.rocketmqgroupId>
            <artifactId>rocketmq-clientartifactId>
            <version>4.8.0version>
        dependency>

2.生产者整合

创建类交由spring管理
先配置

rocketMq:
  nameserver: 192.168.3.72:9876

交由spring管理

/**
 * @Auther: jiliugang
 * @Date: 2021/2/24 14:56
 * @Description:
 */
@Configuration
public class RocketMqProducer Config {
    @Value("${rocketMq.nameserver}")
    private String nameserver;
    @Bean(name = "msgProducer")
    public DefaultMQProducer msgProducer() throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer("msgProducer");
        producer.setNamesrvAddr(nameserver);
        producer.setRetryTimesWhenSendFailed(5);
        producer.start();
        System.out.println("msgProducer已启动");
        return producer;
    }
}

在类中使用

/**
 * 设备信息Service业务层处理
 *
 * @author jiliugang
 * @date 2021-01-29
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class EquipmentServiceImpl implements IEquipmentService
{
	//注入msgProducer对象
    @Resource(name = "msgProducer")
    private DefaultMQProducer msgProducer;
    @Override
    public int getPic(String eqNo) {
    	//创建消息并发送
        Map data = new HashMap();
        data.put("eqNo","test");
        data.put("cid","test");
        data.put("command","Read");
        Message message = new Message("msgServerTopic","command", JSON.toJSONString(data).getBytes());
        SendResult send = null;
        try {
            send = msgProducer.send(message);
        } catch (MQClientException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (MQBrokerException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (send.getSendStatus() == SendStatus.SEND_OK){
            return 1;
        }
        return -1;
    }
}

消费者整合

创建类交由spring管理
先配置

rocketMq:
  nameserver: 192.168.3.72:9876

实例化消费者,交由spring管理

/**
 * @Auther: jiliugang
 * @Date: 2021/3/3 10:37
 * @Description:
 */
@Configuration
public class MsgConsumerConfig {
    @Value("${rocketMq.nameserver}")
    private String nameserver;
    @Autowired
    private MsgListerner msgListerner;
    private final static String msgServerTopicName = "msgServerTopic";
    @Bean(name = "msgConsumer")
    public DefaultMQPushConsumer msgConsumer() throws MQClientException {
        // 实例化消费者
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("msgConsumer");
        // 设置NameServer的地址
        consumer.setNamesrvAddr(nameserver);
        // 订阅一个或者多个Topic,以及Tag来过滤需要消费的消息
        consumer.subscribe(msgServerTopicName, "*");
        // 注册回调实现类来处理从broker拉取回来的消息
        consumer.registerMessageListener(msgListerner);
        // 启动消费者实例
        consumer.start();
        System.out.println("msgToS30BConsumer已启动");
        return consumer;
    }
}

实例化监听器处理具体业务逻辑

/**
 * @Auther: jiliugang
 * @Date: 2021/3/3 10:40
 * @Description:
 */
@Component
public class MsgListerner implements MessageListenerConcurrently {
    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        for (MessageExt messageExt : list) {
        	//具体业务逻辑
         	System.out.println(new String(messageExt.getBody()));
        }
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
}

总结

以上就是今天要讲的内容,本文简单介绍了springboot整合rocketMq的使用

你可能感兴趣的:(java,springboot,消息队列,springboot,rocketMq,消息队列,spring,java)