0、启动rocketmq相关组件
start mqnamesrv.cmd
start mqbroker.cmd -n 127.0.0.1:9876 autoCreateTopicEnable=true
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.10</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>rocketmqpro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rocketmqpro</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- RocketMQ -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、application.yml
server:
port: 8080
rocketmq:
name-server: 127.0.0.1:9876 #私有云消费mq地址
3、生产者
package com.rocketmqpro.producer;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import java.util.List;
public class RMQPro {
public static void main(String[] args) {
final boolean[] close = {false};
DefaultMQProducer producer = new DefaultMQProducer("Producer");
// 必须要设置nameserver地址
producer.setNamesrvAddr("127.0.0.1:9876");
try {
//producer.setClientIP("**");
//设置实例名称
producer.setInstanceName("rocketMQ");
//设置重试的次数
producer.setRetryTimesWhenSendFailed(3);
//开启生产者
producer.start();
//创建一条消息
Message msg = new Message("PushTopic", "push", "1", "生产消息".getBytes());
//发送,并触发回调函数
producer.send(msg, new SendCallback() {
@Override
//成功的回调函数
public void onSuccess(SendResult sendResult) {
System.out.println(sendResult.getSendStatus() + "----" + "成功了");
}
@Override
//出现异常的回调函数
public void onException(Throwable e) {
System.out.println("失败了----" + e.getMessage());
}
});
//获取某个主题的消息队列
List<MessageQueue> messageQueues = producer.fetchPublishMessageQueues("PushTopic");
System.out.println("消息队列数量:" + messageQueues.size());
for(int i=0;i<messageQueues.size();i++){
System.out.println(messageQueues.get(i).toString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// while (!close[0]) {
//
// }
// producer.shutdown();
}
}
}
4、消费者
package com.rocketmqpro.consumer;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.stereotype.Component;
@Component
@RocketMQMessageListener(topic = "PushTopic", consumerGroup = "dwh-datatocloud")
public class RMQCon implements RocketMQListener<MessageExt>, RocketMQPushConsumerLifecycleListener {
@Override
public void onMessage(MessageExt message) {
System.out.println(new String(message.getBody()));
}
@Override
public void prepareStart(DefaultMQPushConsumer consumer) {
//从消息队列头部开始消费
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
//设置广播消费模式
consumer.setMessageModel(MessageModel.BROADCASTING);
//订阅主题
//consumer.subscribe("PushTopic", "push");
//注册消息监听器
//consumer.registerMessageListener(this);
//启动消费端
//consumer.start();
}
}