当应用程序加载时,在 spring 中将类方法作为线程运行很热吗? [英] Hot to run a class method in spring as a thread when the app loads?

查看:44
本文介绍了当应用程序加载时,在 spring 中将类方法作为线程运行很热吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从quartz-schedualer 运行一个类,我需要它始终运行并与主应用程序并行运行.该类将始终检查要处理的文件夹中的新文件.我虽然将它作为一个侦听器包含在 web.xml 中,但是这样构造函数不会运行,只加载 calss.有什么建议吗?

i need to run a class from quartz-schedualer, and i need it to be running always and parallel form the main app. The class will always be cheking for new files in a folder to process. I though to include it as a listener in the web.xml, how ever this way the constructor does not run, only the calss is loaded. Any sugestions ?

这是我在 web.xml 中添加的内容:

Here what i added in the web.xml:

<listener>
        <listener-class>com.bamboo.common.util.QuartzSchedualer</listener-class>
</listener>

这是我声明类的方式:

public class QuartzSchedualer {

     public void QuartzSchedualer (){
                try{

                    // Grab the Scheduler instance from the Factory

                    Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();


                    // and start it off

                    scheduler.start();


                    scheduler.shutdown();

                }  catch (SchedulerException se) {
            se.printStackTrace();
        }
     }

}

先谢谢你!

推荐答案

您不需要将它包含在 web.xml 中,只需像您可能已经做的那样将您的 appcontext 加载到您的 web.xml 中,并处理调度春天之内:

You don't need to include it in web.xml, just load your appcontext in your web.xml as you already do probably, and deal with the scheduling within spring:

引用具有要调用的方法的业务对象的作业:

The job referring to your business object which has the method to be invoked:

<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="exampleBusinessObject" />
  <property name="targetMethod" value="doIt" />
  <property name="concurrent" value="false" />
</bean>

负责触发方法的触发器:

The trigger that takes care of firing the method:

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="exampleJob" />
  <!-- run every morning at 6 AM -->
  <property name="cronExpression" value="0 0 6 * * ?" />
</bean>

用于连接触发器的 schedulerFactoryBean:

The schedulerFactoryBean for wiring the trigger:

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
    <list>
      <ref bean="cronTrigger" />
    </list>
  </property>
</bean>

Spring 文档中进一步查看 2.5,此处 3.0.

See further in Spring documentation for 2.5, here for 3.0.

这篇关于当应用程序加载时,在 spring 中将类方法作为线程运行很热吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆