定时任务1

 

https://www.cnblogs.com/iceworld520/p/7053939.html

https://blog.youkuaiyun.com/zyb2017/article/details/78997853

定时任务依赖的jar包在哪导入?

工程项目是依赖于gradle依赖管理,所以把需要引用的包列出来。

apply plugin: 'org.springframework.boot'

bootRepackage {
	mainClass = 'batch.StartBatchApplication'
}

processResources.dependsOn copyConfiguration

dependencies {
	compile project(":fps-core")
	compile project(":fps-socket")
	compile project(":fps-channel")
	compile project(":fps-router")
	
	compile 'org.quartz-scheduler:quartz:2.2.1'
    testCompile ('org.springframework:spring-test:4.0.0.RELEASE')
    testCompile ('junit:junit:4.11')
}

第二步:启动StartBatchApplication.java类文件,即是启动服务,因为导入@ImportResource("classpath:config/fps/batch/application-context.xml")这个,在这个xml文件中,即有一些配置文件,在这个配置文件中,就包含很多配置文件,包括数据源的配置,短信的配置,定时任务的配置,都在application-context.xml这个文件中。

package com.zycfc.fps.batch;

import org.apache.catalina.connector.Connector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.util.SocketUtils;


@Configuration
@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
@ImportResource("classpath:config/fps/batch/application-context.xml")
public class StartBatchApplication implements CommandLineRunner {
	
	static final Logger logger = LoggerFactory.getLogger(StartBatchApplication.class);
	
	@Bean
	public Integer port() {
		return SocketUtils.findAvailableTcpPort();    
	}

	@Bean
	public EmbeddedServletContainerFactory servletContainer() {
		TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
		tomcat.addAdditionalTomcatConnectors(createStandardConnector());
		return tomcat;
	}

	private Connector createStandardConnector() {
		Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
		connector.setPort(port());
		return connector;
	}
	
	public static void main(final String[] args) {
		SpringApplication.run(StartBatchApplication.class, args);
	}
	
	@Override
	public final void run(final String... args) throws Exception {
		logger.info("spring boot demo Initialization Succeed.");
		System.out.println(print());
	}

	
	
}

第三步:application-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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
				http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
				http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
				">

	<context:annotation-config />

	<context:component-scan base-package="com.zycfc.fps" />

	<!-- 启动对@AspectJ注解的支持 -->
	<aop:aspectj-autoproxy />
	<!--通知spring使用cglib而不是jdk的来生成代理方法 AOP可以拦截到Controller -->
	<aop:aspectj-autoproxy proxy-target-class="true" />

	<util:properties id="fpsBatchProps" location="classpath:config/fps/batch/config.properties" />

	<bean id="propertiesReader" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="locations">
			<list>
				<value>classpath:config/fps/batch/config.properties</value>
			</list>
		</property>
	</bean>

    <!-- 核心配置dao -->
	<import resource="classpath:config/fps/core/core-context.xml" />
	
	<import resource="classpath:config/fps/batch/spring-quartz.xml" />
	
	
</beans>

第四步:对于spring-quartz.xml文件, name="triggers"意思是org.springframework.scheduling.quartz.SchedulerFactoryBean这个bean文件中,你可以把bean文件看成一个实例化对象,对象中有一些方法,而属性property中的name="triggers" 相当于bean文件中的名字为triggers()方法,实际上Javabean是一种java类,通过封装属性和方法成为具有某种功能的或者处理某个业务的对象,简称bean。list相当于传入参数的值,<ref local="dzTrigger"/>,dzTrigger找到对应id为dzTrigger的bean,在id=dzTrigger这个bean文件中,包含两个方法,分别是:jobDetail()方法,给这个方法传入的参数值是一个对象dzJobDetail, cronExpression()方法,这个方法传入的是一个具体的value值,即是#{fpsBatchProps['ds.fps.quartzSQL']},dzJobDetail又是一个bean文件,在这个id=dzJobDetail的bean文件中,包含几个方法,并且分别给方法传入参数,对于targetObject()方法,传入的参数是dzQuartz这个bean文件,这个就是要具体执行的函数或者定时任务具体执行的任务。

<ref local="xx"/>
  用"local"属性指定目标其实是指向同一文件内对应"id"属性值为此"local"值的索引
"local"属性的值必须和目标bean的id属性相同。如果同一文件内没有匹配的元素,xml解析器将提示错误。同样,如果目标在同一XML文件内,使用"local"变量是最好的选择(为了尽可能早地知道错误)

 

<ref bean="xx"/>
  用"bean"属性指定目标bean是最常规的形式,这允许创建索引到任何同一个容器内的bean(无论是否在同一XML 文件中)或者父级的容器内的bean。"bean"属性的值可以和目标bean的"id"属性相同,也可以和目标bean的"name"属性内的一个值相同

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" 
	default-autowire="no"  >

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref local="dzTrigger"/>
			</list>
		</property>
	</bean>


<bean id="dzTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" scope="prototype">
		<property name="jobDetail">
			<ref bean="dzJobDetail" />
		</property>
		<property name="cronExpression">
			<value>#{fpsBatchProps['quartz.dzTrigger.cronExpression']}</value>
		</property>
	</bean>

	<bean id="dzJobDetail" class="com.suixingpay.common.core.quartz.JobDetailFactoryBean" >
		<property name="jobQuartzDataSource">
			<ref bean="dataSource" />
		</property>
		<property name="quartzSQL" value="#{fpsBatchProps['ds.fps.quartzSQL']}" />
		<property name="targetObject">
			<ref bean="dzQuartz" />
		</property>
		<property name="targetMethod">
			<value>execute</value>
		</property>
		<property name="concurrent">
			<value>false</value>
		</property>
	</bean>







</beans>

第五步:定时任务具体的dzQuartz的类

package batch.quartz;

import java.util.Date;

import org.apache.log4j.MDC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zycfc.fps.batch.service.impl.busi.DzBusiService;
import com.zycfc.fps.batch.service.impl.busi.DzInnerBusiService;
import com.zycfc.fps.core.util.DateUtils;


@Component("dzQuartz")
public class DzQuartz {
	static final Logger logger = LoggerFactory.getLogger(DzQuartz.class);

	@Autowired
	private DzBusiService dzBusiService;

	@Autowired
	private DzInnerBusiService dzInnerBusiService;
	
	
	public void execute() {
		
		Date curDate=DateUtils.addTime("02", -1);
		String dzBatNo=DateUtils.getCurDT()+DateUtils.getCurTM();
		
		try {
			dzBusiService.dz(curDate,dzBatNo);
		}catch(Exception e) {
			logger.error("异常",e);
		}
		logger.info("完成:"+DateUtils.getCurDT()+DateUtils.getCurTM());
		
		
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值