亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java基于QuartzJobBean實(shí)現(xiàn)定時(shí)功能的示例代碼

 更新時(shí)間:2023年09月07日 10:29:25   作者:憑欄聽雨客  
QuartzJobBean是Quartz框架中的一個(gè)抽象類,用于定義和實(shí)現(xiàn)可由Quartz調(diào)度的作業(yè),本文主要介紹了java基于QuartzJobBean實(shí)現(xiàn)定時(shí)功能的示例代碼,具有一定的參考價(jià)值,感興趣可以了解一下

業(yè)務(wù)場景:JAVA實(shí)現(xiàn)一個(gè)定時(shí)發(fā)送郵件的任務(wù)。

我們可以使用QuartzJobBean 來完成上述功能。QuartzJobBean 是 Quartz 框架中的一個(gè)抽象類,用于定義和實(shí)現(xiàn)可由 Quartz 調(diào)度的作業(yè)(Job)??梢酝ㄟ^繼承 QuartzJobBean 類并實(shí)現(xiàn)其中的抽象方法來定義自己的作業(yè)。

以下是一個(gè)基本的使用示例:

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class MyJob extends QuartzJobBean {
? ? @Override
? ? protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
? ? ? ? // 在這里編寫您的作業(yè)邏輯
? ? ? ? System.out.println("MyJob is executing...");
? ? }
}

在上面的示例中,我們創(chuàng)建了一個(gè)名為 MyJob 的作業(yè)類,它繼承自 QuartzJobBean。我們需要實(shí)現(xiàn) executeInternal 方法,這是作業(yè)的實(shí)際執(zhí)行邏輯。在這個(gè)方法中,您可以編寫您的作業(yè)代碼。QuartzJobBean 提供了一些有用的功能,例如獲取作業(yè)執(zhí)行上下文(JobExecutionContext)和處理作業(yè)執(zhí)行異常(JobExecutionException)等。

基于上面的介紹,

我們使用 Spring Boot 框架來實(shí)現(xiàn)基于 QuartzJobBean 的定時(shí)發(fā)送郵件的任務(wù),可以按照以下步驟進(jìn)行操作:

1. 添加依賴

在 Maven 或 Gradle 構(gòu)建文件中添加 Spring Boot 和 Quartz 的相關(guān)依賴。

對于 Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

2.創(chuàng)建定時(shí)任務(wù)類

創(chuàng)建一個(gè)繼承自 QuartzJobBean 的定時(shí)任務(wù)類,例如 EmailJob,并實(shí)現(xiàn)任務(wù)的邏輯。

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class EmailJob extends QuartzJobBean {
? ? private EmailSender emailSender;
? ? // 通過 setter 方法注入 EmailSender
? ? public void setEmailSender(EmailSender emailSender) {
? ? ? ? this.emailSender = emailSender;
? ? }
? ? @Override
? ? protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
?? ??? ??? ??? ?// 在這里編寫任務(wù)的邏輯
? ? ? ? emailSender.sendEmail();
? ? }
}

上述的EmailSender 是通過setter方法注入的,我們還可以通過構(gòu)造函數(shù)注入或者使用 Spring 的自動(dòng)裝配(Autowired)功能來實(shí)現(xiàn)依賴注入。

構(gòu)造函數(shù)注入:

修改 EmailJob 類,添加一個(gè)帶有 EmailSender 參數(shù)的構(gòu)造函數(shù),并在構(gòu)造函數(shù)中進(jìn)行注入。

public class EmailJob extends QuartzJobBean {
? ? private final EmailSender emailSender;
? ? public EmailJob(EmailSender emailSender) {
? ? ? ? this.emailSender = emailSender;
? ? }
? ? // 省略其他代碼...
}

在配置類中,使用構(gòu)造函數(shù)注入 EmailSender。

@Configuration
public class QuartzConfig {
? ? private final EmailSender emailSender;
? ? public QuartzConfig(EmailSender emailSender) {
? ? ? ? this.emailSender = emailSender;
? ? }
? ? @Bean
? ? public JobDetailFactoryBean emailJobDetail() {
? ? ? ? JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
? ? ? ? factoryBean.setJobClass(EmailJob.class);
? ? ? ? factoryBean.setDurability(true);
? ? ? ? factoryBean.setConstructorArgumentValues(Collections.singletonMap("emailSender", emailSender));
? ? ? ? return factoryBean;
? ? }
? ? // 省略其他代碼...
}

這樣,EmailSender 實(shí)例將通過構(gòu)造函數(shù)注入到 EmailJob 類中。

自動(dòng)裝配(Autowired)注解:

修改 EmailJob 類,使用 @Autowired 注解標(biāo)記 EmailSender 字段。

public class EmailJob extends QuartzJobBean {
? ? @Autowired
? ? private EmailSender emailSender;
? ? // 省略其他代碼...
}

在配置類中使用 @Autowired 注解將 EmailJob 實(shí)例注入到配置類中,然后通過 JobDataMap 將實(shí)例傳遞給 JobDetailFactoryBean

@Configuration
public class QuartzConfig {
? ? @Autowired
? ? private EmailJob emailJob;
? ? @Bean
? ? public JobDetailFactoryBean emailJobDetail() {
? ? ? ? JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
? ? ? ? factoryBean.setJobClass(EmailJob.class);
? ? ? ? factoryBean.setDurability(true);
? ? ? ? factoryBean.setJobDataMap(new JobDataMap(Collections.singletonMap("emailJob", emailJob)));
? ? ? ? return factoryBean;
? ? }
? ? // 省略其他代碼...
}

3. 創(chuàng)建郵件發(fā)送類

創(chuàng)建一個(gè)負(fù)責(zé)實(shí)際郵件發(fā)送的類,例如 EmailSender。

public class EmailSender {
    public void sendEmail() {
        // 實(shí)際的郵件發(fā)送邏輯
        System.out.println("發(fā)送郵件...");
    }
}

4.創(chuàng)建配置類

創(chuàng)建一個(gè)配置類,用于配置 Quartz 調(diào)度器和任務(wù)觸發(fā)器。

import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
@Configuration
public class QuartzConfig {
? ? @Bean
? ? public JobDetailFactoryBean emailJobDetail() {
? ? ? ? JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
? ? ? ? factoryBean.setJobClass(EmailJob.class);
? ? ? ? factoryBean.setDurability(true);
? ? ? ? return factoryBean;
? ? }
? ? @Bean
? ? public CronTriggerFactoryBean emailJobTrigger(JobDetail emailJobDetail) {
? ? ? ? CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
? ? ? ? factoryBean.setJobDetail(emailJobDetail);
? ? ? ? factoryBean.setCronExpression("0 0 8 * * ?"); // 每天 8 點(diǎn)執(zhí)行
? ? ? ? return factoryBean;
? ? }
? ? @Bean
? ? public SchedulerFactoryBean schedulerFactory(Trigger emailJobTrigger) {
? ? ? ? SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
? ? ? ? factoryBean.setTriggers(emailJobTrigger);
? ? ? ? return factoryBean;
? ? }
}

在上述配置類中,我們創(chuàng)建了一個(gè) JobDetail 實(shí)例來指定要執(zhí)行的任務(wù)類,然后創(chuàng)建一個(gè) CronTrigger 實(shí)例來定義觸發(fā)器的調(diào)度規(guī)則。最后,使用 SchedulerFactoryBean 將觸發(fā)器配置到 Quartz 調(diào)度器中。

5.啟動(dòng)應(yīng)用程序

創(chuàng)建一個(gè) Spring Boot 應(yīng)用程序的入口類,并在其中添加 @EnableScheduling 注解來啟用調(diào)度任務(wù)。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Application {
? ? public static void main(String[] args) {
? ? ? ? SpringApplication.run(Application.class, args);
? ? }
}

現(xiàn)在,當(dāng)你啟動(dòng) Spring Boot 應(yīng)用程序時(shí),定時(shí)任務(wù)將按照指定的調(diào)度規(guī)則觸發(fā)執(zhí)行,調(diào)用 EmailJob 類的 executeInternal 方法,進(jìn)而調(diào)用 EmailSender 類的 sendEmail 方法來發(fā)送郵件。

到此這篇關(guān)于java基于QuartzJobBean實(shí)現(xiàn)定時(shí)功能的示例代碼的文章就介紹到這了,更多相關(guān)java QuartzJobBean定時(shí)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot中如何對actuator進(jìn)行關(guān)閉

    SpringBoot中如何對actuator進(jìn)行關(guān)閉

    這篇文章主要介紹了SpringBoot中如何對actuator進(jìn)行關(guān)閉問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Java中數(shù)組越界異常的優(yōu)雅解決方式

    Java中數(shù)組越界異常的優(yōu)雅解決方式

    ?數(shù)組越界報(bào)錯(cuò)通常發(fā)生在嘗試訪問數(shù)組中不存在的索引時(shí),這可能導(dǎo)致程序崩潰或異常,這篇文章主要給大家介紹了關(guān)于Java中數(shù)組越界異常的優(yōu)雅解決方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • maven配置多個(gè)鏡像的實(shí)現(xiàn)方法

    maven配置多個(gè)鏡像的實(shí)現(xiàn)方法

    這篇文章主要介紹了maven配置多個(gè)鏡像的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • IDEA中的打包Build Artifacts圖文詳解

    IDEA中的打包Build Artifacts圖文詳解

    當(dāng)項(xiàng)目開發(fā)完畢,需要對外發(fā)布時(shí),我們就會(huì)用到IDEABuild Artifacts功能,那么如果在idea中打包呢,這篇文章主要介紹了IDEA中的打包Build Artifacts詳解,需要的朋友可以參考下
    2024-03-03
  • 關(guān)于使用Mybatisplus自帶的selectById和insert方法時(shí)的一些問題

    關(guān)于使用Mybatisplus自帶的selectById和insert方法時(shí)的一些問題

    這篇文章主要介紹了關(guān)于使用Mybatisplus自帶的selectById和insert方法時(shí)的一些問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • java集合遍歷的幾種方式總結(jié)及詳細(xì)比較

    java集合遍歷的幾種方式總結(jié)及詳細(xì)比較

    下面小編就為大家?guī)硪黄猨ava集合遍歷的幾種方式總結(jié)及詳細(xì)比較。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • springboot項(xiàng)目docker分層構(gòu)建的配置方式

    springboot項(xiàng)目docker分層構(gòu)建的配置方式

    在使用dockerfile構(gòu)建springboot項(xiàng)目時(shí),速度較慢,用時(shí)比較長,為了加快構(gòu)建docker鏡像的速度,采用分層構(gòu)建的方式,這篇文章主要介紹了springboot項(xiàng)目docker分層構(gòu)建,需要的朋友可以參考下
    2024-03-03
  • 使用Arthas定位問題及分析

    使用Arthas定位問題及分析

    本文通過使用Arthas工具對一個(gè)bug進(jìn)行分析,發(fā)現(xiàn)該bug的原因是不同類型的動(dòng)態(tài)代理(JDK和CGlib)實(shí)現(xiàn)機(jī)制的不同導(dǎo)致的
    2025-01-01
  • 基于Java HashMap的死循環(huán)的啟示詳解

    基于Java HashMap的死循環(huán)的啟示詳解

    本篇文章是對Java HashMap的死循環(huán)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Spring?AOP對嵌套方法不起作用的解決

    Spring?AOP對嵌套方法不起作用的解決

    這篇文章主要介紹了Spring?AOP對嵌套方法不起作用的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01

最新評論