简体   繁体   English

如何在应用启动时执行更多的Spring bean方法

[英]How to execute more then one method of a spring bean on app startup

I have a spring bean with 4 blocking queues. 我有一个带有4个阻塞队列的spring bean。 Each queue is assigned a method (named processQueueX() ) which calls take() on that queue and processes taken object from queue. 每个队列都分配了一个方法(名为processQueueX()),该方法在该队列上调用take()并处理从队列中获取的对象。

I want to call each of those method in a separate thread on app startup. 我想在应用程序启动时在单独的线程中调用每个方法。 I tried with task scheduler and fixed-delay setting but that in some way blocks tomcat and it stops responding to requests. 我尝试使用任务计划程序和固定延迟设置,但是以某种方式阻止了tomcat,并且它停止了对请求的响应。 Each method needs to be called once, so scheduling was a bad idea I guess. 每个方法都需要调用一次,所以调度是一个坏主意。

Init method does not work also since it works in a single thread, each method has endless loop to process queue forever. 初始化方法也不起作用,因为它在单个线程中工作,每种方法都有无限循环以永远处理队列。

Is there a way to call these methods declaratively from spring config file in manner similar to task namespace? 有没有办法以类似于任务名称空间的方式从spring config文件中声明性地调用这些方法? Or programmatically? 还是以编程方式?

Tnx 特纳克斯

我认为使用调度程序不是一个坏主意,使用具有简单触发器的quart调度程序,因此quarz将为您执行线程操作并且tomcat不会受到影响。并使用足够数量的线程配置石英。

Would 23.4. 23.4。 The Spring TaskExecutor abstraction help? Spring TaskExecutor抽象有帮助吗?

Where the example has a MessagePrinterTask class, you would have similar, but your run() method would access one of the queues. 在示例具有MessagePrinterTask类的地方,您将具有类似的类,但是run()方法将访问队列之一。 You would set up your Spring config to inject one of the queues into the task, so depending on how similar your queues are, you might be able to use the same Runnable task. 您将设置Spring配置以将一个队列注入任务,因此,根据队列的相似程度,您也许可以使用相同的Runnable任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM