本文介绍了为什么Spring 4在上下文中只允许一个TaskScheduler?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Spring Web应用程序,该应用程序将从Spring 3.2移植到Spring4.我们的应用程序在启动Web应用程序时将几个子上下文组合到一个运行时上下文中.

We have a Spring web application we're porting from Spring 3.2 to Spring 4. Our application has several sub-contexts assembled into a single runtime context when the web application is started.

我们在两个子上下文中使用单独的TaskScheduler.使用Spring 3.2可以正常工作.使用Spring 4时,我们会收到以下消息的异常:

We use separate TaskSchedulers in two of our sub-contexts. With Spring 3.2 this works fine; when using Spring 4 we get an exception with the following message:

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]

通过以下方式定义一个调度程序:

One scheduler is defined via:

<?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)中定义的:

The other scheduler is defined in (additional beans removed for clarity):

<?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>

在运行时使用以下命令组装上下文(为了清楚起见,删除了其他上下文):

The contexts are assembled at runtime using (additional contexts removed for clarity):

<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有此限制?一个应该如何解决?

Why does Spring 4 have this restriction? How should one work around it?

推荐答案

是的,可以肯定,这是行为上的改变.看来现在每个上下文最多只能有2个调度程序.我在Spring 4的新WebSocket消息代理程序中遇到了这个问题,因为它为Simple STOMP代理程序和SockJS适配器分配了2个调度程序.当我添加自己的消息时,它完全被您收到的相同消息弄糊涂了.我很烦恼,我不得不通过错误而不是文档来找出此限制. Spring 4文档中似乎没有描述此陷阱.

Yeah, this is a change in behavior for sure. It appears that the ScheduledAnnotationBeanPostProcessor now has a limit of 2 schedulers per context. I ran into this with Spring 4's new WebSocket message broker as it allocates 2 schedulers for the Simple STOMP broker and the SockJS adapter. When I added my own, it totally dorked out with the same message you got. I found it rather annoying that I had to find out this limitation via errors rather documentation. This gotcha doesn't appear to be described in the Spring 4 documentation.

解决方案是创建管理自己的TaskScheduler的自己的SchedulingConfigurer.我怀疑原因是只有一种,并且需要添加其他TaskScheduler实现以将它们彼此隔离.我做了这样的事情:

The solution is to create your own SchedulingConfigurer that manages its own TaskScheduler. I suspect that the reason is that there is only one and additional TaskScheduler implementations need to be added in order to isolate them from one another. I did something like so:

@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等注释.但是您却获得了更多的控制权,并且一切正常.希望这会有所帮助.

Once I did that, the issue went away and things worked as desired. The down side is that you can't use annotations like @Scheduled, etc.. But you do gain more control and things work. Hope this helps.

这篇关于为什么Spring 4在上下文中只允许一个TaskScheduler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:17