<dependency>
<groupId>com.maihaoche</groupId>
<artifactId>spring-boot-starter-rocketmq</artifactId>
<version>0.0.5</version>
</dependency>
rocketmq:
name-server-address: 172.21.10.111:9876
# 可选, 如果无需发送消息则忽略该配置
producer-group: local_pufang_producer
# 发送超时配置毫秒数, 可选, 默认3000
send-msg-timeout: 5000
# 追溯消息具体消费情况的开关
#trace-enabled: true
在springboot应用主入口添加@EnableMQConfiguration
注解开启自动装配:
@SpringBootApplication
@EnableMQConfiguration
class CamaroDemoApplication {
}
可以通过@MQKey
注解将消息POJO中的对应字段设置为消息key,通过prefix
定义key的前缀:
data class DemoMessage(
@MQKey(prefix = "sku_")
val skuId:Long,
val skuType:String)
详见wiki:
@MQProducer
class DemoProducer : AbstractMQProducer() {
}
详见wiki:
@MQConsumer(consumerGroup = "local_pufang_test_consumer", topic = "suclogger")
class DemoConsumer : AbstractMQPushConsumer<DemoMessage>() {
override fun process(message: DemoMessage?, extMap: MutableMap<String, Any>?): Boolean {
// extMap 中包含messageExt中的属性和message.properties中的属性
println("message id : ${extMap!![MessageExtConst.PROPERTY_EXT_MSG_ID]}")
return true
}
}
// 注入发送者
@Autowired
lateinit var demoProducer:DemoProducer
...
// 发送
demoProducer.syncSend("suclogger", DemoMessage(1, "plain_message"))