osworkflow api 之 timer

来源:互联网 发布:中国移动网络电视 投诉 编辑:程序博客网 时间:2024/05/17 07:25

 

com.opensymphony.workflow.timer

这部分主要是实现定时任务处理。在这里主要应用的是Quartz。这个也是opensymphony的一个开源东东

这个包里主要包括下面这三个类:

下面我想还是先从我熟悉一点的角度来阐述这个定时器,在spring中也有Quartz。在org.springframework.scheduling.quartz包里。

spring中的核心思想是,通过springbean配置文件配置trigger和自己的具体job

如一个配置文件为:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

     <description>test spring</description>

    

     <bean name="myJob"

            class="org.springframework.scheduling.quartz.JobDetailBean">

            <property name="jobClass">

                   <value>ce.net.quartz.TestJob</value>

            </property>

 

            <property name="jobDataAsMap">

                   <map>

                          <entry key="timeout">

                                 <value>5</value>

                          </entry>

                   </map>

            </property>

     </bean>

 

     <bean id="cronTrigger"

            class="org.springframework.scheduling.quartz.CronTriggerBean">

            <property name="jobDetail">

                   <ref bean="myJob" />

            </property>

            <property name="cronExpression">

                   <value>0 49 10 * * ?</value>

            </property>

     </bean>

 

     <bean

            class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

            <property name="triggers">

                   <list>

                          <ref local="cronTrigger" />

                   </list>

            </property>

     </bean>

</beans>

 

如上,配置文件配置了需要做的具体job类、trigger解释一下:<value>0 49 10 * * ?</value>代表秒分时*****。如上就是在每天的1049分自动触发此任务。其他就很容易理解了。

具体的监听类:

public class WebListener implements ServletContextListener

{

   

    Scheduler scheduler =null;

    public WebListener()

    {

        super();

       

    }

    /* (非 Javadoc

     * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)

     */

    public void contextInitialized(ServletContextEvent arg0)

    {

        try

        {

            String spring_config_path=arg0.getServletContext().getRealPath("/")+"WEB-INF//spring_config.xml";

            System.out.println("spring_config_path="+spring_config_path);

            ApplicationContext applicationContext=new FileSystemXmlApplicationContext(spring_config_path);

            JobDetailBean job = (JobDetailBean) applicationContext

                            .getBean("myJob");

            CronTriggerBean trigger = (CronTriggerBean) applicationContext

                            .getBean("cronTrigger");

            scheduler = StdSchedulerFactory.getDefaultScheduler();

            //scheduler.scheduleJob(job, trigger);

            scheduler.start();

        }catch (Exception exc)

        {

            exc.printStackTrace();

        }

    }

 

    /* (非 Javadoc

     * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)

     */

    public void contextDestroyed(ServletContextEvent arg0)

    {

        try

        {

            scheduler.shutdown(true);

        }catch (Exception exc)

        {

           

        }

    }

}

 

 

具体的job

 

public class TestJob extends QuartzJobBean

{

    private int timeout;

 

    /**

     * @param timeout

     */

    public void setTimeout(int timeout)

    {

     this.timeout = timeout;

    }

 

    /* (非 Javadoc

     * @see org.springframework.scheduling.quartz.QuartzJobBean#executeInternal(org.quartz.JobExecutionContext)

     */

    protected void executeInternal(JobExecutionContext arg0)

                    throws JobExecutionException

    {

        File file = new File("D:/QuartzOut.txt");

        try

        {

            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String dateAndTime=dateFormat.format(new Date());

            BufferedWriter bw=new BufferedWriter(new FileWriter(file));

            PrintWriter pw=new PrintWriter(bw);

            pw.println("Quarta is runned at "+dateAndTime);

            bw.close();

            pw.close();

        } catch (IOException e)

        {

            // TODO 自动生成 catch

            e.printStackTrace();

        }

    }

}

这样,假如此小项目例子部署到tomcat中,则在每次tomcat启动的时候,自动加载就会在每天的1049分处理此TestJob类。

 

通过上面,大家应该可以对quartz有个大致理解,下面我们来一起学习一下osworkflow里的quartz类。

 

LocalWorkflowJob

这类主要包括了一个execute方法,是具体需要执行的job,里面具体用到了idtriggerid还有username(主要用做实例化一个wf对象)

最后通过wf.executeTriggerFunction来执行具体的实际内容。

QuartzRunner

包括一个main方法和一个shutdown方法。

Main主要做的s.startsscheduleshutdown则是关闭。

WorkflowJob

相对LocalWorkflowJob来说,它不是针对本地的。通过soapwsdl来实现的。具体这部分是具体如何实现的,还没细了解。以后如果工作涉及到,会近一步学习。

Shutdown

废类,和QuartzRunner 一样。可能是作者手误吧。

 

怎么样和上面的spring对应起来的,其实QuartzRunner就是相当于上面监听类。负责schedule的启动,而LocalWorkflowJob则是具体执行工作的方法。

 

<function type="class">
 
<arg name="class.name">com.opensymphony.workflow.util.ScheduleJob</arg>
 
<arg name="triggerId">1</arg>
 
<arg name="jobName">testJob</arg>
 
<arg name="triggerName">testTrigger</arg>
 
<arg name="groupName">test</arg>
 
<arg name="repeat">10</arg>
 
<arg name="repeatDelay">2000</arg>
 
<arg name="cronExpression">0,5,10,15,20,25,30,35,40,45,50,55 * * * * ?</arg>
 
<arg name="username">test</arg>
 
<arg name="password">test</arg>
 
<arg name="local">true</arg>
 
<arg name="schedulerStart">true</arg>
</function>

<trigger-functions>
 
<trigger-function id="1″ >
 
<function>
  

 
</function>
</trigger-functions>

 

ScheduleJob是一个FunctionProiver,因此具有execute方法。在该方法执行期间,ScheduleJob将会读取这些配置参数,创建好job实例(实际上是一个JobDetail实例)和trigger实例,然后启动schedule。大致流程如下:

根据传入的shedulerName参数,利用org.quartz.impl.StdSchedulerFactorygetScheduler方法创建sheduler实例,该实例实现了org.quartz.Scheduler接口;

根据传入的jobClass参数,决定创建何种Job实例,osworkflow自身提供了两种选择:WorkflowJobLocalWorkflowJob。前者支持SOAP协议,后者则是本地调用,它们都实现了org.quartz.Job接口。

创建一个描述Job信息的JobDetail实例,并做好初始设置;

若传入参数中未指定cronExpression,则创建SimpleTrigger,并设置好startDateendDaterepeat,否则创建CronTrigger

jobDetailtrigger准备完毕后,就可以启动schedule了:

上面的关于osworkflow的配置文件代码可能有错误,spring那部分可以保证正确。

针对osworkflowquartz的实践尚不足,请见谅,暂时只能分析到此!以后有实例会及时补上!

原创粉丝点击