rocketMQ的简单调用

一.同步发送

@RequestMapping("/send")
    public void send() throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
        for (int i = 0; i < 100; i++) {
            final int index = i;
            String msg = "demo msg test";
            logger.info("开始发送消息:" + msg);
            Message sendMsg = new Message("DemoTopic", "DemoTag", msg.getBytes());
            //默认3秒超时
            SendResult sendResult = defaultMQProducer.send(sendMsg);
            logger.info("消息发送响应信息:" + sendResult.toString() + "当前为第" + i + "次");
        }
    }

二.异步发送

如果对响应时间有要求(要求程序不阻塞),可以异步调用

配置类代码不变 在调用时改变方法即可

package cn.baocl.rocketmq.controllor;

import cn.baocl.rocketmq.entity.TestVo;
import com.alibaba.rocketmq.client.exception.MQBrokerException;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendCallback;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.remoting.exception.RemotingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/test")
public class TestControllor {
    private static final Logger logger = LoggerFactory.getLogger(TestControllor.class);

    /**
     * 使用RocketMq的生产者
     */
    @Autowired
    private DefaultMQProducer defaultMQProducer;

    @RequestMapping("/send")
    public void send() throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
        for (int i = 0; i < 100; i++) {
            final int index = i;
            String msg = "demo msg test";
            logger.info("开始发送消息:" + msg);
            Message sendMsg = new Message("DemoTopic", "DemoTag", msg.getBytes());
            //异步传输方法
            defaultMQProducer.send(sendMsg, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.printf("%-10d OK %s %n", index,
                            sendResult.getMsgId());
                }
                @Override
                public void onException(Throwable e) {
                    System.out.printf("%-10d Exception %s %n", index, e);
                    e.printStackTrace();
                }
            });
        }
    }
}

三.单向传输

单向传输用于需要中等可靠性的情况(只发送,不调用回调 直接返回),例如日志收集。

 public void send() throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
        for (int i = 0; i < 100; i++) {
            final int index = i;
            String msg = "demo msg test";
            logger.info("开始发送消息:" + msg);
            //单项调用
            Message sendMsg = new Message("DemoTopic", "DemoTag", msg.getBytes());
            //默认3秒超时
            defaultMQProducer.sendOneway(sendMsg);
        }
    }

 

你可能感兴趣的:(rocketMQ)