2 ways to integrate quartz2 with springmvc

 

2 ways to integrate quartz2 with springmvc

 

The first way: jobDetail uses MethodInvokingJobDetailFactoryBean

 

(1) spring configuration

<!-- Scheduled task Bean: quartz can't get the annotated userService, use the xml file and add a UserService object set to inject the Job -->

<bean id="myJob" class="cn.mgr.job.demo.MyJob">

<property name="userService" ref="_userService"></property>

</bean>

 

<bean id="_userService" class="cn.mgr.user.service.UserService"></bean>

 

<!-- Define the calling object and the method of calling the object -->

<bean id="myJobDetail"

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

<!-- Called class -->

<property name="targetObject" ref="myJob" />

<!-- call a method in the class -->

<property name="targetMethod" value="execute" />

<!-- Whether to allow concurrent execution of tasks. When the value is false, it means that a new thread must be started after the previous thread is processed -->

<property name="concurrent" value="false" />

</bean>

 

<!-- Configuration after quartz-2.x-->

<bean id="myJobTrigger"

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

<property name="jobDetail">

<ref bean="myJobDetail" />

</property>

<property name="cronExpression">

<value>0/5 * * * * ?</value>

</property>

</bean>

 

 

<!-- General management class: If lazy-init='false', the scheduler will be executed after the container starts -->

<!-- If lazy-init='true', the bean needs to be instantiated to execute the scheduler -->

<bean name="simpleScheduler" lazy-init="true" autowire="no"

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

<property name="triggers">

<list>

<ref bean="myJobTrigger" />

</list>

</property>

<property name="autoStartup" value="true"/>

</bean>

 

(2)Job

public class MyJob {

 

private IUserService userService;

 

public void execute(){

System.out.println((new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS")).format(new Date()));

System.out.println(userService.getMessage() + "%S%%");

 

}

 

public void setUserService(IUserService userService) {

this.userService = userService;

}

 

}

 

The second way: jobDetail uses org.springframework.scheduling.quartz.JobDetailFactoryBean

 

(1) spring configuration

<bean id="roleService2" class="cn.mgr.role.service.RoleService"></bean>

 

<!-- Define the calling object and the method of calling the object -->

<bean id="simpleJobDetail"

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

<!-- the class called 

<property name="targetObject" ref="simpleJob" />-->

<!-- call the method in the class

<property name="targetMethod" value="execute" />  -->

<!-- Whether to allow concurrent execution of tasks. When the value is false, it means that a new thread must be started after the previous thread is processed. 

<property name="concurrent" value="false" />-->

<property name="jobClass" value="cn.mgr.job.demo.SimpleJob" />

        <property name="group" value="TEST" />

        <property name="durability" value="true" />

</bean>

 

<!-- Configuration after quartz-2.x-->

<bean id="simpleJobTrigger"

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

<property name="jobDetail">

<ref bean="simpleJobDetail" />

</property>

<property name="cronExpression">

<value>0/5 * * * * ?</value>

</property>

</bean>

 

 

<!-- General management class: If lazy-init='false', the scheduler will be executed after the container starts -->

<!-- If lazy-init='true', the bean needs to be instantiated to execute the scheduler -->

<bean name="simpleScheduler" 

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

<property name="triggers">

<list>

<ref bean="simpleJobTrigger" />

</list>

</property>

<!-- <property name="autoStartup" value="true"/> -->

<!-- Start Scheduler with a delay of 30 seconds -->  

<property name="startupDelay" value="20"></property>

                <!-- Configure the spring context through the applicationContextSchedulerContextKey property-->

<property name="applicationContextSchedulerContextKey">  

<value>applicationContext</value>  

</property>  

<!-- <property name="dataSource" ref="dataSource" />       -->

        <property name="schedulerContextAsMap">      

            <map>      

                <!-- The service managed by spring needs to be placed here to be able to inject successfully -->      

                <description>schedulerContextAsMap</description>      

                <entry key="roleService2" value-ref="roleService2"/>  

                <entry key="userService" value-ref="userService"/>    

            </map>      

        </property>

<!--         <property name="configLocation" value="classpath:quartz.properties" /> -->

</bean>

 

(2)Job

public abstract class Job extends QuartzJobBean{

 

protected void init(JobExecutionContext context){

Scheduler scheduler = (Scheduler) context.getScheduler();  

DataSource dataSource = null;

//Get the service object in the JobExecutionContext  

SchedulerContext schCtx = null;

try {

schCtx = context.getScheduler().getContext();

} catch (SchedulerException e) {

e.printStackTrace ();

}

   //Get the context in Spring  

ApplicationContext appCtx = (ApplicationContext)schCtx.get(Const.APPLICATION_CONTEXT);

   //sqlSessionFactory = (SqlSessionFactory)appCtx.getBean(Const.SQL_SESSION_FACTORY);

   dataSource = (DataSource)appCtx.getBean(Const.DATA_SOURCE);

   System.out.println("**** ds = "+ dataSource);

   Object o = appCtx.getBean("roleService2");

   System.out.println("**** roleService = "+ o);

   

   Object userService = appCtx.getBean("userService");

   System.out.println("**** userService = "+ userService);

}

}

 

public class SimpleJob extends Job{

 

private IRoleService roleService;

 

@Override

protected void executeInternal(JobExecutionContext context)

throws JobExecutionException {

init(context);

 

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

}

 

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326207198&siteId=291194637