使用 rocketmq5 是比較簡單的事情。也有些同學對 sdk 原始介面會陌生,會希望有個整合的示例。
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client-java</artifactId>
<version>${rocketmq5.version}</version>
</dependency>
完整的整合程式碼參考:
https://gitee.com/opensolon/solon-examples/tree/main/b.Integration/demoB002-rocketmq5
希望更加簡化使用的同學,可以使用:
rocketmq5-solon-cloud-plugin (但定製性會變差)
1、看看配置怎麼搞?
使用 Solon 初始器 生成一個 Solon Web 模板專案,然後新增上面的 rocketmq5 依賴。
- 新增 yml 配置(具體的配置屬性,參考:ClientConfigurationBuilder,ProducerBuilder, PushConsumerBuilder)
solon.app:
name: "demo-app"
group: "demo"
solon.logging:
logger:
root:
level: INFO
# 配置可以自由定義,與 @Bean 程式碼對應起來即可(以下為參考)
solon.rocketmq:
properties: #公共配置(配置項,參考:ClientConfigurationBuilder)
endpoints: "127.0.0.1:8081"
sessionCredentialsProvider:
"@type": "demoB002.SessionCredentialsProviderImpl" # solon 支援 "@type" 型別申明當前配置節的例項型別
accessKey: "xxx"
accessSecret: "xxx"
securityToken: "xxx"
requestTimeout: "10s"
producer: #生產者專屬配置(配置項,參考:ProducerBuilder)
maxAttempts: 3
consumer: #消費者專屬配置(配置項,參考:PushConsumerBuilder)
consumerGroup: "${solon.app.group}_${solon.app.name}"
consumptionThreadCount: 2
maxCacheMessageCount: 1
maxCacheMessageSizeInBytes: 1
- 新增 java 配置器
@Configuration
public class RocketmqConfig {
private ClientServiceProvider clientProvider = ClientServiceProvider.loadService();
@Bean
public ClientConfiguration client(@Inject("${solon.rocketmq.properties}") Properties common){
ClientConfigurationBuilder builder = ClientConfiguration.newBuilder();
//注入屬性
Utils.injectProperties(builder, common);
return builder.build();
}
@Bean
public Producer producer(@Inject("${solon.rocketmq.producer}") Properties producer,
ClientConfiguration clientConfiguration) throws ClientException {
ProducerBuilder producerBuilder = clientProvider.newProducerBuilder();
//注入屬性
if (producer.size() > 0) {
Utils.injectProperties(producerBuilder, producer);
}
producerBuilder.setClientConfiguration(clientConfiguration);
return producerBuilder.build();
}
@Bean
public PushConsumer consumer(@Inject("${solon.rocketmq.consumer}") Properties consumer,
ClientConfiguration clientConfiguration,
MessageListener messageListener) throws ClientException{
//按需選擇 PushConsumerBuilder 或 SimpleConsumerBuilder
PushConsumerBuilder consumerBuilder = clientProvider.newPushConsumerBuilder();
//注入屬性
Utils.injectProperties(consumerBuilder, consumer);
Map<String, FilterExpression> subscriptionExpressions = new HashMap<>();
subscriptionExpressions.put("topic.test", new FilterExpression("*"));
consumerBuilder.setSubscriptionExpressions(subscriptionExpressions);
consumerBuilder.setClientConfiguration(clientConfiguration);
consumerBuilder.setMessageListener(messageListener);
return consumerBuilder.build();
}
}
//這個實現類,(相對於 StaticSessionCredentialsProvider)方便配置自動注入
public class SessionCredentialsProviderImpl implements SessionCredentialsProvider {
private String accessKey;
private String accessSecret;
private String securityToken;
private SessionCredentials sessionCredentials;
@Override
public SessionCredentials getSessionCredentials() {
if (sessionCredentials == null) {
if (securityToken == null) {
sessionCredentials = new SessionCredentials(accessKey, accessSecret);
} else {
sessionCredentials = new SessionCredentials(accessKey, accessSecret, securityToken);
}
}
return sessionCredentials;
}
}
2、程式碼應用
- 傳送(或生產),這裡代控制器由使用者請求再傳送訊息(僅供參考):
@Controller
public class DemoController {
@Inject
private Producer producer;
@Mapping("/send")
public void send(String msg) throws ClientException {
//傳送
producer.send(new MessageBuilderImpl()
.setTopic("topic.test")
.setBody(msg.getBytes())
.build());
}
}
- 監聽(或消費),這裡採用訂閱回撥的方式:(僅供參考)
@Component
public class DemoMessageListener implements MessageListener {
@Override
public ConsumeResult consume(MessageView messageView) {
System.out.println(messageView);
return ConsumeResult.SUCCESS;
}
}