java定时器(项目中的应用)以及ERROR org.springframework.scheduling.support.MethodInvokingRunnable的原因

首先,我使用的是spring自带的定时器,因此,要在spring中配置

<!--添加标头-->
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/context 
        			http://www.springframework.org/schema/context/spring-context-4.2.xsd 
        			http://www.springframework.org/schema/util 
        			http://www.springframework.org/schema/util/spring-util-4.2.xsd
        			http://www.springframework.org/schema/task 
        			http://www.springframework.org/schema/task/spring-task-4.2.xsd"
<!--扫描定时器类所在的包   注意路径,我当时把cn落了-->
	<context:annotation-config/>
	<context:component-scan base-package="cn.com.trueway.xxtx.pojo"/>
	<task:executor id="executor" pool-size="10"/> 
	<task:scheduler id="scheduler" pool-size="10"/>
	<!-- 启用annotation方式 --> 
	<task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
	<!--这个为了配置bean到xml-->
	<bean  id ="applicationContextUtil"  class ="cn.com.trueway.xxtx.pojo.ApplicationContextUtil" ></bean >

配置结束

写定时器的类

package cn.com.trueway.xxtx.pojo;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import cn.com.trueway.xxtx.service.XxtxService;

@Component
@Lazy(false)
public class timetask {
	
	private static Logger log = Logger.getLogger(timetask.class);
	private XxtxService xxtxService;
	//30秒执行一次 每个*号分别对应秒 分 时 天...
	@Scheduled(cron="0/30 * * * * ?")
	public void excute(){
		log.warn("取期限还有一天的任务");
		xxtxService = (XxtxService)ApplicationContextUtil.getBeans("xxtxService");
		//执行service中的各种方法
	}
}

由于定时器的注入优先级高于@Resource 和@Autowire
因此要换个注入方式
新建ApplicationContextUtil类

package cn.com.trueway.xxtx.pojo;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextUtil implements ApplicationContextAware {
	private static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
    public static ApplicationContext getApplicationContext() {
        return context;
    }
    public static Object getBeans(String name) {
        return getApplicationContext().getBean(name);
    }
}

注意service上要有个注解

@Service("xxtxService")//标明service的名字,方便定时器的类中getbeans取
public class XxtxServiceImpl implements XxtxService {

亲测有效

至于
ERROR org.springframework.scheduling.support.MethodInvokingRunnable的原因
就是由于注入优先级的原因,找不到类,用上面的方法就能解决

org.springframework.scheduling.support.taskutils.LoggingErrorHandler.handleException方法是Spring框架用于处理任务调度过程出现的异常的一个实用工具类。在任务调度执行的过程,如果出现了异常,这个方法会被调用来处理这个异常。 该方法接收一个Throwable类型的参数,并会将异常信息以日志的形式记录下来。它首先判断异常是否为Error类型,如果是的话,会将异常信息输出到日志,并返回。如果异常不是Error类型,则会判断异常的Severity级别,如果异常为FATAL级别,则会将异常信息输出到日志,并返回。如果异常不是FATAL级别,则会将异常信息输出到日志,并继续执行其他操作。 日志的输出位置由logError方法指定,默认情况下,日志的输出级别为Error或FATAL。如果需要设置自定义的输出位置或输出级别,可以通过覆盖logError方法来实现。 该方法由Spring框架提供,主要用于捕获和处理任务调度过程发生的异常,保证任务调度的可靠性和稳定性。通过将异常信息记录到日志,可以方便地进行排查和分析,帮助开发人员快速定位问题并进行修复。 总结来说,org.springframework.scheduling.support.taskutils.LoggingErrorHandler.handleException方法是Spring框架用于处理任务调度异常的一个工具类方法,通过记录异常信息到日志,提高了任务调度过程的可靠性和稳定性,便于开发人员进行问题排查和修复。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值