pom.xml
文件中添加以下依赖: <properties>
<rocketmq.version>2.1.0rocketmq.version>
properties>
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-spring-boot-starterartifactId>
<version>${rocketmq.version}version>
dependency>
application.properties
或 application.yml
文件中添加以下配置:rocketmq:
name-server: 127.0.0.1:9876
producer:
group: variable-monitor-producer-group
package com.kang.grafana.rocketmq.producer;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 编写发送者 创建一个发送者,用于向指定的主题发送消息
*/
@Service
public class Sender {
@Autowired
private RocketMQTemplate rocketMQTemplate;
public void send(String topic, String message) {
rocketMQTemplate.convertAndSend(topic, message);
}
}
package com.kang.grafana.rocketmq.consume;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;
@Service
@RocketMQMessageListener(topic = "variable-monitor-topic", consumerGroup = "variable-monitor-consumer-group")
public class Consumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
System.out.println("Received message: " + message);
}
}
package com.kang.grafana.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kang.grafana.rocketmq.producer.Sender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @Author Emperor Kang
* @ClassName RocketMqController
* @Description TODO
* @Date 2023/9/12 14:17
* @Version 1.0
* @Motto 让营地比你来时更干净
*/
@RestController
@RequestMapping("/rocket")
public class RocketMqController {
@Autowired
private ThreadPoolExecutor bigdataThreadPoolExecutor;
@Autowired
private Sender sender;
@RequestMapping("/send/message")
public Object send(){
String topic = "variable-monitor-topic";
String message = JSON.toJSONString(bigdataThreadPoolExecutor, SerializerFeature.WriteMapNullValue);
sender.send(topic,message);
return Boolean.TRUE;
}
}