quart 简单应用
Posted 不要相信我
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了quart 简单应用相关的知识,希望对你有一定的参考价值。
pom.xml
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.byref</groupId> <artifactId>web-quartz</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>web-quartz Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> <!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz-jobs --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.1</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.transaction/jta --> <dependency> <groupId>javax.transaction</groupId> <artifactId>jta</artifactId> <version>1.1</version> </dependency> </dependencies> <build> <finalName>web-quartz</finalName> </build> </project>
quartz.properties
#============================================================================ # Configure Main Scheduler Properties #============================================================================ org.quartz.scheduler.instanceName = QuartzScheduler org.quartz.scheduler.instanceId = AUTO #============================================================================ # Configure ThreadPool #============================================================================ org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 3 org.quartz.threadPool.threadPriority = 5 #============================================================================ # Configure JobStore #============================================================================ org.quartz.jobStore.misfireThreshold = 60000 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore #============================================================================ # Configure Plugins #============================================================================ org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin org.quartz.plugin.jobInitializer.fileNames = quartz_job.xml org.quartz.plugin.jobInitializer.failOnFileNotFound = true org.quartz.plugin.jobInitializer.scanInterval =10 org.quartz.plugin.jobInitializer.wrapInUserTransaction = false org.quartz.scheduler.skipUpdateCheck = true
quartz_job.xml
<?xml version="1.0" encoding="UTF-8"?> <job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_2_0.xsd" version="2.0"> <schedule> <job> <name>myjob</name> <group>mygroup</group> <description>my job</description> <job-class>cn.yrm.web.job.MyJob</job-class> </job> <trigger> <cron> <name>my trigger</name> <group>mygroup</group> <job-name>myjob</job-name> <job-group>mygroup</job-group> <cron-expression>0/5 * * * * ?</cron-expression> </cron> </trigger> </schedule> </job-scheduling-data>
Job实现类:
package cn.yrm.web.job; import org.quartz.DisallowConcurrentExecution; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; @DisallowConcurrentExecution public class MyJob implements Job { public void execute(JobExecutionContext arg0) throws JobExecutionException { // TODO Auto-generated method stub System.out.println("my job execute"); } }
ServletContextListener实现类:
package cn.yrm.web.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.quartz.CronScheduleBuilder; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import cn.yrm.web.job.MyJob; public class MyQuatzListener implements ServletContextListener { Logger logger = LoggerFactory.getLogger(this.getClass()); private Scheduler scheduler; public void contextDestroyed(ServletContextEvent arg0) { logger.info("shutdown scheduler"); if (scheduler != null) { try { scheduler.shutdown(); } catch (SchedulerException e) { logger.error(e.getMessage()); e.printStackTrace(); } } } public void contextInitialized(ServletContextEvent arg0) { logger.info("context initialized begin start quartz"); try { scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); // JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("myJob", "myGroup").build(); // CronScheduleBuilder cronScheduleBuilder= CronScheduleBuilder.cronSchedule("*/5 * * * * ?"); // Trigger trigger = TriggerBuilder.newTrigger() // .withIdentity("myTrigger","myGroup") // .startNow().withSchedule(cronScheduleBuilder).build(); // // scheduler.scheduleJob(jobDetail, trigger); } catch (SchedulerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
以上是关于quart 简单应用的主要内容,如果未能解决你的问题,请参考以下文章
定时器篇---java.util.TimerTask和quarts
Python Quart/Hypercorn 流式响应导致 net::ERR_HTTP2_PROTOCOL_ERROR 200