There is a problem in my friend’s project. His side is based on Spring architecture and has a relatively simple task that requires scheduled execution. After understanding his needs, he proposed a relatively simple Spring+quartz implementation.
Note that this article only discusses how to complete the simplest scheduled tasks under the completed Spring project.
The first step is to know the Spring architecture, which is very interesting and interesting. The effect of freely plugging and unplugging functional modules can be achieved. The project is based on MAVEN package dependency management, so list the required dependency package references this time:
1 <!-- 定时器依赖 开始 --> 2 <dependency> 3 <groupid>org.springframework</groupid> 4 <artifactid>spring-context-support</artifactid> 5 <version>4.0.2.RELEASE</version> 6 </dependency> 7 8 9 10 <dependency>11 <groupid>org.quartz-scheduler</groupid>12 <artifactid>quartz</artifactid>13 <version>2.2.1</version>14 </dependency>15 <!-- 定时器依赖 结束 -->
Of course, this must match the corresponding Spring version. . Our project here is 4.0.2. The previous package spring-context-support is mainly used as a component for communication and management between Spring and quartz. If it is commented out, an error like this will be reported
Configure in MAVEN After completing the packages that need to be added (other packages will not be discussed here for the time being, this article only discusses the configuration under the complete Spring project), we can start to add functional modules for scheduled tasks to this project.
The second step is to start from the origin of the web project and make changes in web.xml. Since the Spring configuration file of the original project was Spring-mvc.xml, I changed the configuration file of the scheduled task to spring-time.xml. In this way, the same scan configuration can be read at startup. The specific code is as follows:
1 <context-param>2 <param-name>contextConfigLocation</param-name>3 <param-value>classpath:spring-*.xml</param-value>4 </context-param>
Then to show you my engineering structure:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans> 7 8 <!-- 启动触发器的配置开始 --> 9 <bean> 11 <property> 12 <list> 13 <ref></ref> 14 </list> 15 </property> 16 </bean> 17 <!-- 启动触发器的配置结束 --> 18 19 <!-- 调度的配置开始 --> 20 <!-- 21 quartz-1.8以前的配置 22 <bean id="myJobTrigger" 23 class="org.springframework.scheduling.quartz.CronTriggerBean"> 24 <property name="jobDetail"> 25 <ref bean="myJobDetail" /> 26 </property> 27 <property name="cronExpression"> 28 <value>0/1 * * * * ?</value> 29 </property> 30 </bean> 31 --> 32 <!-- quartz-2.x的配置 --> 33 <bean> 35 <property> 36 <ref></ref> 37 </property> 38 <property> 39 40 <value>0/10 * * * * ?</value>41 <!-- <value>1 52 * * * ?</value> --> 42 </property> 43 </bean> 44 <!-- 调度的配置结束 --> 45 46 <!-- job的配置开始 --> 47 <bean> 49 <property> 50 <ref></ref> 51 </property> 52 <property> 53 <value>work</value> 54 </property> 55 </bean> 56 <!-- job的配置结束 --> 57 58 <!-- 工作的bean --> 59 <bean></bean> 60 61 </beans>After completing the configuration file configuration here, you can start the next step, specifically The business logic is implemented;The fourth step is to implement the specific business logic. What should be noted here is the two points in the picture below
##You can see here that the scheduled task is executed every 10 seconds according to the
There will be two articles next, one is a simpler implementation method of writing scheduled tasks, and the other explains cron expressions.
The above is the detailed content of Example of implementation of Spring+quartz. For more information, please follow other related articles on the PHP Chinese website!

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

JavaisnotentirelyplatformindependentduetoJVMvariationsandnativecodeintegration,butitlargelyupholdsitsWORApromise.1)JavacompilestobytecoderunbytheJVM,allowingcross-platformexecution.2)However,eachplatformrequiresaspecificJVM,anddifferencesinJVMimpleme

TheJavaVirtualMachine(JVM)isanabstractcomputingmachinecrucialforJavaexecutionasitrunsJavabytecode,enablingthe"writeonce,runanywhere"capability.TheJVM'skeycomponentsinclude:1)ClassLoader,whichloads,links,andinitializesclasses;2)RuntimeDataAr

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

The five major features of Java are polymorphism, Lambda expressions, StreamsAPI, generics and exception handling. 1. Polymorphism allows objects of different classes to be used as objects of common base classes. 2. Lambda expressions make the code more concise, especially suitable for handling collections and streams. 3.StreamsAPI efficiently processes large data sets and supports declarative operations. 4. Generics provide type safety and reusability, and type errors are caught during compilation. 5. Exception handling helps handle errors elegantly and write reliable software.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
