新增rocketmq-demo演示工程
This commit is contained in:
+48
@@ -0,0 +1,48 @@
|
||||
package com.rocketmq;
|
||||
|
||||
import com.rocketmq.demo.model.Order;
|
||||
import com.rocketmq.demo.service.SenderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* Rocketmq生产者 demo
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class RocketMqProduceApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RocketMqProduceApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CustomRunner customRunner() {
|
||||
return new CustomRunner();
|
||||
}
|
||||
|
||||
/**
|
||||
* 工程启动后执行
|
||||
* 共发送5条消息:2条为字符消息,3条为带tag的对象消息
|
||||
*/
|
||||
public static class CustomRunner implements CommandLineRunner {
|
||||
@Autowired
|
||||
private SenderService senderService;
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
int count = 5;
|
||||
for (int index = 1; index <= count; index++) {
|
||||
String msgContent = "msg-" + index;
|
||||
if (index % 2 == 0) {
|
||||
senderService.send(msgContent);
|
||||
} else {
|
||||
senderService.sendWithTags(new Order((long)index, "order-"+index), "tagObj");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.rocketmq.demo.config;
|
||||
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.Output;
|
||||
import com.rocketmq.demo.config.RocketMqConfig.MySource;
|
||||
import org.springframework.cloud.stream.messaging.Source;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
|
||||
/**
|
||||
* 配置消息生产者
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
@EnableBinding({MySource.class})
|
||||
public class RocketMqConfig {
|
||||
public interface MySource {
|
||||
@Output(Source.OUTPUT)
|
||||
MessageChannel output();
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.rocketmq.demo.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 订单实体
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class Order implements Serializable {
|
||||
private static final long serialVersionUID = 2801814838883246461L;
|
||||
|
||||
private Long orderId;
|
||||
private String orderNo;
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.rocketmq.demo.service;
|
||||
|
||||
import org.apache.rocketmq.common.message.MessageConst;
|
||||
import com.rocketmq.demo.config.RocketMqConfig.MySource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@Service
|
||||
public class SenderService {
|
||||
@Autowired
|
||||
private MySource source;
|
||||
|
||||
/**
|
||||
* 发送字符消息
|
||||
*/
|
||||
public void send(String msg) {
|
||||
source.output().send(MessageBuilder.withPayload(msg).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送带tag的对象消息
|
||||
*/
|
||||
public <T> void sendWithTags(T msg, String tag) {
|
||||
Message message = MessageBuilder.withPayload(msg)
|
||||
.setHeader(MessageConst.PROPERTY_TAGS, tag)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
|
||||
.build();
|
||||
source.output().send(message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user