Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 回调驱动的Spring云数据流源应用程序_Java_Spring_Spring Cloud Stream_Spring Cloud Dataflow - Fatal编程技术网

Java 回调驱动的Spring云数据流源应用程序

Java 回调驱动的Spring云数据流源应用程序,java,spring,spring-cloud-stream,spring-cloud-dataflow,Java,Spring,Spring Cloud Stream,Spring Cloud Dataflow,我想创建一个基于lib的springclouddataflow源应用程序,该lib连接到消息传递服务IRC,并在消息到达时调用我的回调。源应用程序的唯一目标是从接收到的IRC消息创建一条SCDF消息,并将其发送到流 我提出了以下解决方案: 用@Component注释的IRCStener类进行一些配置,并在调用start方法时开始侦听IRC消息。当收到消息时,其onGenericMessage回调仅通过注入的源属性将消息发送到流: @Component public class IrcListen

我想创建一个基于lib的springclouddataflow源应用程序,该lib连接到消息传递服务IRC,并在消息到达时调用我的回调。源应用程序的唯一目标是从接收到的IRC消息创建一条SCDF消息,并将其发送到流

我提出了以下解决方案:

用@Component注释的IRCStener类进行一些配置,并在调用start方法时开始侦听IRC消息。当收到消息时,其onGenericMessage回调仅通过注入的源属性将消息发送到流:

@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 Cloud数据流领域中采取的正确方法,或者可能我遗漏了一些重要的东西?

看起来不错;但是,一般来说,消息驱动源扩展了MessageProducerSupport并调用sendMessageMessage

并在本例中重写doStart


如果发送失败,您可以访问邮件历史记录跟踪和错误处理。

谢谢!但是给我的机器人打电话安全吗;它进入MessageProducerSupport子类的doStart方法中的无限事件侦听循环?不,如果它阻止,它将停止应用程序启动。您需要使用TaskExecutor在不同的线程上运行它;在主线程上调用doStart。现在调用start是什么?目前我在应用程序的main中调用它-您可以看到问题中最后一行有意义的代码。是的,这与其他消息驱动源一致。您这样做是可以的,因为它是在上下文启动之后。对于MPS,在加载上下文时调用start,除非将autoStartup设置为false。
@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();
    }
}