如何等待QueueChannel处理所有消息?

huangapple go评论76阅读模式
英文:

How await QueueChannel all message processed?

问题

考虑下面的代码:

@Configuration
public class MyConf {
    @MessagingGateway(defaultRequestChannel = "channel")
    public interface Sender {
        void send(String out);
    }
}

@Component
public class Consumer {
    @ServiceActivator(inputChannel = "channel", poller = @Poller(fixedRate = "100"))
    public void handle(String input) throws InterruptedException {
        //
    }
}

@Component
public class HistoricalTagRunner implements CommandLineRunner {
    @Autowired
    private Sender sender;

    @Override
    public void run(String... args) throws Exception {
        List<String> input = ...
        input.forEach(r -> sender.send(r));

        //ok,现在所有的输入都已发送,应用程序将退出
        //而不等待消息处理完成
    }
}

所以所有的消息都发送到消费者,但应用程序会在等待所有消息被处理之前退出。是否有方法告诉 Spring 等待直到所有在 "channel" 中的消息被处理?

英文:

Consider a code:

@Configuration
public class MyConf {
      @MessagingGateway(defaultRequestChannel = &quot;channel&quot;)
        public interface Sender {
            void send(String out);

        }

}

@Component
public class Consumer {
    @ServiceActivator(inputChannel = &quot;channel&quot;, poller = @Poller(fixedRate = &quot;100&quot;))
    public void handle(String input) throws InterruptedException {
        //
    }
}

@Component
public class HistoricalTagRunner implements CommandLineRunner {
      @Autowired
      private Sender sender;

      @Override
      public void run(String... args) throws Exception {
            List&lt;String&gt; input = ...
            input.forEach(r -&gt; sender.send(r));

            //ok, now all input is send and application exit
            //without waiting for message processing
      }
}

So all message are sent to consumer, but application exits without wating that all messages are processed
Is there a way to tell spring wait until all messages in "channel" are processed?

答案1

得分: 1

Spring应用实际上只是一个Java应用程序,Spring并不负责控制应用程序的运行方式。您可以使用任何Java特性将应用程序阻塞在主线程上,直到某个事件发生。

例如,在我们的示例中,我们使用System.in.read()来阻塞主线程:

System.out.println("Hit 'Enter' to terminate");
System.in.read();
ctx.close();

在这种情况下,最终用户必须从CLI输入内容以解除该线程的阻塞并退出程序。

另一种方法是等待某个CountDownLatch,如果您预先知道消息的数量。因此,在处理消息时,您的流程必须“倒数”这个门闩。

英文:

The Spring application is really just Java application and it is really not a Spring responsibility to control how your application is going to live. You can take into a service any Java feature to block a main thread until some event happens.

For example in our samples we use a System.in.read() to block main thread:

    System.out.println(&quot;Hit &#39;Enter&#39; to terminate&quot;);
	System.in.read();
	ctx.close();

In this case end-user must enter something from the CLI to unblock that thread and exit from the program.

Another way is to wait for some CountDownLatch if you know a number of messages in advance. So, you flow must "count down" thast latch when a message is processed.

huangapple
  • 本文由 发表于 2020年4月8日 15:54:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61095841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定