feat: 升级SpringBoot和SpringCloud版本
This commit is contained in:
-20
@@ -1,20 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
+10
-7
@@ -1,37 +1,40 @@
|
||||
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.cloud.stream.function.StreamBridge;
|
||||
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;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@Service
|
||||
public class SenderService {
|
||||
@Autowired
|
||||
private MySource source;
|
||||
private final static String TEST_OUT = "test-out-0";
|
||||
|
||||
@Resource
|
||||
private StreamBridge streamBridge;
|
||||
|
||||
/**
|
||||
* 发送字符消息
|
||||
*/
|
||||
public void send(String msg) {
|
||||
source.output().send(MessageBuilder.withPayload(msg).build());
|
||||
streamBridge.send(TEST_OUT, MessageBuilder.withPayload(msg).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送带tag的对象消息
|
||||
*/
|
||||
public <T> void sendWithTags(T msg, String tag) {
|
||||
Message message = MessageBuilder.withPayload(msg)
|
||||
Message<T> message = MessageBuilder.withPayload(msg)
|
||||
.setHeader(MessageConst.PROPERTY_TAGS, tag)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
|
||||
.build();
|
||||
source.output().send(message);
|
||||
streamBridge.send(TEST_OUT, message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user