Spring boot:将消息推送到每个请求的特定主题
Posted
技术标签:
【中文标题】Spring boot:将消息推送到每个请求的特定主题【英文标题】:Spring boot: push message to specific topic for each request 【发布时间】:2020-04-01 23:55:02 【问题描述】:我正在使用 pub sub 与 spring boot 的集成,我的配置类如下所示:
@Configuration
public class PubSubConfiguration
@Value("$spring.pubsub.topic.name")
private String topicName;
@Bean
@ServiceActivator(inputChannel = "MyOutputChannel")
public PubSubMessageHandler messageSender(PubSubTemplate pubsubTemplate)
return new PubSubMessageHandler(pubsubTemplate, topicName);
@MessagingGateway(defaultRequestChannel = "MyOutputChannel")
public interface PubsubOutboundGateway
void sendToPubsub(String attribute);
所以现在,我只调用了 sendToPubSub 方法,该方法将有效负载从我的应用程序添加到主题中,如下所示:
@Autowired
private PubSubConfiguration.PubsubOutboundGateway outboundGateway;
// used line in my code wherever is needed.
outboundGateway.sendToPubsub(jsonInString);
以上代码仅适用于我从应用程序属性文件加载的一个主题。
但是现在我想让我的主题名称动态添加到messageSender中,该怎么做。
【问题讨论】:
【参考方案1】:考虑创建一个BeanFactory 来生成一个给定主题名称的 PubSubMessageHandler Bean。 PubSubMessageHandler 还有一个 setTopic() 方法,可能有用。
【讨论】:
以上是关于Spring boot:将消息推送到每个请求的特定主题的主要内容,如果未能解决你的问题,请参考以下文章