RocketMQ整合springboot应用

如果要在spring boot中使用RocketMq,需要导入响应的依赖

        <dependency>
            <groupId>org.apache.rocketmq</groupId>
            <artifactId>rocketmq-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

在需要使用到mq的地方通过注解注入进来即可

@Autowired
private RocketMQTemplate  rocketMQTemplate;

 常用的发送消息的几个方法

    同步发送,等待发送结果返回,
    destination:消息的目标地址,一般以topic:tag赋值
    payload:消息体,可以是任何对象
    
    public SendResult syncSend(String destination, Object payload)

 异步发送,通过回调函数处理发送结果
public void asyncSend(String destination, Object payload, SendCallback sendCallback)
例如:        
rocketMQTemplate.asyncSend(MqConstant.TOPIC_TAG,"发送一个异步消息",new SendCallback() {
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功");
            }
            public void onException(Throwable e) {
                System.out.println("发送失败");
            }
        });

单向发送,不等待发送结果返回
public void sendOneWay(String destination, Object payload)

同步顺序发送,确保消息按顺序发送到同一队列
hashKEY:用于消息分区的哈希键,相同的哈希键保证消息进入同一队列
public SendResult syncSendOrderly(String destination, Object payload, String hashKey)

异步顺序发送,通过回调函数处理发送结果
public void asyncSendOrderly(String destination, Object payload, String hashKey, SendCallback sendCallback)

单向排序发送,不等待发送结果返回
public void sendOneWayOrderly(String destination, Object payload, String hashKey)

请求响应模式,发送请求并等待响应消息,这种方式适用于需要请求-响应模式的场景
request:请求消息体,可以是任何对象
responseType:响应消息的类型
public <T> T sendAndReceive(String destination, Object request, Class<T> responseType)
例如:
public void sendRequestAndReceiveResponse() {
        String destination = "RequestTopic";
        String requestMessage = "Hello RocketMQ, can you respond?";
 
        // 发送请求并接收响应
        String response = rocketMQTemplate.sendAndReceive(destination, requestMessage, String.class);
 
        // 打印响应消息
        System.out.println("Received response: " + response);
    }

1、带超时时间的sendAndReceive,
timeout:等待响应的超时时间,单位为毫秒
public <T> T sendAndReceive(String destination, Object request, Class<T> responseType, long timeout)

2、带回调函数的sendAndReceive,
listener:本地事务监听器,用于处理事务消息的回调
responseType:响应消息的类型
public <T> T sendAndReceive(String destination, Object request, Class<T> responseType, RocketMQLocalTransactionListener listener)
例如:
// 自定义事务监听器
RocketMQLocalTransactionListener listener = new RocketMQLocalTransactionListener() {
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object arg) {
        // 执行本地事务逻辑
        return RocketMQLocalTransactionState.COMMIT;
    }
 
    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
        // 检查本地事务状态
        return RocketMQLocalTransactionState.COMMIT;
    }
};
 
String response = rocketMQTemplate.sendAndReceive("RequestTopic", "Hello RocketMQ", String.class, listener);
System.out.println("Received response: " + response);

创建消费者

@Component
@RocketMQMessageListener(topic = "springboot-mq",consumerGroup = "consumer-group1")
public class RocketMQConsumer implements RocketMQListener<String> {
    @Override
    public void onMessage(String s) {
        System.out.println("消费者收到消息:" + s);
    }
}

引入RocketMQMessageListener注解,设置topic和consumerGroup的值即可,消费者会自动监听有关消息,处理消费

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值