rocketmq-spring-boot-starter使用指南

版权声明:欢迎分享技术,转载请声明。更多技术见 github.com/javahongxi https://blog.csdn.net/javahongxi/article/details/86160085

https://github.com/apache/rocketmq-spring.git

针对官方starter修改点 官方

  • 支持连接多个集群(订阅) (官方一个应用只能连接一个集群)
  • 顺序消息消费失败,可配重试次数 (非顺序消息默认重试16次,每次时间延后)
  • 发送延时消息方法参数优化(魔法参数改为枚举)
  • 优化getMessageType方法,支持 MyConsumer extends AbstractConsumer implements RocketMQListener
    (官方只支持MyConsumer implements RocketMQListener)
  • RocketMQTemplate方法重载(加入keys)
  • 暂未加入事务消息功能 (官方最新版支持)

dependencies

<dependency>
    <groupId>org.hongxi</groupId>
    <artifactId>rocketmq-spring-boot-starter</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

application.yml

spring:
  rocketmq:
    nameServer: 127.0.0.1:9876
    producer:
      group: boot_producer_group

Send Message

@SpringBootApplication
public class ProducerApplication implements CommandLineRunner {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    
    public static void main(String[] args){
        SpringApplication.run(ProducerApplication.class, args);
    }
    
    public void run(String... args) throws Exception {
		// 如下两种方式等价
        rocketMQTemplate.convertAndSend("test-topic-1", "Hello, World!");
        rocketMQTemplate.send("test-topic-1", MessageBuilder.withPayload("Hello, World! I'm from spring message").build());
        // 第三个参数为key
        rocketMQTemplate.syncSend("test-topic-1", "Hello, World! I'm from simple message", "18122811143034568830");
		
		// topic: ORDER,tag: paid, cacel
		rocketMQTemplate.convertAndSend("ORDER:paid", "Hello, World!");
        rocketMQTemplate.convertAndSend("ORDER:cancel", "Hello, World!");
		
	    // 消息体为自定义对象
        rocketMQTemplate.convertAndSend("test-topic-2", new OrderPaidEvent("T_001", new BigDecimal("88.00")));


		// 发送延迟消息
        rocketMQTemplate.sendDelayed("test-topic-1", "I'm delayed message", MessageDelayLevel.TIME_1M);
		
		// 发送即发即失消息(不关心发送结果)
		rocketMQTemplate.sendOneWay("test-topic-1", MessageBuilder.withPayload("I'm one way message").build());


		// 发送顺序消息
        rocketMQTemplate.syncSendOrderly("test-topic-4", "I'm order message", "1234");
		
		// 发送异步消息
		rocketMQTemplate.asyncSend("test-topic-1", MessageBuilder.withPayload("I'm one way message").build(), new SendCallback() {
    @Override
    public void onSuccess(SendResult sendResult) {
        
    }

    @Override
    public void onException(Throwable e) {
        
    }
});

        System.out.println("send finished!");
    }

}

Consume Message

@SpringBootApplication
public class ConsumerApplication{
    
    public static void main(String[] args){
        SpringApplication.run(ConsumerApplication.class, args);
    }

}
@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-1", consumerGroup = "my-consumer_test-topic-1")
public class MyConsumer implements RocketMQListener<String> {
    public void onMessage(String message) {
        log.info("received message: " + message);
    }
}
@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-2", consumerGroup = "my-consumer_test-topic-2")
public class MyConsumer2 implements RocketMQListener<OrderPaidEvent> {
    public void onMessage(OrderPaidEvent orderPaidEvent) {
        log.info("received orderPaidEvent: " + orderPaidEvent);
    }
}
/**
 * 指定连接某个MQ集群
 */

@Slf4j
@Service
@RocketMQMessageListener(nameServer = "127.0.0.1:9877", instanceName = "tradeCluster", topic = "test-topic-3", consumerGroup = "my-consumer_test-topic-3")
public class MyConsumer3 implements RocketMQListener<String> {
    public void onMessage(String message) {
        log.info("received message: " + message);
    }
}
/**
 * 顺序消息消费失败,默认不重试(官方是一直重试)
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-4", consumerGroup = "my-consumer_test-topic-4",
    consumeMode = ConsumeMode.ORDERLY)
public class MyConsumer4 implements RocketMQListener<String> {
    public void onMessage(String message) {
        log.info("received message: " + message);
        int a = 1 / 0;
    }
}
/**
 * 配置重试次数 reconsumeTimes = 3
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-4", consumerGroup = "my-consumer_test-topic-5",
    consumeMode = ConsumeMode.ORDERLY, reconsumeTimes = 3)
public class MyConsumer5 implements RocketMQListener<MessageExt> {
    public void onMessage(MessageExt messageExt) {
        log.info("received message: " + messageExt);
        int a = 1 / 0;
    }
}
/**
 * 配置重试次数 reconsumeTimes = -1 代表一直重试
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-4", consumerGroup = "my-consumer_test-topic-6",
    consumeMode = ConsumeMode.ORDERLY, reconsumeTimes = -1)
public class MyConsumer6 implements RocketMQListener<MessageExt> {
    public void onMessage(MessageExt messageExt) {
        log.info("received message: " + messageExt);
        int a = 1 / 0;
    }
}

注意:Consumer里抛出异常才会重试,所以使用者不要把Consumer里的整个代码try-catch

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试