我们有一个Spring Web应用程序,我们正在从Spring 3.2移植到Spring 4.我们的应用程序在Web应用程序启动时将几个子上下文组合成一个运行时上下文。
我们在两个子上下文中使用单独的TaskSchedulers。使用Spring 3.2,这很好用;使用Spring 4时,我们会收到以下消息的异常:
java.lang.IllegalStateException: More than one TaskScheduler and/or ScheduledExecutorService exist within the context. Remove all but one of the beans; or implement the SchedulingConfigurer interface and call ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback. Found the following beans: [commonScheduler, communicationTaskScheduler]
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:289) ~[spring-context-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:72) ~[spring-context-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:98) ~[spring-context-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:333) ~[spring-context-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:776) ~[spring-context-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:485) ~[spring-context-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) ~[spring-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) ~[spring-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) ~[spring-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4961) ~[catalina.jar:7.0.50]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5455) ~[catalina.jar:7.0.50]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[catalina.jar:7.0.50]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) ~[catalina.jar:7.0.50]
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) ~[catalina.jar:7.0.50]
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:634) ~[catalina.jar:7.0.50]
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1074) ~[catalina.jar:7.0.26]
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1858) ~[catalina.jar:7.0.26]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) ~[na:1.7.0_25]
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) ~[na:1.7.0_25]
at java.util.concurrent.FutureTask.run(FutureTask.java:166) ~[na:1.7.0_25]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) ~[na:1.7.0_25]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) ~[na:1.7.0_25]
at java.lang.Thread.run(Thread.java:724) ~[na:1.7.0_25]
通过以下方式定义一个调度程序:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<!-- Enables annotation-driven task scheduling; detects @Scheduled- and
@Async-annotated process methods to be invoked via proxy -->
<task:annotation-driven mode="aspectj" />
<task:scheduler id="commonScheduler" pool-size="5" />
</beans>
定义了另一个调度程序(为清楚起见,删除了其他bean):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<context:spring-configured />
<bean id="communicationExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="15" />
<property name="maxPoolSize" value="20" />
<property name="queueCapacity" value="20" />
</bean>
<bean id="communicationTaskScheduler"
class="org.springframework.scheduling.concurrent.ConcurrentTaskScheduler">
<property name="concurrentExecutor" ref="communicationExecutor" />
</bean>
</beans>
在运行时使用(为清晰起见而删除了其他上下文)汇总了上下文:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<import resource="classpath:spring/tasks-context.xml" />
<import resource="classpath:spring/collectors-context.xml" />
</beans>
为什么Spring 4有这个限制?应该如何解决它?
答案 0 :(得分:16)
是的,这肯定会改变行为。看起来ScheduledAnnotationBeanPostProcessor现在每个上下文限制为2个调度程序。我使用Spring 4的新WebSocket消息代理遇到了这个问题,因为它为Simple STOMP代理和SockJS适配器分配了2个调度程序。当我添加自己的时候,它完全沉浸在你得到的同样的信息中。我发现相当恼人的是我必须通过错误而不是文档找出这个限制。这个问题似乎没有在Spring 4文档中描述过。
解决方案是创建自己的SchedulingConfigurer
来管理自己的TaskScheduler
。我怀疑原因是只有一个,并且需要添加额外的TaskScheduler
实现以便将它们彼此隔离。我做了类似的事情:
@Configuration
@EnableScheduling
public class MySchedulingConfigurer implements SchedulingConfigurer
{
@Bean
public TimedThingy timedThingy()
{
return new TimedThingy();
}
@Bean()
public ThreadPoolTaskScheduler taskScheduler() {
return new ThreadPoolTaskScheduler();
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar)
{
taskRegistrar.setTaskScheduler(taskScheduler());
taskRegistrar.addFixedRateTask(new Runnable()
{
public void run()
{
timedThingy().sendIt();
}
}, 1000);
}
}
一旦我这样做了,问题就会消失,事情会按照预期发挥作用。不好的一面是你不能使用@Scheduled
之类的注释等等。但是你确实获得了更多的控制权和事情。希望这会有所帮助。
答案 1 :(得分:0)
您需要明确指定要与@Scheduled
注释一起使用的调度程序。只需添加scheduler
属性:
<task:annotation-driven scheduler="commonScheduler" mode="aspectj" />
<task:scheduler id="commonScheduler" pool-size="5" />
答案 2 :(得分:0)
只需将bean添加到您的配置中:
@Bean()
public ThreadPoolTaskScheduler taskScheduler() {
return new ThreadPoolTaskScheduler();
}