Gunakan pegas untuk mengintegrasikan kuarsa untuk mengimplementasikan timer (proyek Maven untuk demonstrasi)
Metode yang tidak didasarkan pada kelas dasar tertentu
1. Paket Lingkungan Pengembangan dan Jar Ketergantungan
Musim Semi 4.2.6.Release
Maven 3.3.9
JDK 1.7
Ide 15.04
2. Ketergantungan JAR yang sangat diperlukan (ditambahkan dalam file POM.XML dalam proyek Maven)
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency>
3. File yang digunakan saat mengimplementasikan timer:
PLANWORKEXCUTE.JAVA - Kelas yang dieksekusi dengan timer
spring-plan.xml --xml untuk mengonfigurasi informasi timer
Keempat, terapkan langkah timer:
1. Buat file planworkexcute.java, di bawah paket cc.royao.plantask.
Paket cc.royao.plantask; impor java.text.simpledateFormat; impor java.util.date; import java.util.hashmap; org.apache.log4j.logger; // Anda dapat menghapus impor org.springframework. ditarikouditkask () {SimpleDateFormat outformat = new SimpleDateFormat ("yyyyy mm bulan ddd tanggal hh: mm: ss"); System.out.println("Start withdrawal exemption task------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Logger.info ("Mulai Penarikan dan Pengecualian dari Tinjauan tugas---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2. Buat file konfigurasi spring-plan.xml Catatan: Cukup buat file konfigurasi timer. Jika beberapa timer diperlukan, cukup tambahkan kacang di spring-plan.xml dan tentukan metode kelas timer. Tidak perlu membuat beberapa XML.
・ Untuk ekspresi cron berapa lama timer akan dieksekusi, silakan merujuk ke: http://www.vevb.com/article/138900.htm
・ URL untuk menghasilkan ekspresi online: http://cron.qqe2.com/
<? 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:/wwork.wwork.or http://www.springframework.org/schema/beans/spring-weans-2.5.xsd"default-lazy-init="false"> Id Id = "job1"/> <!-memodifikasi jalan ke kelas waktu Anda-> <!-Anda dapat membuat banyak bean timing iD-<! bean = "job1"/> </propt> <name properti = "targetMethod"> <value> ditarikkanouditkask </value> <!-nama metode dari kelas timer-> </property> </ bean> <bean id = "crontrigger_1"> <povact name = "jobdetail"> <bean bean = "crontrigger_1"> <povact name = "jobdetail"> <ref bean = "jobdetazer_"> <povage name = "jobdetail"> <ref ref bean = "jobdetazer_1"> <povact name = "JobDetail"> <Ref bean = "jobdetail <" Properti "lereng/" name = "cronexpression"> <value> 0/2 * * * *? </value> <!- 0 10 0 * *? Jalankan pada 0:10 setiap hari-> </prup Property> </ bean> <bean> <name properti = "triggers"> <cist> <ref local = "crontrigger_1"/> <!-setiap pengatur waktu ditambahkan, Anda juga harus menambahkan-> </list> </propert> </bean> </bean>
3. Anda perlu memperkenalkan spring-plan.xml di ApplicationContext.xml. Kode berikut berfokus pada garis bawah
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls xmlns: jee = "http://www.springframework.org/schema/jee" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: context = "http:/schaMa/tx. xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: cache = "http:/schema.mvc" xmlns: cache = "http:caMAMAMAMAMLE" xmlns: cache = "http: XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http:/wwww.springfram http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/cache http://www.spramework.org/schema http://www.springframework.org/schema/cache/spring-cache-3.2.xsd "default-lazy-init =" true "> <!-memuat konfigurasi file properti sistem-<list> <bean id =" properti-configurer "> <nama properti =" lokasi "> <list> <value>! <value> web-inf/sms.properties </ value>-> </list> </propert> </ bean> <bean id = "datasource"> <name properti = "driverclassname"> <value> $ {jdbc.driverclass} </value> </properti> <!-<nama properti = "jdbc." <value> jdbc: mysql: //192.168.14.239: 3306/tes? UseUnicode = true & characterencoding = UTF-8 </value> </sovore> <Properti Nama = "Nama Pengguna"> <value> $ {jdbc.username} </value> </properti> Properti> <value = "JDBC.username} </value> </properti> Properti> <value =" datai = " <value>${jdbc.password}</value> </property> <property name="maxActive"> <value>20</value> </property> <property name="maxIdle"> <value>60</value> </property> <property name="maxWait"> <value>20000</value> <!-- 0 --> </property> <property name="removeAbandoned"> <value>true</value> </propt> <name properti = "RemoveAbandOnDtimeout"> <value> 60000000 </value> <!-180-> </property> <!-add-> <name properti = "validationQuery" value = "pilih 1"> </properti nama properti = "testwhileIdle" value = "true"> </property name = "Properti =" name="testOnBorrow" value="true"></property> <property name="timeBetweenEvictionRunsMillis" value="3600000"></property> <property name="numTestsPerEvictionRun" value="50"></property> <property name="minEvictableIdleTimeMillis" value="120000"></property> <!-- add --> </bean> <!-- SqlSessionFactory --> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="threadPoolTaskExecutor"> <property name="corePoolSize" value="1"/> <property name="maxPoolSize" value="10"/> <property name="keepAliveSeconds" value = "300"/> <name properti = "queuecapacity" value = "50"/> <name properti = "waitfortaskstocompleteonshutdown" value = "true"/> </ bean> <bean id = "transactionManager"> <Properti Nama = "DataSource" Ref = "DataSource"> </Properti <//BEANT <!-: BEANT!-<! Base-package = "com.royao">-> <!-<konteks: termasuk filter type = "regex"-> <!-ekspresi = "com.royao.services.*" />-> <!-< /component-scan>-> <aop: config proxy-target-class = "true"> <aop: pointc: pointcon "pointcut (pointcut" ID "ID" ID "ID" ID "ID" ID "ID" ID "ID" ID "ID" ID "ID" ID "ID" ID "ID" ID "ID LAGER-TARGET; CC.*LayananImpl.*(..)) "/> <aop: penasihat pointcut-ref =" serviceoperation "saran-ref =" txadvice "/> </aop: config> <!-configure transaction notification-> <tx: advent id =" txadvice "txadvice" transaction " <TX: Metode Name = "*" Rollback-for = "Exception"/> </tx: Atribut> </tx: Saran> <tx: nasihat id = "transactionAmagerAdivice" transaction-manager = "transactionManager"> <tx: attributes> <tx: nama metode = "*insert*" propagation "="///</Atributes> <tx: Metode nama = "*Sisipkan*" propagation = "wajib" <tx: Metode name = "*perbarui*" propagasi = "diperlukan"/> <tx: nama metode = "*perbarui*" propagasi = "diperlukan"/> <tx: nama metode = "*del*" propagasi = "wajib"/> <tx: metode = "doapproved" propagation = "wajib"/<tx: tx: Batch = "Batpred =" propagation " name = "Edittemplate" propagation = "wajib"/> <tx: Metode name = "dummyDelete" propagation = "wajib"/> <tx: nama metode = "BatchDeluser" propagasi = "Diperlukan"/> <!-<tx: Metode nama = "*" TRANEGASI = "diperlukan"/>-> </tx: attribute = "*" TREEGRATION = "diperlukan"/>-> </tx: attrib: "tx:"*"tx:" tx: "tx:" tx: "tx:" tx: " name = "BasePackage"> <value> cc.royao.mana.Auth.mapper.*</value> </prop Property = "SQLSessionFactoryBeanName" value = "SQLSessionFactory"/> </toming here, pointere, "pointer sumber, it-> <import resource = "spring-plan.xml"/> </boy>Meringkaskan
Di atas adalah apa yang diperkenalkan editor kepada Anda menggunakan Spring Integrated Quartz untuk mengimplementasikan fungsi timer. Saya harap ini akan membantu semua orang. Jika Anda memiliki pertanyaan, silakan tinggalkan saya pesan dan editor akan membalas semua orang tepat waktu. Terima kasih banyak atas dukungan Anda ke situs web Wulin.com!