[英]Callback-driven Spring Cloud Dataflow source application
我想基于连接到消息传递服务(实际上是IRC)的lib创建一个Spring Cloud Dataflow源应用程序,并在消息到达时调用我的回调。 源应用程序的唯一目标是从收到的IRC消息创建SCDF消息并将其发送到流。
我想出了以下解决方案:
用@Component
注释的IrcListener
类进行一些配置,并在调用start()
方法时开始侦听IRC消息。 收到消息后,其onGenericMessage
回调仅通过注入的source
属性将消息发送至流:
@Component
public class IrcListener extends ListenerAdapter {
@Override
public void onGenericMessage(GenericMessageEvent event) {
Message msg = new Message();
msg.content = event.getMessage();
source.output().send(MessageBuilder.withPayload(msg).build());
}
private Source source;
private String _name;
private String _server;
private List<String> _channels;
public void start() throws Exception {
Configuration configuration = new Configuration.Builder()
.setName(_name)
.addServer(_server)
.addAutoJoinChannels(_channels)
.addListener(this)
.buildConfiguration();
PircBotX bot = new PircBotX(configuration);
bot.startBot();
}
@Autowired
public IrcListener(Source source) {
this.source = source;
_name = "ircsource";
_server = "irc.rizon.net";
_channels = Arrays.asList("#test".split(","));
}
}
主类运行Spring Application,并在IrcListener
组件上调用上述的start()
方法。
@EnableBinding(Source.class)
@SpringBootApplication
public class IrcStreamApplication {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(IrcStreamApplication.class, args);
context.getBean(IrcListener.class).start();
}
}
这样行之有效,并且消息已成功接收并发布到流中,但是我想知道这是否是在Spring(Cloud Dataflow)Universe中采用的正确方法,或者我可能错过了一些重要的东西?
看起来还不错; 但是,通常,消息驱动的源扩展了MessageProducerSupport
并调用sendMessage(Message<?>)
。
(在这种情况下,请重写doStart()
)。
它将使您能够访问消息历史记录跟踪和错误处理(如果发送失败)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.