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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值