quartz和spring结合后的使用,模仿型

本文详细介绍了如何利用Spring框架中的定时任务特性,结合 Quartz 库,实现自动检查当前日期是否为工作日的功能。通过配置 applicationContext.xml 文件,定义了定时任务 bean 和触发器,确保了每天定时执行工作日检查逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

没有真正理解内核,只是模仿使用。

用来定时检验是否为工作日。

applicationContext.xml

<?

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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx=

"http://www.springframework.org/schema/tx"

xsi:schemaLocation=

"

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"

>

 

<!-- cronTrigger extends QuartzJobBean,MyJob1 -->

<bean id="myJob1" class="org.springframework.scheduling.quartz.JobDetailBean"

>

<property name="jobClass" value="com.apple.sqm.dnwd.detect.delta.Detect"

/>

</bean>

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

>

<property name="jobDetail" ref="myJob1"

/>

<property name="cronExpression" value="0/5 * * * * ?"

/>

</bean

>

<!-- set scheduler -->

<bean id="myScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"

>

<property name="triggers"

>

<list

>

<ref bean="cronTrigger"

/>

</list

>

</property

>

<property name="quartzProperties"

>

<props

>

<prop key="org.quartz.threadPool.threadCount">50</prop

>

<prop key="org.quartz.scheduler.instanceId">AUTO</prop

>

</props

>

</property

>

</bean

>

</

 

beans>

 

 

 

commons-logging.properties

# \u6b64\u914d\u7f6e\u6587\u4ef6\u53ef\u4ee5\u7701\u7565,\u9ed8\u8ba4\u5c31\u662f\u4f7f\u7528\u7684log4j

# \u5fc5\u987b\u8981\u6709commons-logging-1.1.1.jar;

# commons-logging-adapters-1.1.1.jar,commons-logging-api-1.1.1.jar\u4e3a\u53ef\u9009

org.apache.commons.logging.Log=

org.apache.commons.logging.impl.Log4JLogger

 

log4j.rootLogger=

INFO,appender1

# org.springframework\u5305\u4e0b\u9762\u6240\u6709\u7684\u65e5\u5fd7\u8f93\u51fa\u7684\u7ea7\u522b\u8bbe\u4e3aDEBUG

log4j.logger.org.springframework=

INFO

# \u63a7\u5236\u53f0\u8f93\u51fa

log4j.appender.appender1=

org.apache.log4j.ConsoleAppender

log4j.appender.appender1.layout=

org.apache.log4j.PatternLayout

log4j.appender.appender1.layout.ConversionPattern=

%d{yyyy-MM-dd HH:mm:ss:SSS}[%p]:

%m%n

# \u7acb\u5373\u8f93\u51fa

log4j.appender.appender1.immediateFlush=

true

 

 

package

com.apple.sqm.dnwd;

import

org.apache.commons.logging.Log;

import

org.apache.commons.logging.LogFactory;

import

org.springframework.context.support.ClassPathXmlApplicationContext;

 

public

classDNWDServer {

/**

*

@param

args

*/

private static Log log = LogFactory.getLog(DNWDServer.class);

public static void main(String[] args) throws Exception {

log.info("Detect non-working-day start");

new ClassPathXmlApplicationContext("applicationContext.xml");

}

}

package

com.apple.sqm.dnwd.detect;

import

org.apache.commons.logging.Log;

import

org.apache.commons.logging.LogFactory;

import

org.quartz.JobExecutionContext;

import

org.quartz.JobExecutionException;

import

org.springframework.scheduling.quartz.QuartzJobBean;

 

public

abstract class ADetect extends

QuartzJobBean {

Log

log = LogFactory.getLog(ADetect.class

);

protected void executeInternal(JobExecutionContext ctx) throws

JobExecutionException {

extractQuanlity();

}

public abstract void

extractQuanlity();

}

package

com.apple.sqm.dnwd.detect.delta;

import

org.apache.commons.logging.Log;

import

org.apache.commons.logging.LogFactory;

import

com.apple.sqm.dnwd.detect.ADetect;

 

 

public

class Detect extends

ADetect{

Log

log = LogFactory.getLog(Detect.class

);

@Override

public void

extractQuanlity() {

log.info("Hello,in detecting"

);

log.info(Detect.class

.getName());

}

}

转载于:https://www.cnblogs.com/yqskj/articles/2195496.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值