- 2023-03-14 14:06:22
- 4634 热度
- 0 评论
定时执行任务,这是项目中常用的东西,今天我们来做一个使用Spring定时器进行任务定制的小例子,仅供学习!
首先要增加相应的JAR。
因为这是一个小例子,使用的JAR包不是很多,用到了spring.jar,quartz-all-1.6.5.jar,quartz-1.5.2.jar,commons-logging.jar,log4j-1.2.14.jar!不用关心版本,从你下载到的Spring中找即可。
定义web.xml配置文件
要在配置文件中定义Spring和Log4j的使用。具体看工程即可。重点关注的是如果你做例子时使用了web-app_2_5.xsd,那么在部分服务器上是跑不起来的。
<?xml version="1.0" encoding="UTF-8"?> <!-- 如果定义的是web-app_2_5.xsd,那么在部分服务器上是跑不通过的 --> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- 系统默认首页面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 定义Spring配置文件的加载路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring.xml</param-value> </context-param> <!-- 定义Log4j日志配置文件 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> <!-- 定义日志监听 --> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <!-- 定义Spring监听 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
定义Spring配置文件
这个文件的位置你可以自己定义,我放到了web-inf下面。里面需要定义四个内容,具体看注释。重点是时间间隔的配置,这个你可以网上查一下,或看readme文件。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 每个定义的任务都要在这里进行引用才能运行 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="BusinessTestTrigger" /> </list> </property> </bean> <!-- 定义我们要运行的类,可以使用注入定制一些参数 --> <bean id="BusinessTestTime" class="com.test.Test"> <property name="para" value="Spring定时器测试V1" /> </bean> <!-- 引用,配置要运行的方法 --> <bean id="BusinessTestDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="BusinessTestTime" /> </property> <property name="concurrent" value="false" /> <property name="targetMethod" value="run" /> </bean> <!-- 引用,定制调用间隔,具体时间配置的正则,请阅读readme.txt --> <bean id="BusinessTestTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="BusinessTestDetail" /> </property> <property name="cronExpression"> <value>0/5 * * * * ?</value> </property> </bean> </beans>
执行的代码
这个代码就是一个普通的代码,里面定义一个要执行的方法就可以了,方法名称自己定义并在Spring配置文件中配置即可。
package com.test; import java.text.SimpleDateFormat; import java.util.Date; /** * 执行类 */ public class Test { // 执行参数 private String para ; // 执行方法 public void run() { // 定义输出的格式化日期,以便于区分调用 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("the para is : " + para + " ! Time is :" + format.format(new Date())); } public String getPara() { return para; } public void setPara(String para) { this.para = para; } }
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Java(82)
- Cloud(82)
- Spring Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- mongoDB(22)
- MVC(22)
- JDBC(22)
- IDEA(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- SpringBoot(17)
- Docker(17)
- Eclipse(16)
- Vue(16)
- Git(16)
- JPA(15)
- Apache(15)
- ORA(15)
- Oracle(14)
- jdk(14)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Pro(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- int(11)
- Myeclipse(11)
- stream(11)
- not(10)
- Bug(10)
- maven(9)
- Map(9)
- Hystrix(9)
- ast(9)
- session(8)
- Window(8)
- Swagger(8)
- APP(8)
- Bit(8)
- API(8)
- Cache(7)
- File(7)
- mail(7)
- IntelliJ(7)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- JavaMail(7)
- Log4J(6)
- pushlet(6)
- apt(6)
- read(6)
- Freemarker(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- nginx(6)
- Server(6)
- ueditor(6)
- jar(6)
- UDP(6)
- ehcache(6)
- RabbitMQ(6)
- star(6)
- and(6)
- Excel(6)
- string(5)
- Syntaxhighlighter(5)
- script(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)
- Servlet(5)
- Config(5)
- Emlog(5)
- discuz(5)