本文主要是介绍链接阿里云的RocketMQ,发送信息,写一个公用的service类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
RocketMQMessage模型类
这个类封装了消息发送所需的所有参数。
package com.aliyun.openservices;public class RocketMQMessage {private String topicName;private String tag;private String key;private String body;private String messageGroup;public RocketMQMessage() {}public RocketMQMessage(String topicName, String tag, String key, String body, String messageGroup) {this.topicName = topicName;this.tag = tag;this.key = key;this.body = body;this.messageGroup = messageGroup;}public String getTopicName() {return topicName;}public void setTopicName(String topicName) {this.topicName = topicName;}public String getTag() {return tag;}public void setTag(String tag) {this.tag = tag;}public String getKey() {return key;}public void setKey(String key) {this.key = key;}public String getBody() {return body;}public void setBody(String body) {this.body = body;}public String getMessageGroup() {return messageGroup;}public void setMessageGroup(String messageGroup) {this.messageGroup = messageGroup;}
}
RocketMQService服务类
这个服务类使用RocketMQMessage
类实例来发送消息。
package com.aliyun.openservices;import org.apache.rocketmq.client.apis.ClientConfiguration;
import org.apache.rocketmq.client.apis.ClientConfigurationBuilder;
import org.apache.rocketmq.client.apis.ClientException;
import org.apache.rocketmq.client.apis.ClientServiceProvider;
import org.apache.rocketmq.client.apis.producer.Producer;
import org.apache.rocketmq.client.apis.producer.SendReceipt;
import org.apache.rocketmq.client.apis.message.MessageBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets;@Service
public class RocketMQService {@Value("${rocketmq.endpoint}")private String endpoint;@Value("${rocketmq.username}")private String username;@Value("${rocketmq.password}")private String password;public SendReceipt sendMessage(RocketMQMessage message) throws ClientException {ClientServiceProvider provider = ClientServiceProvider.loadService();ClientConfigurationBuilder configBuilder = ClientConfiguration.newBuilder().setEndpoints(endpoint);if (username != null && password != null) {configBuilder.setCredentialProvider(new StaticSessionCredentialsProvider(username, password));}ClientConfiguration configuration = configBuilder.build();Producer producer = provider.newProducerBuilder().setClientConfiguration(configuration).setTopics(message.getTopicName()).build();MessageBuilder builder = provider.newMessageBuilder().setTopic(message.getTopicName()).setBody(message.getBody().getBytes(StandardCharsets.UTF_8));if (message.getKey() != null && !message.getKey().isEmpty()) {builder.setKeys(message.getKey());}if (message.getTag() != null && !message.getTag().isEmpty()) {builder.setTag(message.getTag());}if (message.getMessageGroup() != null && !message.getMessageGroup().isEmpty()) {builder.setMessageGroup(message.getMessageGroup());}SendReceipt sendReceipt = producer.send(builder.build());System.out.println("Send mq message success! Topic is:" + message.getTopicName() + " msgId is: " + sendReceipt.getMessageId().toString());producer.close();return sendReceipt;}
}
application.properties配置文件
# RocketMQ配置
rocketmq.endpoint=你的RocketMQ接入点
rocketmq.username=你的用户名
rocketmq.password=你的密码
请确保将你的RocketMQ接入点
、你的用户名
和你的密码
替换成你自己的RocketMQ服务的实际接入点、用户名和密码。
通过这种方式,我们创建了一个灵活的服务类RocketMQService
,它可以发送包含各种参数的消息到RocketMQ。通过使用RocketMQMessage
类来封装消息参数,我们简化了方法的调用。
这篇关于链接阿里云的RocketMQ,发送信息,写一个公用的service类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!