Using rocketmq5 is relatively simple. Some of you will be unfamiliar with the sdk raw interface and will want to have an example of integration.
<dependency>
<groupId></groupId>
<artifactId>rocketmq-client-java</artifactId>
<version>${}</version>
</dependency>
Full integration code reference:
/opensolon/solon-examples/tree/main//demoB002-rocketmq5
Students who wish to simplify their use even more:
rocketmq5-solon-cloud-plugin (but less customizable)
1, see how the configuration works?
utilizationSolon Initializer Generate a Solon Web template project and add the rocketmq5 dependency above.
- Add yml configuration (for specific configuration properties, refer to: ClientConfigurationBuilder, ProducerBuilder, PushConsumerBuilder)
:
name: "demo-app"
group: "demo"
:
name: "demo-app" group: "demo" : logger.
root: "demo-app" group: "demo" : logger: root: "demo
level: INFO
# Configuration can be freely defined to correspond to the @Bean code (here's the reference)
:
properties: # public configuration (configuration items, see: ClientConfigurationBuilder)
endpoints: "127.0.0.1:8081"
sessionCredentialsProvider.
"@type": "" # solon supports the "@type" type to assert the type of the instance of the current configuration section.
accessKey: "xxx"
accessSecret: "xxx"
securityToken: "xxx"
requestTimeout: "10s"
producer: # producer-specific configuration (configuration item, see: ProducerBuilder)
maxAttempts: 3
consumer: #Consumer-specific configuration (configuration item, see: PushConsumerBuilder)
consumerGroup: "${}_${}"
consumptionThreadCount: 2
maxCacheMessageCount: 1
maxCacheMessageSizeInBytes: 1
- Adding the java configurator
@Configuration
public class RocketmqConfig {
private ClientServiceProvider clientProvider = ();
@Bean
public ClientConfiguration client(@Inject("${}") Properties common){
ClientConfigurationBuilder builder = ();
//Injecting Properties
(builder, common);
return ();
}
@Bean
public Producer producer(@Inject("${}") Properties producer,
ClientConfiguration clientConfiguration) throws ClientException {
ProducerBuilder producerBuilder = ();
//Injecting Properties
if (() > 0) {
(producerBuilder, producer);
}
(clientConfiguration);
return ();
}
@Bean
public PushConsumer consumer(@Inject("${}") Properties consumer,
ClientConfiguration clientConfiguration,
MessageListener messageListener) throws ClientException{
//select on demand PushConsumerBuilder maybe SimpleConsumerBuilder
PushConsumerBuilder consumerBuilder = ();
//Injecting Properties
(consumerBuilder, consumer);
Map<String, FilterExpression> subscriptionExpressions = new HashMap<>();
("", new FilterExpression("*"));
(subscriptionExpressions);
(clientConfiguration);
(messageListener);
return ();
}
}
//This implementation class,(as opposed to StaticSessionCredentialsProvider)Easy to configure auto-injection
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、Code application
- sent (or produced), here the generation controller is requested by the user to then send the message (FYI):
@Controller
public class DemoController {
@Inject
private Producer producer;
@Mapping("/send")
public void send(String msg) throws ClientException {
//dispatch
(new MessageBuilderImpl()
.setTopic("")
.setBody(())
.build());
}
}
- Listening (or consuming), the subscription callback is used here: (FYI)
@Component
public class DemoMessageListener implements MessageListener {
@Override
public ConsumeResult consume(MessageView messageView) {
(messageView);
return ;
}
}