1

我第一次使用 Spring 任务执行器系统,但无法让它工作......

我已经阅读了关于 SO 的各种帖子,但没有任何迹象表明该任务正在执行。首先,我在我的服务 bean 上尝试了 @Scheduled 注释,但在阅读了这遇到了 AOP 代理问题后,我使用了直接的 XML 配置,如下所示:

<task:executor id="executorWithPoolSizeRange"
        pool-size="5-25"
        queue-capacity="100" />       
 <task:scheduler id="taskScheduler" pool-size="2" /> 
 <task:scheduled-tasks>
 <task:scheduled ref="fileWriter" method="test" fixed-rate="5000" />
</task:scheduled-tasks>

使用 fileWriter bean 是一个常规的 Spring bean,测试方法如下:

public void test (){
   System.err.println("run in job");
}

通过使用 DEBUG 日志记录设置运行,我知道以下内容:

  1. bean 被加载并初始化。
  2. 如果我输错了“方法”属性的名称,则会引发异常,因此至少会解析任务定义。
  3. 调试语句中没有任何内容表明任务的任何激活
  4. 永远不会触发测试方法中的断点。

我希望在我的应用程序或弹簧单元测试运行时每 5 秒看到一次,以查看控制台上打印出的 test() 方法的消息。我正在使用 Spring 3.0.6 并在 Mac 10.6 Java 6 上的 Eclipse 3.7 中测试通过 Jetty 运行的应用程序。我们使用的所有其他 Spring 功能(数据库、安全性、MVC 工作正常)。非常感谢您的任何建议!

4

2 回答 2

1

If you haven't try adding Quartz to your classpath (you shouldn't need it but...).

Whats most likely happening is that the scheduler is running and failing to execute your proxy. The proxy probably throws an exception and the scheduler's exception policy is probably discarding it.

The other option... (and I am probably going to get down voted) is to not use Springs scheduler. Unless you need the Quartz Cron stuff I find Springs Task scheduler overly complicated and yet weak compared to:

Guava's ListeningScheduledExecutorService

The listening executor service will allow you to chain events. You can easily wrap the above in some service bean. Yes I know you probably want the decoupling that Spring provides... but you can get far better decoupling and event based by combing the ExecutorService and Guava's EventBus.

于 2012-10-16T01:55:00.817 回答
0

尝试这个:

 <task:scheduled-tasks scheduler="taskScheduler">
   <task:scheduled ref="fileWriter" method="test" fixed-rate="5000" />
 </task:scheduled-tasks>
于 2012-10-15T09:37:23.523 回答