我们有一个Spring Web应用程序,我们将从Spring 3.2移植到Spring 4.当Web应用程序启动时,我们的应用程序将几个子上下文组合成一个运行时上下文.
我们在两个子上下文中使用单独的TaskScheduler.使用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]
Run Code Online (Sandbox Code Playgroud)
一个调度程序通过以下方式定义
<?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>
Run Code Online (Sandbox Code Playgroud)
另一个调度程序定义在(为清楚起见,删除了其他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>
Run Code Online (Sandbox Code Playgroud)
上下文在运行时使用(为清晰起见而删除了其他上下文)进行汇总:
<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>
Run Code Online (Sandbox Code Playgroud)
为什么Spring 4有这个限制?应该如何解决这个问题?
Rya*_*ugh 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);
}
}
Run Code Online (Sandbox Code Playgroud)
一旦我这样做,问题便消失了,事情按照预期发挥作用.不好的一面是你不能使用像@Scheduled等等的注释.但你确实获得了更多的控制和事情.希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
15016 次 |
| 最近记录: |