详解Quartz 与 Spring框架集成的三种方式

网友投稿 308 2023-01-19


详解Quartz 与 Spring框架集成的三种方式

XML+ Spring MVC 版本

POM文件

4.0.0

com.adagio

task

0.0.1-SNAPSHOT

war

task

UTF-8

4.1.6.RELEASE

org.aspectj

aspectjweaver

1.8.3

org.quartz-scheduler

quartz

2.2.1

javax

javaee-api

7.0

provided

junit

junit

4.12

test

org.springframework

spring-context

${spring.version}

org.springframework

spring-test

${spring.version}

org.springframework

spring-context-support

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-tx

${spring.version}

task

maven-compiler-plugin

2.3.2

1.7

1.7

org.apache.maven.plugins

maven-war-plugin

2.6

org.apache.maven.plugins

maven-surefire-plugin

2.18.1

true

web.xml

contextConfigLocation

classpath*:/spring/spring-core.xml

org.springframework.web.context.ContextLoaderListener

org.springframework.web.context.request.RequestContextListener

CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

CharacterEncodingFilter

/*

dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

1

dispatcher

/

task

index.jsp

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

spring.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:task="http://springframework.org/schema/task"

xsi:schemaLocation="http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.1.xsd

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.1.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.1.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.1.xsd

http://springframework.org/schema/task

http://springframework.org/schema/task/spring-task-4.0.xsd "

>

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:task="http://springframework.org/schema/task"

xsi:schemaLocation="http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.1.xsd

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.1.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.1.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.1.xsd

http://springframework.org/schema/task

http://springframework.org/schema/task/spring-task-4.0.xsd "

>

spring-mvc.xml

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-4.1.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.1.xsd">

text/plain;charset=UTF-8

text/html;charset=UTF-8

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-4.1.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.1.xsd">

text/plain;charset=UTF-8

text/html;charset=UTF-8

spring-quartz.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

excute

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

excute

HelloTask

package com.adagio;

import org.springframework.stereotype.Component;

@Component

public class HelloTask {

public void excute() {

System.out.println("excute...22222>>>>>>>>");

}

}

configuration + Spring MVC

和上面类似,只是 spring-quartz.xml 转成configuration

package com.adagio;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.scheduling.quartz.CronTriggerFactoryBean;

import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;

import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration

public class QuartzConfig {

@Bean

public SchedulerFactoryBean factoryBean() {

SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();

factoryBean.setTriggers(cronTriggerFactoryBean().getObject());

return factoryBean;

}

@Bean

public CronTriggerFactoryBean cronTriggerFactoryBean() {

CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();

cronTriggerFactoryBean.setJobDetail(methodInvokingJobDetailFactoryBean().getObject());

cronTriggerFactoryBean.setCronExpression("0/5 * * * * ?");

return cronTriggerFactoryBean;

}

@Bean

public MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean() {

MethodInvokingJobDetailFactoryBean m = new MethodInvokingJobDetailFactoryBean();

m.setTargetObject(helloTask());

m.setTargetMethod("excute");

return m;

}

@Bean

public HelloTask helloTask() {

return new HelloTask();

}

}

Configuration + Spring Boot

QuartzConfig 和 HelloTask 与上面一样

启动方式不同

package com.adagio;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages= {"com.adagio"})

@SpringBootApplication

public class TimerTaskApplication {

public static void main(String[] args) {

SpringApplication.run(TimerTaskApplication.class, args);

}

}

总结

对xml的方式比较熟悉,所有先配置好xml

confugration如果不是Spring Boot的项目的话,还是用的比较少


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springBoot整合RocketMQ及坑的示例代码
下一篇:Spring Boot系列教程之死信队列详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~