好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

xml+ spring mvc 版本

pom文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

<project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >

  <modelversion> 4.0 . 0 </modelversion>

 

  <groupid>com.adagio</groupid>

  <artifactid>task</artifactid>

  <version> 0.0 . 1 -snapshot</version>

  <packaging>war</packaging>

  <name>task</name>

  <description/>

 

  <properties>

   <project.build.sourceencoding>utf- 8 </project.build.sourceencoding>

   <spring.version> 4.1 . 6 .release</spring.version>

  </properties>

 

  <dependencies>

 

    <dependency>

     <groupid>org.aspectj</groupid>

     <artifactid>aspectjweaver</artifactid>

     <version> 1.8 . 3 </version>

   </dependency>

  

    <dependency>

     <groupid>org.quartz-scheduler</groupid>

     <artifactid>quartz</artifactid>

     <version> 2.2 . 1 </version>

   </dependency>

  

   <dependency>

    <groupid>javax</groupid>

    <artifactid>javaee-api</artifactid>

    <version> 7.0 </version>

    <scope>provided</scope>

   </dependency>

  

   <dependency>

      <groupid>junit</groupid>

      <artifactid>junit</artifactid>

      <version> 4.12 </version>

      <scope>test</scope>

    </dependency>

 

    <!--spring dependency-->

    <dependency>

      <groupid>org.springframework</groupid>

      <artifactid>spring-context</artifactid>

      <version>${spring.version}</version>

    </dependency>

    <dependency>

      <groupid>org.springframework</groupid>

      <artifactid>spring-test</artifactid>

      <version>${spring.version}</version>

    </dependency>

    <dependency>

      <groupid>org.springframework</groupid>

      <artifactid>spring-context-support</artifactid>

      <version>${spring.version}</version>

    </dependency>

    <dependency>

      <groupid>org.springframework</groupid>

      <artifactid>spring-webmvc</artifactid>

      <version>${spring.version}</version>

    </dependency>

    <dependency>

      <groupid>org.springframework</groupid>

      <artifactid>spring-tx</artifactid>

      <version>${spring.version}</version>

    </dependency>

 

 

 

  </dependencies>

  

  <build>

    <finalname>task</finalname>

   <plugins>

    <plugin>

     <artifactid>maven-compiler-plugin</artifactid>

     <version> 2.3 . 2 </version>

     <configuration>

      <source> 1.7 </source>

      <target> 1.7 </target>

     </configuration>

    </plugin>

    <plugin>

         <groupid>org.apache.maven.plugins</groupid>

         <artifactid>maven-war-plugin</artifactid>

         <version> 2.6 </version>

       </plugin>

       <plugin>

         <groupid>org.apache.maven.plugins</groupid>

         <artifactid>maven-surefire-plugin</artifactid>

         <version> 2.18 . 1 </version>

         <configuration>

           <skiptests> true </skiptests>

         </configuration>

       </plugin>

   </plugins>

  </build>

 

</project>

web.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

<?xml version= "1.0" encoding= "utf-8" ?>

<web-app xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns= "http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation= "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id= "webapp_id" version= "3.1" >

  <context-param>

   <param-name>contextconfiglocation</param-name>

   <param-value>

       classpath*:/spring/spring-core.xml

     </param-value>

  </context-param>

  <listener>

   <listener- class >org.springframework.web.context.contextloaderlistener</listener- class >

  </listener>

  <listener>

   <listener- class >org.springframework.web.context.request.requestcontextlistener</listener- class >

  </listener>

  <filter>

   <filter-name>characterencodingfilter</filter-name>

   <filter- class >org.springframework.web.filter.characterencodingfilter</filter- class >

   <init-param>

    <param-name>encoding</param-name>

    <param-value>utf- 8 </param-value>

   </init-param>

  </filter>

  <filter-mapping>

   <filter-name>characterencodingfilter</filter-name>

   <url-pattern>/*</url-pattern>

  </filter-mapping>

  <servlet>

   <servlet-name>dispatcher</servlet-name>

   <servlet- class >org.springframework.web.servlet.dispatcherservlet</servlet- class >

   <init-param>

    <param-name>contextconfiglocation</param-name>

    <param-value>classpath*:/spring/spring-mvc.xml</param-value>

   </init-param>

   <load-on-startup> 1 </load-on-startup>

  </servlet>

  <servlet-mapping>

   <servlet-name>dispatcher</servlet-name>

   <url-pattern>/</url-pattern>

  </servlet-mapping>

 

  <display-name>task</display-name>

  <welcome-file-list>

   <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <welcome-file-list>

   <welcome-file>index.html</welcome-file>

   <welcome-file>index.htm</welcome-file>

   <welcome-file>index.jsp</welcome-file>

   <welcome-file> default .html</welcome-file>

   <welcome-file> default .htm</welcome-file>

   <welcome-file> default .jsp</welcome-file>

  </welcome-file-list>

</web-app>

spring.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<?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"

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

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

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

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

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

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

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

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

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

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

     >

 

  

   <context:component-scan base- package = "com.adagio" >

     <context:exclude-filter type= "annotation" expression= "org.springframework.stereotype.controller" />

   </context:component-scan>

  

   < import resource= "spring-quartz.xml" />

  

</beans>

spring-mvc.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

<?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"

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

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

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

   xsi:schemalocation="http: //www.springframework.org/schema/beans

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

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

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

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

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

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

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

   <!--扫描controller-->

   <context:component-scan base- package = "com.adagio" >

     <context:include-filter type= "annotation" expression= "org.springframework.stereotype.controller" />

   </context:component-scan>

  

   <aop:aspectj-autoproxy proxy-target- class = "true" />

  

   <mvc:annotation-driven>

     <mvc:message-converters>

       <bean class = "org.springframework.http.converter.stringhttpmessageconverter" >

         <constructor-arg name= "defaultcharset" value= "utf-8" />  

         <property name= "supportedmediatypes" >  

           <list>

             <value>text/plain;charset=utf- 8 </value>  

             <value>text/html;charset=utf- 8 </value>  

           </list>  

         </property>  

       </bean>  

     </mvc:message-converters>

   </mvc:annotation-driven>

 

 

   <!--定义视图解析-->

   <bean class = "org.springframework.web.servlet.view.internalresourceviewresolver" >

     <property name= "prefix" value= "/web-inf/views/" />

     <property name= "suffix" value= ".jsp" />

   </bean>

  

   <!--对静态资源的处理-->

   <!-- <mvc: default -servlet-handler /> -->

   <!-- handles http get requests for /resources/** by efficiently serving up static resources in the ${webapproot}/resources directory -->

   <mvc:resources mapping= "/static/**" location= "/static/" />

  

 

</beans>

spring-quartz.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

<?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.xsd" >

  

 

   <!-- ======================== 调度工厂 ======================== -->

   <bean id= "springjobschedulerfactorybean" class = "org.springframework.scheduling.quartz.schedulerfactorybean" >

     <property name= "triggers" >

       <list>

         <!-- 调度触发器 -->

         <ref bean= "hellotrigger" />

       </list>

     </property>

   </bean>

 

    <!-- ======================== 调度触发器 ======================== -->

    <bean id= "hellotrigger" class = "org.springframework.scheduling.quartz.crontriggerfactorybean" >

     <!-- 任务调度类 -->

     <property name= "jobdetail" ref= "hellojob" ></property>

     <property name= "cronexpression" value= "0/5 * * * * ?" ></property>

   </bean>

  

    <!-- 任务调度类 -->

   <bean id= "hellojob" class = "org.springframework.scheduling.quartz.methodinvokingjobdetailfactorybean" >

     <!-- 任务执行类 -->

     <property name= "targetobject" >

       <ref bean= "hellotask" />

     </property>

     <property name= "targetmethod" >

       <value>excute</value><!-- 要执行的方法名称 -->

     </property>

   </bean>

</beans>

hellotask

?

1

2

3

4

5

6

7

8

9

10

11

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

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 与上面一样

启动方式不同

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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的项目的话,还是用的比较少

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://segmentfault.com/a/1190000016976422

查看更多关于详解Quartz 与 Spring框架集成的三种方式的详细内容...

  阅读:13次