SpringBoot定时任务xml配置

    由于定时任务需要工程实施人员进行配置,写在Java代码里面不利于维护,故采用SpringBoot集成Spring XML文件进行配置,属于新瓶装旧酒。

1 新建定时任务类

package com.duhongming.springbootscheduled.scheduled;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DataScheduledTask {

    private Integer i;
    private Double d;
    private String s;
    private Boolean b;

    public DataScheduledTask(Integer i,Double d,String s,Boolean b){
        super();
        this.i = i;
        this.d = d;
        this.b = b;
        this.s = s;
    }

    /**
     * xml方式
     */
    public void springContextXmlexecute(){
        log.info("i:{},d:{},b:{},s:{}",i,d,b,s);
    }

    /**
     * 注解方式
     */
    @Scheduled(cron = "${config.cron}" )
    public void scheduled() {
        log.info("scheduled");
    }

}

2 在resources下面配置spring-context.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:task="http://www.springframework.org/schema/task"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"
	   default-lazy-init="true">

	<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">          
	    <!-- 核心线程数,默认为1 -->          
	    <property name="corePoolSize" value="10" />          
	    <!-- 最大线程数,默认为Integer.MAX_VALUE -->          
	    <property name="maxPoolSize" value="50" />          
	    <!-- 队列最大长度,一般需要设置值>=notifyScheduledMainExecutor.maxNum;默认为Integer.MAX_VALUE -->          
	    <property name="queueCapacity" value="1000" />          
	    <!-- 线程池维护线程所允许的空闲时间,默认为60s -->          
	    <property name="keepAliveSeconds" value="300" />          
	    <!-- 线程池对拒绝任务(无线程可用)的处理策略,目前只支持AbortPolicy、CallerRunsPolicy;默认为后者 -->          
	    <property name="rejectedExecutionHandler">          
	        <!-- AbortPolicy:直接抛出java.util.concurrent.RejectedExecutionException异常 -->          
	        <!-- CallerRunsPolicy:主线程直接执行该任务,执行完之后尝试添加下一个任务到线程池中,可以有效降低向线程池内添加任务的速度 -->          
	        <!-- DiscardOldestPolicy:抛弃旧的任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->          
	        <!-- DiscardPolicy:抛弃当前任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->          
	        <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />          
	    </property>          
	</bean> 
	
	<!-- 计划任务配置,用 @Service @Lazy(false)标注类,用@Scheduled(cron = "0 0 2 * * ?")标注方法 -->
    <task:scheduler id="scheduler" pool-size="24" />
    
    <task:annotation-driven scheduler="scheduler" executor="taskExecutor" proxy-target-class="true"/>
    
    <!-- * * * * * ? 测试专用:一秒一次-->
    <task:scheduled-tasks>      
		<task:scheduled ref="dataScheduledTask" method="springContextXmlexecute" cron="0/1 * * * * ?" /> <!-- 一秒钟一次 -->
    </task:scheduled-tasks>

	<!-- DataScheduledTask注入 -->
	<bean id="dataScheduledTask" class="com.duhongming.springbootscheduled.scheduled.DataScheduledTask">
		<constructor-arg name="i" value="1" /><!-- Integer -->
		<constructor-arg name="d" value="1.01" /><!-- Double -->
		<constructor-arg name="s" value="字符串"/><!-- String -->
		<constructor-arg name="b" value="true" /><!-- Boolean -->
	</bean>
</beans>

3 在resources下面配置application.properties

#在jar内
spring.context.xml.path=classpath:spring-context.xml
#在jar外
#spring.context.xml.path=file:spring-context.xml

#注解方式配置参数
config.cron=0/5 * * * * ?

4 新建一个包config,以及一个类ConfigClass

package com.duhongming.springbootscheduled.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource(locations={"${spring.context.xml.path}"})
public class ConfigClass {}

5 整体结构如下

转载于:https://my.oschina.net/duhongming52java/blog/1787872

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,我们可以通过在application.properties或application.yml文件中配置定时任务。 首先,我们需要在依赖中引入Spring Boot的定时任务支持: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> ``` 接下来,在application.properties文件中添加以下配置: ```properties # 开启定时任务 spring.quartz.enabled=true # 调度器名称 spring.quartz.properties.org.quartz.scheduler.instanceName=quartzScheduler # 调度器ID spring.quartz.properties.org.quartz.scheduler.instanceId=AUTO # 是否自动启动 spring.quartz.properties.org.quartz.scheduler.startupDelay=0 # 调度器实现类 spring.quartz.properties.org.quartz.scheduler.instanceClass=org.quartz.impl.StdSchedulerFactory # JobStore实现类 spring.quartz.properties.org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX # 数据库表前缀 spring.quartz.properties.org.quartz.jobStore.tablePrefix=QRTZ_ # 是否启用集群 spring.quartz.properties.org.quartz.jobStore.isClustered=true # 数据库连接池最大连接数 spring.quartz.properties.org.quartz.jobStore.maxConnections=10 ``` 以上是一些Quartz的配置,其中org.quartz.jobStore.tablePrefix是Quartz在数据库中创建表的前缀,可以根据需要修改。 接下来,在应用程序中定义定时任务类,并使用@Scheduled注解指定定时任务的执行时间: ```java @Component public class MyJob { @Scheduled(cron = "0 0 0 * * ?") public void doSomething() { // 执行定时任务的代码 } } ``` 在上面的代码中,我们使用@Scheduled注解指定定时任务的执行时间。cron表达式表示该定时任务每天0点执行一次。 最后,启动Spring Boot应用程序,定时任务将按照我们的配置自动执行。 注意:如果要在application.yml中进行配置,请参考Spring Boot官方文档的配置方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值