如何在app启动时执行spring bean的多个方法

时间:2022-07-25 04:02:27

I have a spring bean with 4 blocking queues. Each queue is assigned a method (named processQueueX() ) which calls take() on that queue and processes taken object from queue.

我有一个带有4个阻塞队列的spring bean。为每个队列分配一个方法(名为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. Each method needs to be called once, so scheduling was a bad idea I guess.

我想在app启动时在一个单独的线程中调用这些方法中的每一个。我尝试使用任务调度程序和固定延迟设置,但这在某种程度上阻止了tomcat并且它停止响应请求。每个方法都需要调用一次,所以我猜测调度是一个坏主意。

Init method does not work also since it works in a single thread, each method has endless loop to process queue forever.

Init方法也不起作用,因为它在单个线程中工作,每个方法都有无限循环来永久处理队列。

Is there a way to call these methods declaratively from spring config file in manner similar to task namespace? Or programmatically?

有没有办法以类似于任务名称空间的方式从spring配置文件声明性地调用这些方法?还是以编程方式?

Tnx

2 个解决方案

#1


0  

I think using scheduler not a bad idea use quart scheduler with simple trigger thus quarz will do threading for you and tomcat not effected .And configure quartz with just enough number of thread.

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

#2


0  

Would 23.4. The Spring TaskExecutor abstraction help?

将23.4。 Spring TaskExecutor抽象有帮助吗?

Where the example has a MessagePrinterTask class, you would have similar, but your run() method would access one of the queues. 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.

如果示例具有MessagePrinterTask类,您将具有类似的,但您的run()方法将访问其中一个队列。您可以设置Spring配置以将其中一个队列注入任务,因此根据队列的相似程度,您可以使用相同的Runnable任务。

#1


0  

I think using scheduler not a bad idea use quart scheduler with simple trigger thus quarz will do threading for you and tomcat not effected .And configure quartz with just enough number of thread.

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

#2


0  

Would 23.4. The Spring TaskExecutor abstraction help?

将23.4。 Spring TaskExecutor抽象有帮助吗?

Where the example has a MessagePrinterTask class, you would have similar, but your run() method would access one of the queues. 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.

如果示例具有MessagePrinterTask类,您将具有类似的,但您的run()方法将访问其中一个队列。您可以设置Spring配置以将其中一个队列注入任务,因此根据队列的相似程度,您可以使用相同的Runnable任务。