您的位置:首页 > 编程语言 > Java开发

spring spring scheduled配置示例

2013-10-06 14:48 204 查看


IUserinfoService.java

package com.lilin.iservice;

public interface IUserinfoService {

public void addUserinfo(String name) throws Exception;

}

UserinfoServiceImpl.java

package com.lilin.serviceimpl;

import com.lilin.iservice.IUserinfoService;

public class UserinfoServiceImpl implements IUserinfoService{

public void addUserinfo(String name) throws Exception{

System.out.println("添加了一个新的用户"+name);

}

}

Test2Task.java

  package com.lilin.schedule;

import com.lilin.iservice.IUserinfoService;

public class Test2Task {

private IUserinfoService userinfoService;

public void setUserinfoService(IUserinfoService userinfoService) {

this.userinfoService = userinfoService;

}

public void doIt(){

try {

userinfoService.addUserinfo("李林2");

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

TestTask.java
 package com.lilin.schedule;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.springframework.scheduling.quartz.QuartzJobBean;

import com.lilin.iservice.IUserinfoService;

 

public class TestTask extends QuartzJobBean {

private IUserinfoService userinfoService;

public void setUserinfoService(IUserinfoService userinfoService) {

this.userinfoService = userinfoService;

}

@Override

protected void executeInternal(JobExecutionContext jobExecutionContext)

throws JobExecutionException {

// TODO Auto-generated method stub

try {

userinfoService.addUserinfo("李林");

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

    

}

JdkTimerTask.java

package com.lilin.schedule;

import java.util.TimerTask;

import com.lilin.iservice.IUserinfoService;

public class JdkTimerTask extends TimerTask{

private IUserinfoService userinfoService;
public void setUserinfoService(IUserinfoService userinfoService) {
this.userinfoService = userinfoService;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
userinfoService.addUserinfo("jdk 李林");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
    

}
 

JdkTimerTask2.java

package com.lilin.schedule;

import java.util.TimerTask;

import com.lilin.iservice.IUserinfoService;

public class JdkTimerTask2{

private IUserinfoService userinfoService;

public void setUserinfoService(IUserinfoService userinfoService) {

this.userinfoService = userinfoService;

}

public void doIt() {

try {

userinfoService.addUserinfo("jdk 李林2");

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}



}

<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans ;
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop ;
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx ;
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ;
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/cache ;
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd ;
">
   
 <bean id="userinfoService1" class="com.lilin.serviceimpl.UserinfoServiceImpl"></bean>
  
 <!-- quartz方式1 -->  
 <bean id="testJob" class="org.springframework.scheduling.quartz.JobDetailBean">
  <property name="jobClass" value="com.lilin.schedule.TestTask" />
  <property name="jobDataAsMap">
    <map>
      <entry key="userinfoService"  value-ref="userinfoService1"/>
    </map>
  </property>
</bean>

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="testJob" />
    <!-- 
     0 0 12 * * ?在每天中午12:00触发 
0 15 10 ? * *每天上午10:15 触发 
0 15 10 * * ?每天上午10:15 触发 
0 15 10 * * ? *每天上午10:15 触发 
0 15 10 * * ? 2005在2005年中的每天上午10:15 触发 
0 * 14 * * ?每天在下午2:00至2:59之间每分钟触发一次 
0 0/5 14 * * ?每天在下午2:00至2:59之间每5分钟触发一次 
0 0/5 14,18 * * ?每天在下午2:00至2:59和6:00至6:59之间的每5分钟触发一次 
0 0-5 14 * * ?每天在下午2:00至2:05之间每分钟触发一次 
0 10,44 14 ? 3 WED每三月份的星期三在下午2:00和2:44时触发 
0 15 10 ? * MON-FRI从星期一至星期五的每天上午10:15触发 
0 15 10 15 * ?在每个月的每15天的上午10:15触发 
0 15 10 L * ?在每个月的最后一天的上午10:15触发 
0 15 10 ? * 6L在每个月的最后一个星期五的上午10:15触发 
0 15 10 ? * 6L 2002-2005在2002, 2003, 2004 and 2005年的每个月的最后一个星期五的上午10:15触发 
0 15 10 ? * 6#3在每个月的第三个星期五的上午10:15触发 
0 0 12 1/5 * ?从每月的第一天起每过5天的中午12:00时触发 
0 11 11 11 11 ?在每个11月11日的上午11:11时触发. 
     -->
    <property name="cronExpression" value="0 * * * * ?"></property>
</bean> 

<!-- quartz方式2 -->  
<bean id="test2Task" class="com.lilin.schedule.Test2Task">
   <property name="userinfoService" ref="userinfoService1"></property>
</bean>

<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="test2Task" />
  <property name="targetMethod" value="doIt" />
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
    <!-- see the example of method invoking job above -->
    <property name="jobDetail" ref="jobDetail" />
    <!-- 10 seconds -->
    <property name="startDelay" value="1000" />
    <!-- repeat every 10 seconds -->
    <property name="repeatInterval" value="10000" /> 
</bean> 
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
       <list>
          <ref bean="cronTrigger"/>
          <ref bean="simpleTrigger"/>
       </list>
    </property>
</bean>    

<!-- jdk timer 1-->
<bean id="jdkTimeTask" class="com.lilin.schedule.JdkTimerTask">
   <property name="userinfoService" ref="userinfoService1"></property>
</bean>
<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <!-- wait 1 seconds before starting repeated execution -->
    <property name="delay" value="1000" />
    <!-- run every 1 seconds -->
    <property name="period" value="10000" />
    <property name="timerTask" ref="jdkTimeTask" />
</bean>  
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
    <property name="scheduledTimerTasks">
        <list>
            <!-- see the example above -->
            <ref bean="scheduledTask" />
             <ref bean="scheduledTask2" />
        </list>
    </property>
</bean>
<!-- jdk timer 2--> 
<bean id="jdkTimeTask2" class="com.lilin.schedule.JdkTimerTask2">
   <property name="userinfoService" ref="userinfoService1"></property>
</bean>
<bean id="doIt2" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
    <property name="targetObject" ref="jdkTimeTask2" />
    <property name="targetMethod" value="doIt" />
</bean>
<bean id="scheduledTask2" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <!-- wait 1 seconds before starting repeated execution -->
    <property name="delay" value="1000" />
    <!-- run every 1 seconds -->
    <property name="period" value="10000" />
    <property name="timerTask" ref="doIt2" />
</bean> 
     
</beans>

web.xml

   <!-- 加载spring begin-->
  <context-param>       
      <param-name>contextConfigLocation</param-name>       
      <param-value>classpath:applicationContext.xml</param-value>   
  </context-param>       
  <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  </listener>  
  <!-- 加载spring end-->

 
jar:
spring.*jar

 com.springsource.org.quartz-1.6.2.jar  //石英的jar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring JAVA