0
点赞
收藏
分享

微信扫一扫

spring实现方式

爱喝酒的幸福人 2022-02-06 阅读 81

spring对Java的Timer类和Quartz都提供了一个抽象层,使用我们可以更方便地使用它们。

1)spring对Timer的集成

<?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:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"

default-autowire="byName">

<bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">

<property name="scheduledTimerTasks">

<list>

<ref local="schedule" />

</list>

</property>

</bean>

<bean id="schedule" class="org.springframework.scheduling.timer.ScheduledTimerTask">

<property name="timerTask">

<ref bean="myTask" />

</property>

<property name="delay">

<value>1000</value>

</property>

<property name="period">

<value>1000</value>

</property>

</bean>

<bean id="myTask" class="com.test.MyTask" />

</beans>

2)spring对quartz的集成 

<?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:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"

default-autowire="byName">

<bean id="myTask" class="com.test.MyTask" />

<!-- jobDetail -->

<bean id="quartzJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

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

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

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

</bean>

<!-- trigger -->

<bean id="quartzTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

    <property name="jobDetail" ref="quartzJobDetail" />

    <property name="cronExpression" value="0/3 * * * * ?"/>

</bean>

<!-- schdule -->

<bean id="quertzSchdule" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

      <property name="triggers">

         <list>

            <ref bean="quartzTrigger" />

         </list>

      </property>

   </bean>

</beans>

 在spring容器加载配置文件中的bean后,任务调度就会开始。 

举报

相关推荐

0 条评论