gpt4 book ai didi

java - 在Java中定期执行任务

转载 作者:行者123 更新时间:2023-11-30 06:13:15 27 4
gpt4 key购买 nike

我创建了一个向客户发送提醒消息的应用程序。但我会 喜欢让它在特定时间发送这些消息。 我的数据库中已经记录了截止日期和消息类型。有 4 种类型的提醒消息 客户:

  1. 截止日期后 45 天前
  2. 30 天前。
  3. 两周前。
  4. 1 天前。

enter image description here

这是我现在的错误:

enter image description here

最佳答案

您可以为此使用 Quartz Scheduler,您需要如下创建 Job

Approach 1: How to use Quartz Scheduler in Standalone java program

public class HelloJob implements Job {
public void execute(JobExecutionContext arg0) throws JobExecutionException{
//your code goes here for sending reminders
}
}

并使用 SimpleTrigger 在指定时间运行一次作业 [in below code in new Date()](您需要根据 45/30/14/1 等过期时间计算天前)

导入语句:

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

try{

SchedulerFactory sf=new StdSchedulerFactory();
Scheduler sched=sf.getScheduler();

JobDetail jd = newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build();

SimpleTrigger st = (SimpleTrigger) newTrigger()
.withIdentity("trigger1", sched.DEFAULT_GROUP)
.startAt(new Date())
.withSchedule(simpleSchedule()
.withIntervalInSeconds(10)
.withRepeatCount(1))
.build();

Date ft = sched.scheduleJob(jd, st);

System.out.println(jd.getKey() +
" will run at: " + ft +
" and repeat: " + st.getRepeatCount() +
" times, every " + st.getRepeatInterval() / 1000 + " seconds");

sched.start();

}catch (SchedulerException e) {
System.out.println("Exception: "+e.getMessage());;
}

Approach 2: How to use Quartz Scheduler in Servlets:

您需要将类似这样的内容添加到您的 WEB-INF/web.xml 文件中:

<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<display-name>Quartz Initializer Servlet</display-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>config-file</param-name>
<param-value>quartz.properties</param-value>
</init-param>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>wait-on-shutdown</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>start-scheduler-on-load</param-name>
<param-value>true</param-value>
</init-param>
</servlet>

你需要一个 src/main/resources/quartz.properties 调度器的配置文件

# 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

您可以像这样检索调度程序并在您自己的 Servlet 中使用它:

public class YourServlet extends HttpServlet {
public init(ServletConfig cfg) {
ServletContext servletContext = cfg.getServletContext();
StdSchedulerFactory factory = (StdSchedulerFactory) servletContext.getAttribute(QuartzFactoryServlet.QUARTZ_FACTORY_KEY);
Scheduler quartzScheduler = factory.getScheduler("MyQuartzScheduler");
// and continue to implement as in approach 1
}
}

Approach 3: How to use Quartz Scheduler in Spring:

您需要创建quartz-context.xml

<context:component-scan base-package="com.gs.spring" />

<!-- if you need to invoke a method on an object -->
<bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myBean" />
<property name="targetMethod" value="sendReminder" />
</bean>

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="simpleJobDetail" />
<property name="repeatCount" value="0"/>
<property name="repeatInterval" value="10"/>
</bean>

<!-- Scheduler factory bean to glue together jobDetails and triggers to Configure Quartz Scheduler -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="simpleJobDetail" />
</list>
</property>

<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>

POJO任务bean

package com.gs.spring;

import org.springframework.stereotype.Component;

@Component("myBean")
public class MyBean {

public void sendReminder() {
//send an reminder
}

}

关于java - 在Java中定期执行任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32069774/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com