Use Spring para integrar cuarzo para implementar el temporizador (Proyecto Maven para la demostración)
Métodos que no se basan en clases base específicas
1. Entorno de desarrollo y paquetes de jarra de dependencia
Primavera 4.2.6.
Maven 3.3.9
JDK 1.7
Idea 15.04
2. Una dependencia de JAR indispensable (agregada en el archivo pom.xml en el proyecto Maven)
<Spendency> <MoupRoMID> org.springframework </groupid> <artifactid> spring-Context-support </artifactid> <versever> 4.2.6.release </versión> </pendency> <pendency> <proupid> org.quartz-scheduler </groupid> <artifactid> cuartz </artifactid> <lother> <Versión> 2.12.
3. Archivos utilizados al implementar el temporizador:
planworkexcute.java - clase ejecutada por temporizador
spring-plan.xml --xml para configurar la información del temporizador
Cuarto, implementa los pasos del temporizador:
1. Cree el archivo PlanWorkExCute.java, debajo del paquete CC.Royao.Plantask.
package cc.royao.plantask;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import org.apache.log4j.logger; // puede eliminar importar org.springframework.beans.factory.annotation.autewired; public class PlanWorKexeCute {logger logger = logger.getLogger (this.getClasssssss ()); // logger imprime el log, y puede eliminar/*** método de tiempo de tiempo*/public retrawhouditTask () {SimpleDateFormat outFormat = new SimpleDateFormat ("aaa yyyyy mm mes ddd fecha hh: mm: ss"); System.out.println("Start withdrawal exemption task------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- logger.info ("Iniciar retiro y exención de la revisión tarea---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------2. Cree el archivo de configuración Spring-Plan.xml Nota: Simplemente cree un archivo de configuración del temporizador. Si se necesitan varios temporizadores, simplemente agregue frijoles en Spring-Plan.xml y defina el método de clase de temporizador. No hay necesidad de crear múltiples XML.
・ Para la expresión cron de cuánto tiempo se ejecutará el temporizador, consulte: http://www.vevb.com/article/138900.htm
・ La URL para generar expresiones en línea: http://cron.qqe2.com/
<? xml versión = "1.0" encoding = "utf-8"?> <frijoles xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: esquemalation http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"default lazy-init="false"><bean id = "job1"/> <! Modifique la ruta a su clase de tiempo-> <! Puede crear múltiples beates de tiempo múltiples-> <bean id = "JobdetaT_1" <name de propiedad " <ref bean = "Job1"/> </Property> <Property Name = "TargetMethod"> <alon> RetirarauditTask </value> <!-Nombre del método de la clase de temporizador-> </Property> </bean> <bean id = "Crontrigger_1"> <Property Name = "JobDetAil"> <Refan = "JobDetail_1"/> <!-Esto corresponde a The Open name = "cronexpression"> <valor> 0/2 * * * *? </value> <!- 0 10 0 * *? Ejecutar a las 0:10 todos los días-> </property> </reme> <bean> <propiedad name = "desgarradoros"> <list> <ref local = "crontrigger_1"/> <!-Cada temporizador agregado, también debe agregar-> </list> </property> </bean> </beans>
3. Necesita introducir spring-plan.xml en applicationContext.xml. El siguiente código se centra en el resultado final
<? xml versión = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" "" "" xmlns: jee = "http://www.springframework.org/schema/Jee" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: context = "http://www.springframework.org/schema/schemt" " xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: cache = "http://wwww.springframe xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springingwork.org/schema/context/spring-contex 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.springframework.org/ http://www.springframework.org/schema/cache/spring-cache-3.2.xsd "predeterminado-lazy-init =" true "> <!-Cargando la configuración del archivo de propiedades del sistema-> <bean id =" PropertyConfigurer "> <Nombre de propiedad =" ubicaciones "> <List> <valor> webinf/jdbc.properties </value> <! <value> web-inf/sms.properties </value>-> </list> </property> </bean> <bean id = "dataSource"> <Property name = "DriverClassName"> <valor> $ {jdbc.driverClass} </valor> </sperties> <!-<Property name = "DefaultAutOcommit" Value = "false"/>-> <name de propiedad = "iba" url " <value> jdbc: mysql: //192.168.14.239: 3306/test? useUnicode = true y caracteresCoding = utf-8 </alon valor> </propertion> <Property name = "username"> <value> $ {jdbc.userneN </propiety> <Property name = "maxactive"> <value> 20 </value> </propiety> <Property name = "maxidle"> <al valor> 60 </valor> </propine> <propiedad name = "maxwait"> <valor> 20000 </valor> <!-0-> </propiedad> <propers name = "removiabaBaBaBaBaB name = "removeAbaBaBaLedTimeOut"> <valor> 60000000 </value> <!-180-> </sperty> <!-add-> <Property name = "ValidationQuery" valor = "seleccionar 1"> </property> <Property name = "testIlleIdle" value = "true"> </property> <name de propiedad = "testonborrow" valor = "verdadero"> </</name de propiedad "n. value = "true"> </propiety> <Property name = "TimeBetweeVictionRunSmillis" value = "3600000"> </propiety> <Property name = "numTestSpereVictionRun" value = "50"> </sperty> <Property name = "minevictableImemillis" value = "120000"> </spertity> <!-agregue-</</bean> <! id = "SqlSessionFactory"> <Property Name = "DataSource" ref = "DataSource"/> </ Bean> <Bean ID = "ThreadPoolTASKEXECUTOR"> <Property Name = "CorePoolSize" Value = "1"/> <Property Name = "MAXPoolSize" Value = "10"/> <Property Name = "KeepAliveSeSe name = "QueueCapacity" value = "50"/> <Property Name = "WaitfortAskStOcOmpleteShutdown" value = "true"/> </bean> <bean id = "transaccionManager"> <Property name = "dataSource" ref = "dataSource"> </Property> </">" < cc.royao.mana.auth.service.*. impl.*ServiceImpl.*(..))) "/> <aop: Advisor PointCut-REF =" ServiceOperation "ACONSED-REF =" TXADVICE "/> </aOP: config> <!-Configurar notificación de transacción-> <tx: consejo id =" txadvice "transaccion-manager =" <tx:attributes> <tx:method name="*" rollback-for="Exception"/> </tx:attributes> </tx:advice> <tx:advice id="transactionManagerAdivice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*insert*" propagation="REQUIRED"/> <tx:method name="*add*" propagation = "requerido"/> <tx: método name = "*update*" propagation = "requerido"/> <tx: name = "*update*" propagation = "requerido"/> <tx: método name = "*del*" propagation = "requerido"/> <tx: método name = "doapproved" propagation = "requerido"/> <tx: método n. <tx: método name = "edittemplate" propagation = "required"/> <tx: method name = "dummyDelete" propagation = "requerido"/> <tx: método name = "batchDeluser" propagation = "requerido"/> <!-<tx: name de método = "*" propagation = "requerido"/>-> </tx: atributes> </tx: jue name = "BasePackage"> <value> CC.Royao.Mana.auth.mapper.*</value> </propiety> <Property name = "sqlSessionFactoryName" value = "sqlsessionFactory"/> </ bean> <importe recource = "Aplicación Servlet.xml"/> <! <importar recursos = "spring-plan.xml"/> </beans>Resumir
Lo anterior es lo que el editor le presentó utilizando el cuarzo integrado de Spring para implementar la función del temporizador. Espero que sea útil para todos. Si tiene alguna pregunta, déjame un mensaje y el editor responderá a todos a tiempo. ¡Muchas gracias por su apoyo al sitio web de Wulin.com!