spring定时器使用

一:xml配置方式
1.在spring-applicationContext.xml文件中添加如下代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/task 
       http://www.springframework.org/schema/task/spring-task.xsd">



    <context:component-scan base-package="com.aflyun.web" />
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <context:annotation-config />
    <!-- 在applicationContext.xml中进行配置,使用定时器
        ref : pojo类的名称
        method : 调用的方式名称
        cron : cronExpression表达式
        cron="0/5 * * * * ?"  //表示五秒钟执行一次
     -->
    <task:scheduled-tasks>
        <task:scheduled ref="taskCool" method="testJob" cron="0/5 * * * * ?"/>
    </task:scheduled-tasks>



</beans>


2.编写定时器业务逻辑类
@Component
//@Service 都可以 
public class TaskCool {
    /**
     * 第一个定时器测试方法
     */
    public void testJob(){
        System.out.println("test first taskJob .... ");
    }
}


注:上面主要的配置文件中一定要加入task的命名空间和schema。
上面 ref=”taskCool”,默认为这个TaskCool 类的首字母小写的值,若需要修改可以在@Component里面进行修改 ,例如下面 
@Component(“taskCoolJob”) 则此时 ref=”taskCoolJon”。 
到此基于xml配置完成,运行则可以看到效果!



二:注解方式
1.在spring-applicationContext.xml文件中添加如下代码
<!-- 开启这个配置,spring才能识别@Scheduled注解   -->  
    <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
    <task:scheduler id="qbScheduler" pool-size="10"/>

2.编写定时器业务逻辑类
@Component
public class WebTask {
    // 每五秒执行一次
    @Scheduled(cron = "0/5 * * * * ?")
    public void TaskJob() {
        System.out.println("test second annotation style ...");
    }
}


附:cronExpression的配置说明
字段      允许值     允许的特殊字符
秒       0-59        , - * /
分       0-59        , - * /
小时      0-23        , - * /
日期      1-31        , - * ? / L W C
月份      1-12 或者 JAN-DEC     , - * /
星期      1-7 或者 SUN-SAT      , - * ? / L C #
年(可选)       留空, 1970-2099       , - * /

例子:

CRON表达式         含义 
"0 0 12 * * ?"    每天中午十二点触发 
"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点开始到2点59分每分钟一次触发 
"0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发 
"0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 
"0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发 
"0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发 
"0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring定时器可以使用多线程来执行定时任务,以提高执行效率。具体实现方式如下: 1. 在定时任务方法上加上 @Async 注解,表示该方法是一个异步方法,可以在另一个线程中执行。 2. 在配置类中加上 @EnableAsync 注解,启用 Spring 的异步功能。 3. 在定时任务类中,使用 TaskExecutor 或者 ThreadPoolTaskExecutor 来创建一个线程池,用来执行异步方法。 下面是一个示例代码: ```java @Component public class MyScheduler { @Autowired private TaskExecutor taskExecutor; @Async @Scheduled(cron = "0 0/1 * * * ?") public void doSomething() { taskExecutor.execute(new Runnable() { @Override public void run() { // 执行具体的定时任务代码 } }); } } @Configuration @EnableScheduling @EnableAsync public class MyConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { // 创建线程池 ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(10); executor.setMaxPoolSize(100); executor.setQueueCapacity(1000); executor.setThreadNamePrefix("MyScheduler-"); executor.initialize(); return executor; } } ``` 在上面的代码中,@Async 注解表示 doSomething() 方法是一个异步方法,@Scheduled 注解表示该方法是一个定时任务方法,使用 cron 表达式指定定时任务的执行时间。在 doSomething() 方法中,使用 taskExecutor.execute() 方法来执行具体的定时任务代码,taskExecutor 是在 MyConfig 配置类中创建的线程池。通过这种方式,就可以实现 Spring 定时任务的多线程执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值