1、导入依赖
<dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>spring-boot-starter-rocketmq</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <!-- rocketmq dependencies --> <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-client</artifactId> <version>4.4.0</version> </dependency>
2、配置生产者
package com.example.demo.mq; import org.apache.rocketmq.client.exception.MQClientException; import org.apache.rocketmq.client.producer.DefaultMQProducer; import org.springframework.stereotype.Component; @Component public class MyProducer { /** * 生产组,生产者必须在生产组内 */ private String producerGroup = "newTest_group"; /** * nameSrv IP及端口 */ private String nameSrv = "****:9876"; /** * 生产者 */ private DefaultMQProducer producer; /** * 设置producer对应的组及namesrv信息,并启动producer */ public MyProducer(){ producer = new DefaultMQProducer(producerGroup); producer.setNamesrvAddr(nameSrv); start(); } /** * 获取producer * @return */ public DefaultMQProducer getProducer(){ return producer; } /** * 对象在使用之前必须调用一次,并且只能初始化一次 */ public void start(){ try{ this.producer.start(); }catch (MQClientException e){ e.printStackTrace(); } } /** * 一般在应用上下文,使用上下文监听器,进行关闭 */ public void shutdown(){ producer.shutdown();; } }
3、发送消息Controller
package com.example.demo.controller; import com.example.demo.mq.MyProducer; import org.apache.rocketmq.client.exception.MQBrokerException; import org.apache.rocketmq.client.exception.MQClientException; import org.apache.rocketmq.client.producer.SendResult; import org.apache.rocketmq.common.message.Message; import org.apache.rocketmq.remoting.exception.RemotingException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; @RestController public class MQController { @Autowired private MyProducer producer; /** * topic */ private static final String topic = "my_topic_test01"; @GetMapping(value = "/mq/test1") public Object test1(String text) throws InterruptedException, RemotingException, MQClientException, MQBrokerException { /** * message构造函数,三个参数分别为 主题、二级分类、消息内容(需要转换为字节数组) */ Message message = new Message(topic,"lcl",("hello RocketMQ"+text).getBytes()); SendResult result = producer.getProducer().send(message); System.out.println(result); return new HashMap<>(); } }
4、客户端
package com.example2.demo2.mq; import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; import org.apache.rocketmq.client.exception.MQClientException; import org.apache.rocketmq.common.consumer.ConsumeFromWhere; import org.apache.rocketmq.common.message.Message; import org.springframework.stereotype.Component; @Component public class MyConsumer { /** * 生产组,生产者必须在生产组内 */ private String producerGroup = "newTest_group"; /** * nameSrv IP及端口 */ private String nameSrv = "118.190.215.76:9876"; /** * topic */ private static final String topic = "my_topic_test01"; private DefaultMQPushConsumer consumer; public MyConsumer() throws MQClientException { consumer = new DefaultMQPushConsumer(producerGroup); consumer.setNamesrvAddr(nameSrv); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET); consumer.subscribe(topic,"*"); consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context)->{ try{ Message message = msgs.get(0); System.out.println("%s Receive New Messages: %s %n" + Thread.currentThread().getName() + new String(msgs.get(0).getBody())); String topic = message.getTopic(); String body = new String(message.getBody(),"utf-8"); String tags = message.getTags(); String keys = message.getKeys(); System.out.println("topic="+ topic +",tags="+tags+",keys="+keys+",body="+body); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; }catch(Exception e){ e.printStackTrace(); return ConsumeConcurrentlyStatus.RECONSUME_LATER; } }); consumer.start(); } }
5、测试
注意:一定要创建topic,创建topic可以通过控制台创建、可以通过linux命令进行创建,也可以通过程序自动创建,无论以哪种方式创建,都需要创建topic,否则会报错。