spring task

Posted tonggc1668

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring task相关的知识,希望对你有一定的参考价值。

quartz采用多线程,定时任务之间不受影响。spring task采用单线程,定时任务之间会相互阻塞,完成一个任务才会执行下一个。

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">

    <mvc:annotation-driven />     
    <tx:annotation-driven transaction-manager="transactionManager" /> 
    <task:annotation-driven/>  
    
    <bean id="inbound_call_finish_task" class="com.yd.iwmsc.caller.logic.IWMSC_busi_inboundCaller" />
    <bean id="outbound_call_finish_task" class="com.yd.iwmsc.caller.logic.IWMSC_busi_outboundCaller" />
    <task:scheduled-tasks>  
        <task:scheduled ref="inbound_call_finish_task" method="call_finish" cron="0 0/3 * * * ?" />  
        <task:scheduled ref="outbound_call_finish_task" method="call_finish" cron="0 0/3 * * * ?"/>  
    </task:scheduled-tasks>
    
    <!--  加载配置文件-->
   <!--  <context:property-placeholder location="classpath:config.properties"/> -->
    
    <!--  JNDI数据源-->    
     <jee:jndi-lookup id="dataSource" jndi-name="jndi_wmsce" resource-ref="true"/>

    <!-- SPRING JDBC -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
        <constructor-arg ref="dataSource"/> 
    </bean>
    
    <!-- SPRING 事务管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>    
    
    <!--  应用上线文初始化Bean-->
    <!-- <bean class="com.yd.common.runtime.CIPRuntimeContextInitializer" />  -->
    
    <!-- dao,service,controller,component scan -->
    <context:component-scan base-package="com.yd.iwmsc"  />
    <context:component-scan base-package="com.yd.wmsc"  />
    <context:component-scan base-package="com.yd.common.function" />
    <context:component-scan base-package="com.yd.common.auth" />
</beans>

或者用@Scheduled注解

package com.yd.wmsc.util;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class TestAop2 {
    
    @Scheduled(cron="0 0/1 * * * ?")
    public void test() {
        System.out.println("定时器2开始");
    }

}

 



以上是关于spring task的主要内容,如果未能解决你的问题,请参考以下文章

Spring全家桶笔记:Spring+Spring Boot+Spring Cloud+Spring MVC

学习笔记——Spring简介;Spring搭建步骤;Spring的特性;Spring中getBean三种方式;Spring中的标签

Spring--Spring入门

Spring框架--Spring事务管理和Spring事务传播行为

Spring框架--Spring事务管理和Spring事务传播行为

Spring框架--Spring JDBC