spring中的定时器用法

在java实际开发过程中,会经常需要通过定时处理一些问题,比如在每天的0点更新数据库之类的操作。

到目前的工作为止,写过了两种简单的定时器。由于功底不是很厚实,底层在这里就不进行探究,这里主要写出这两种方法以备后用。


1.框架结构为ssm

在spring配置文件applicationContent.xml中添加以下内容

1) xml表头中添加task命名空间

xmlns:task="http://www.springframework.org/schema/task"

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"
 

2)设置自动扫描,通过注解来扫描相应的的bean

  <!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。  -->
	<context:component-scan base-package="com.jeeplus"><!-- base-package 如果多个,用“,”分隔 -->
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

3)配置计划任务
    <!-- 计划任务配置,用 @Service @Lazy(false)标注类,用@Scheduled(cron = "0 0 2 * * ?")标注方法 -->
    <task:executor id="executor" pool-size="10"/> <task:scheduler id="scheduler" pool-size="10"/>
    <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>

4)java建立对应的方法
@Controller
public class TimingUtil {
	
	@Autowired
	private ZjkyBcOrderService zjkyBcOrderService;
	
	@Scheduled(cron="0 0 0 * * ?")   //每天零点执行     
	public void shenheTiming(){
           System.out.println("定时");
        }
}
注意,这里的方法不能有返回值。


2.框架结构为stuts,spring,ibatis

在spring配置文件applicationContent.xml中添加以下内容

<!-- ***********Quartz 作业调度************** -->

<!-- 要调用的工作类,指向所要调用的action -->

<bean id="QuartzJob" class="com.ctfo.module.Traffic.action.TrafficAction"></bean>

    <!-- 定义调用对象和调用对象的方法 -->

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

         <!-- 调用的类 -->
          <property name="targetObject">

                  <!-- ref标签中为bean的id,指向某个class -->

                   <ref bean="QuartzJob"/>

            </property>

            <!-- 调用类中的方法 -->

            <property name="targetMethod">

                <!-- 指向方法名 -->

                <value>faSongXiaoXi</value>

            </property>

   </bean>

       <!-- 定义触发时间 -->

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

            <property name="jobDetail">

                <!-- 指向所要执行的触发器-->

                <ref bean="jobtask1"/>
            </property>

            <!-- cron表达式 -->

            <property name="cronExpression">

                <value>0/30 * * * * ?</value>

            </property>

        </bean>

       <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->

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

            <property name="triggers">

                <list>

                    <ref bean="doTime1"/>

                </list>

            </property>

        </bean>







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架,有多种方式可以实现定时任务调度。以下是Spring内置的定时器实现方式: 1. 使用`@Scheduled`注解:在需要执行定时任务的方法上添加`@Scheduled`注解,并配置相应的定时任务表达式。例如: ```java import org.springframework.scheduling.annotation.Scheduled; public class MyScheduledTask { @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨执行 public void myTask() { // 执行定时任务的逻辑 } } ``` 2. 实现`SchedulingConfigurer`接口:创建一个类实现`SchedulingConfigurer`接口,并重写`configureTasks`方法,用于配置定时任务。例如: ```java import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; @Configuration @EnableScheduling public class MyTaskConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { // 配置定时任务 taskRegistrar.addCronTask(() -> { // 执行定时任务的逻辑 }, "0 0 0 * * ?"); // 每天凌晨执行 } } ``` 3. 使用XML配置:在Spring的XML配置文件使用`<task:scheduled>`标签来配置定时任务。例如: ```xml <beans xmlns:task="http://www.springframework.org/schema/task"> <task:scheduler id="myScheduler" pool-size="10" /> <task:scheduled ref="myTaskBean" method="myTask" cron="0 0 0 * * ?" /> <bean id="myTaskBean" class="com.example.MyTaskBean" /> </beans> ``` 这些是Spring内置的一些定时任务调度的实现方式,根据项目的需求和个人偏好,选择适合的方式来实现定时任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值