请帮我解决这个问题,我会在日食的具体时间每天申请发送自动信息,但我很困惑所以给我建议
答案 0 :(得分:0)
使用Quartz 2.1.5 API
<!--Maven Dependency-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.1.5</version>
</dependency>'
要集成Struts 2和Quartz 2调度程序,我们必须使用标准的Servlet侦听器将Quartz API链接到Struts。 使用ServletContextListener接口,如下所示。
QuartzListener.java
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.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
/**
* This class is used for executing quartz job
* using CronTrigger(Quartz 2.1.5).
* @author Prabhakar Kumar
*/
public class QuartzListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("Destroyed is called.");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
try{
//Set job details.
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("helloJob", "group1").build();
//Set the scheduler timings.
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("cronTrigger", "group1")
.withSchedule(CronScheduleBuilder
.cronSchedule("0/10 * * * * ?")).build();
//"0/10 * * * * ?"--->here you can put date time day of
//week month just read more about Quartz API
//Execute the job.
Scheduler scheduler =
new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}catch(Exception e){
e.printStackTrace();
}
}
}
您的日程安排方法看起来像
HelloJob.java
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* This class defines a quartz job.
* @author Prabhakar Kumar
*/
public class HelloJob implements Job{
public void execute(JobExecutionContext context)
throws JobExecutionException {
//Here is your code in this method(Your Logic)
System.out.println("Hello World.");
}
}
Web.xml看起来像
<?xml version="1.0" encoding="UTF-8"?> <!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> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng. filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class> com.javawithease.business.QuartzListener </listener-class> </listener> </web-app>