我想在Java动态Web应用程序中使用Quartz
2.2创建一个调度程序。我是这个新手。我尝试了网络上的所有教程。我尝试使用上下文侦听器方法初始化调度程序。似乎不起作用。hello
world程序仅在一般的Java应用程序中有效。对于Web应用程序,它看起来很棘手。
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>test.ananth</groupId>
<artifactId>test-app</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>test-app Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>servlet-api</artifactId>
<version>6.0.30</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>test-app</finalName>
</build>
crystal.properties:
#org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
# Main Quartz configuration
org.quartz.scheduler.skipUpdateCheck = true
org.quartz.scheduler.instanceName = MyQuartzScheduler
org.quartz.scheduler.jobFactory.class = org.quartz.simpl.SimpleJobFactory
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
#org.quartz.threadPool.threadCount = 5
HelloJob.java:
package com.test;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job {
public HelloJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("Hello! HelloJob is executing.");
}
}
servlet.java包com.test;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
public class MyServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1567185871113714035L;
public void init(ServletConfig cfg) {
String key = "org.quartz.impl.StdSchedulerFactory.KEY";
ServletContext servletContext = cfg.getServletContext();
StdSchedulerFactory factory = (StdSchedulerFactory) servletContext
.getAttribute(key);
// Scheduler quartzScheduler = factory.getScheduler("MyQuartzScheduler");
Scheduler sched;
try {
sched = factory.getScheduler("MyQuartzScheduler");
//sched = factory.getScheduler();//MyQuartzScheduler
sched.start();
// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class).withIdentity("myJob",
"group1").build();
// Trigger the job to run now, and then every 40 seconds
Trigger trigger = newTrigger()
.withIdentity("myTrigger", "group1")
.startNow()
.withSchedule(
simpleSchedule().withIntervalInSeconds(4)
.repeatForever()).build();
// Tell quartz to schedule the job using our trigger
sched.scheduleJob(job, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
Web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<context-param>
<context-param>
<param-name>quartz:shutdown-on-unload</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>quartz:wait-on-shutdown</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>quartz:start-scheduler-on-load</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>quartz:config-file</param-name>
<param-value>/WEB-INF/quartz.properties</param-value>
</context-param>
<listener>
<listener-class>
org.quartz.ee.servlet.QuartzInitializerListener
</listener-class>
</listener>
<listener>
<listener-class>com.test.ApplicationStartup</listener-class>
</listener>
<display-name>Archetype Created Web Application</display-name>
</web-app>
我正在使用Maven Web应用程序原型。
如果您在Eclipse中使用典型项目,则最基本的示例的结构类似于:
C:.
|
+---src
| | log4j.dtd
| | log4j.xml
| | quartz.properties
| | quartz_data.xml
| |
| \---org
| \---paulvargas
| \---test
| \---quartz
| TestJob.java
|
\---WebContent
\---WEB-INF
| web.xml
|
\---lib
jta-1.1.jar
log4j-1.2.17.jar
quartz-2.1.5.jar
slf4j-api-1.6.5.jar
slf4j-log4j12-1.6.5.jar
每个文件的代码如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<listener>
<listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class>
</listener>
</web-app>
quartz.properties
# ----------------------------- Threads --------------------------- #
# How many jobs can run at the same time?
org.quartz.threadPool.threadCount=5
# ----------------------------- Plugins --------------------------- #
# Class to load the configuration data for each job and trigger.
# In this example, the data is in an XML file.
org.quartz.plugin.jobInitializer.class=org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
quartz_data.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="1.8">
<schedule>
<job>
<name>TestJob</name>
<job-class>org.paulvargas.test.quartz.TestJob</job-class>
</job>
<trigger>
<cron>
<name>TestJob</name>
<job-name>TestJob</job-name>
<cron-expression>0 0/5 * 1/1 * ? *</cron-expression>
</cron>
</trigger>
</schedule>
</job-scheduling-data>
该作业被执行每5分钟(参见式0 0/5 * 1/1 * ? *
中cron- expression
标记)。如果您想要另一个表达式,则可以使用http://www.cronmaker.com/
TestJob.java
package org.paulvargas.test.quartz;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class TestJob implements Job {
@Override
public void execute(final JobExecutionContext ctx)
throws JobExecutionException {
System.out.println("Executing Job");
}
}
log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd" >
<log4j:configuration>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p (%c.java:%L).%M - %m%n"/>
</layout>
</appender>
<root>
<priority value="TRACE" />
<appender-ref ref="STDOUT"/>
</root>
</log4j:configuration>
如果使用的是Maven,则同一项目的结构为:
C:.
| pom.xml
|
\---src
\---main
+---java
| \---org
| \---paulvargas
| \---test
| \---quartz
| TestJob.java
|
+---resources
| log4j.dtd
| log4j.xml
| quartz.properties
| quartz_data.xml
|
\---webapp
| index.jsp
|
\---WEB-INF
web.xml
和文件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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>BasicQuartz</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>BasicQuartz</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
*这需要Servet 3.0+(Tomcat 7 +,Glassfish 3 +,JBoss AS 7)
您只需要两个文件:TestJob.java
上一个示例和以下侦听器:
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.annotation.WebListener;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.quartz.impl.StdSchedulerFactory;
@WebListener
public class QuartzListener extends QuartzInitializerListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
super.contextInitialized(sce);
ServletContext ctx = sce.getServletContext();
StdSchedulerFactory factory = (StdSchedulerFactory) ctx.getAttribute(QUARTZ_FACTORY_KEY);
try {
Scheduler scheduler = factory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob(TestJob.class).build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("simple").withSchedule(
CronScheduleBuilder.cronSchedule("0 0/1 * 1/1 * ? *")).startNow().build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
} catch (Exception e) {
ctx.log("There was an error scheduling the job.", e);
}
}
}
为避免冲突,请勿同时设置默认侦听器web.xml
。在最后一个示例中,默认线程数为10。由于调度程序以待机模式启动,因此有必要调用scheduler.start();
。该"simple"
标识是可选的,但你可以用它来重新安排工作(太棒了!)。例如:
ServletContext ctx = request.getServletContext();
StdSchedulerFactory factory = (StdSchedulerFactory) ctx.getAttribute(QuartzListener.QUARTZ_FACTORY_KEY);
Scheduler scheduler = factory.getScheduler();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("simple").withSchedule(
CronScheduleBuilder.cronSchedule(newCronExpression)).startNow().build();
Date date = scheduler.rescheduleJob(new TriggerKey("simple"), trigger);
问题内容: 我面临着同源政策问题,并且通过研究该主题,我发现对我的特定项目而言,最好的方法是使用JSONP进行跨域请求。 我一直在阅读IBM的有关JSONP的这篇文章,但是我对所发生的事情并不十分了解。 我在这里要求的只是一个简单的jQuery> PHP JSONP请求(或任何可能的术语;))-诸如此类 (显然这是不正确的,只是为了让您了解我要实现的目标:)) : jQuery的: PHP: 我将
问题内容: 有人可以给我一个以这种方式涉及线程的简单示例。 我的代码的问题是,当我单击按钮One时,GUI冻结直到完成。我希望在执行def时按钮保持响应。我该如何解决? 求您了,帮帮我。 问题答案: 使用gtk时,它将运行一个主循环,并将您想做的 所有事情 安排为gtk循环的事件。您不需要线程即可执行任何操作。 这是一个完整的,完整的,随时可以运行的示例,用于执行您想要的操作。 请注意,单击两
问题内容: 我不知道和 方法的作用,因此请举一个简单的示例帮助我。 问题答案: 这是一个非常简单的Python示例,应该补充pickle docs 。
本文向大家介绍Java简单实现UDP和TCP的示例,包括了Java简单实现UDP和TCP的示例的使用技巧和注意事项,需要的朋友参考一下 TCP实现 TCP协议需要在双方之间建立连接,通过输入输出流来进行数据的交换,建立需要通过三次握手,断开需要四次挥手,保证了数据的完整性,但传输效率也会相应的降低。 简单的TCP实现 改进服务端,启用多线程来接受客户端的数据 传递图片 UDP实现 UDP是将数据打
问题内容: 使用Javascript最简单的SOAP示例是什么? 为了尽可能有用,答案应该是: 具有功能性(换句话说,实际上有效) 发送至少一个可以在代码中其他位置设置的参数 处理至少一个可以在代码的其他位置读取的结果值 使用大多数现代浏览器版本 在不使用外部库的情况下尽可能清晰明了 问题答案: 这是我可以创建的最简单的JavaScript SOAP客户端。
问题内容: 请帮助我,如果有人可以给我一个例子,如何在cakephp 2.3.0中使用ajax,一个例子是这样的 当我单击链接时,具有ID的div 会发生变化。我如何使用cakephp 2.3.0做到这一点? 问题答案: 请参见以下示例: