java 任务调度实例

Java任务调度框架Quartz教程实例 介绍  Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java application - from the smallest stand-alone application to the lar 阅读详情

Java任务调度分享:
import java.util.Calendar;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class DBThread {
  private ScheduledExecutorService scheduler;  //它可另行安排在给定的延迟后运行命令,或者定期执行命令。
  public static void main(String [] arg){
  new DBThread().start();
  }
private void start() {
       Calendar cal = Calendar.getInstance();  
      cal.setTimeInMillis(System.currentTimeMillis()); //获取系统当前时间
       scheduler = Executors.newScheduledThreadPool(1);  // 创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行。
      // 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后 在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。
    scheduler.scheduleAtFixedRate(runnable,0,5,TimeUnit.SECONDS);
  //参数介绍
     runnable :需要调度的任务
      0:首次执行的延迟时间   
      5: 连续执行之间的周期
      TimeUnit.SECONDS :0和5 参数的时间单位
      }
   Runnable runnable = new Runnable() {
    public void run() {
        System.out.println("running...");//调用执行任务
    }
  };
}

Spring  任务调度 方式介绍:
1.TimerTask 方式简单调度:(缺点: Timer无法精确指定何时运行)
public class TimerTask extends java.util.TimerTask {
@Override
public void run() {
  // TODO Auto-generated method stub
      System.out.println("TimerTask方式调度的执行的方法");
}

}
在Spring配置文件中:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!--配置实现类-->  
    <bean id="timeTask" class="com.spring.TimerTask"></bean> 
    <bean id="TimeTasks"    class="org.springframework.scheduling.timer.ScheduledTimerTask" >
      <property name="timerTask" ref="timeTask"></property> 
        <!-- 指定任务运行周期,单位毫秒 --> 
        <property name="period" value="1000"></property> 
        <!-- 指定任务延时时间,即第一次运行之前等待时间,单位毫秒 --> 
        <property name="delay" value="1000"></property> 
    </bean>
    <bean class="org.springframework.scheduling.timer.TimerFactoryBean">
      <property name="scheduledTimerTasks"> 
            <list> 
                <ref bean="TimeTasks"/> 
            </list>    
        </property> 
    </bean>
</beans>

2.Quartz实现调度:

2.1. MethodInvokingJobDetailFactoryBean 直接调用需要调度的方法
public class MyQuartz {
public void getHello()
{
   System.out.println("hello"); 
}

}

在spring 配置文件中信息:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="myQuartz" class="com.spring.MyQuartz"></bean>
     
    <bean id="newJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
        <property name="targetObject" ref="myQuartz"></property> 
        <property name="targetMethod" value="getHello"></property> 
    </bean> 
    
   <bean id="simplerTrigger2" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> 
        <property name="jobDetail" ref="newJob"></property> 

        <!-- 设置延迟工作的第一次执行,单位毫秒 --> 
        <property name="startDelay" value="1000"></property> 

        <!-- 设置调度任务频度,单位毫秒 --> 
        <property name="repeatInterval" value="1000"></property> 
  </bean> 

<!-- 设置调度周期 -->
  <bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
        <property name="jobDetail" ref="newJob"></property> 
        <property name="cronExpression" value="0 59 23 * * ?"></property>
  </bean>
  
  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
        <!-- 接受一组触发器,可以接受一个列表 --> 
        <property name="triggers"> 
            <list> 
                <ref bean="simplerTrigger2"/> 
                 <ref bean="cronTrigger"/> 
            </list> 
        </property> 
  </bean>
</beans>

2.2:继承QuartzJobBean 类方式调度
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.*;

public class MyQuartzJob extends QuartzJobBean {

@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
 
  System.out.println("running...");
 
  } 
}

在Spring中配置信息:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="quartzJob" class="org.springframework.scheduling.quartz.JobDetailBean"> 
        <property name="jobClass" value="com.spring.MyQuartzJob"></property> 
        <!-- 接受一个Map,其中包含了需要设置给jobClass的各种属性 --> 
        <property name="jobDataAsMap"> 
            <map></map> 
        </property> 
   </bean> 
<bean id="simplerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> 
        <property name="jobDetail" ref="quartzJob"></property> 
        <!-- 设置延迟工作的第一次执行,单位毫秒 --> 
        <property name="startDelay" value="1000"></property> 
        <!-- 设置调度任务频度,单位毫秒 --> 
        <property name="repeatInterval" value="1000"></property> 
    </bean> 
    <!-- 设置调度时间 -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
        <property name="jobDetail" ref="quartzJob"></property> 
        <property name="cronExpression" value="0 59 23 * * ?"></property>
   </bean>
   <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
        <!-- 接受一组触发器,可以接受一个列表 --> 
        <property name="triggers"> 
            <list> 
                <ref bean="simplerTrigger"/> 
                <ref bean="cronTrigger"/> 
            </list> 
        </property> 
    </bean>
  </beans>

CronTrigger spring quartz定时器的表达式 立即下载

相关推荐

先进封装技术 Part03---重布线层(RDL)的科普

RDL是一种在芯片封装过程中用于重新分布电气连接的技术。它通过在芯片表面或中介层上形成额外的布线层,重新分配芯片的I/O(输入/输出)位置,以适应不同的封装需求和提高电气连接的灵活性。

阿拉伯梳子的专栏 4738

Spring quartz 任务调度(注解方式)

1.maven org.quartz-scheduler quartz 1.8.5 2.xml配置 xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org

小程序 1万+

SecureCRT多种配色方案配置文件

位置 C:\Users%USERNAME%\AppData\Roaming\VanDyke\Config\Global.ini S:"SecureCRT Tab State"=1,1,1,0,0 S:"SecureFX Local Tab State"=1,-1,200 D:"Max Chat Window History"=000000c8 D:"Active Window Alpha Transparency"=000000f0 D:"Save Keymaps As Unicode"=00000000

神棍之路 3724

spring自动任务调度

一   简单配置 第一步: 添加jar文件:quartz-all-x.x.x.jar 编写作业代码,并配置到Spring容器中 添加并配置触发器 把触发器添加到任务调度工厂对象中 第二步: 创建类和方法 public class CountSalaryJob { public void execute() { System.out.println("执行计算工资的作

sinat_33536912的博客 2533

【coze实战】基于Coze的公众号图文自动化生成工作流实战解析

【coze实战】基于Coze的公众号图文自动化生成工作流实战解析

大家好,我是码农老gou,一名专注于编程技术的程序员。在这个博客中,我将与大家分享我在工作和学习中的经验、见解以及一些实用的技术干货。 1万+

3D 结构光相机工作原理全解 | 从编码投射到三维重建,原理 + 代码 + 工业实战一站式掌握

本文从3D结构光相机的整体架构出发,层层拆解了编码投射、相位计算、相位解包裹、三维重建的核心原理,补充了工业实战所需的公式推导、代码实现、参数调优、设备选型和案例落地,核心总结如下:相位是3D结构光成像的“灵魂”,其精度、稳定性直接决定3D重建的效果,包裹相位与绝对相位的区分的是工业实战的基础;编码投射的核心是“正弦条纹+格雷码条纹”组合,正弦条纹负责高精度相位提取,格雷码条纹辅助解包裹,适配工业场景的精度与稳定性需求;

getusushu的博客 418

Spring任务调度

需求一:没秒执行一次某个类中方法

axe的专栏 3555

基于情感分析+聚类分析+LDA主题分析对服装产品类的消费者评论分析

本研究旨在通过对服装产品类的消费者评论进行综合分析,为企业提供更全面、更深入的市场洞察和决策支持。同时,本研究的方法和结果也可以为其他领域的消费者评论分析提供借鉴和参考。

m0_64336780的博客 2万+

Spring定时任务的几种实现

近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我都将结合spring框架来介绍。一.分类从实现的技术上来分类,目前主要有三种技术(或者说有三种产品):Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的...

weixin_30516243的博客 1801

ElectrolyteGPT:首个电池电解液逆向设计的生成式 AI 系统 —— 算法原理与复现解析

数据驱动的重要性:1100 万→100 万的精心筛选,比单纯增大模型更有效领域适配:fLine 表示法、正则分词、化学感知的数据增强(SMILES randomization)都是关键小模型的力量:6M 参数在特定化学空间的表现,远超通用大模型在相同任务上的零样本能力条件生成的极限:10 维条件导致模型崩溃,提示我们需要更好的条件编码策略(如 diffusion guidance、RLHF-style 偏好优化)未来方向配方实验验证。

ASKCOS博客 130

小程序数据采集(3):PC端微信小程序降维调试与流量数据获取详解

本文提出了一种针对PC端微信小程序的逆向分析方法,通过利用PC端基于Chromium架构的天然优势,实现无需Root的降维打击。文章详细介绍了如何开启CEF远程调试协议、基于DevTools Protocol实现流量劫持、对抗PC端安全防线以及获取小程序本地存储数据的技术方案。相比移动端严苛的环境,PC端小程序在调试接口、网络拦截和数据存储等方面存在显著漏洞,为爬虫开发提供了更便捷的突破口。

数据知道的博客 838

Spring任务调度-基于Quartz

任务调度图解: 1.继承JobDetail类的方式 Java代码 public class QuartzJob extends QuartzJobBean { private String command; public void setCommand(String command) { th

心情夜雪 839

AI大模型探索之路-应用篇11:AI大模型应用智能评估(Ragas)

随着人工智能技术的飞速发展,AI大模型(LLM)已经成为了推动技术创新和应用的关键因素。这些大模型在语言理解、图像识别、自然语言生成等领域展现出了惊人的能力。然而,随着模型规模的增大,它们对计算资源的消耗、环境适应性、模型稳定性、安全性和可解释性等方面也提出了新的挑战。因此,对AI大模型进行智能评估变得至关重要,以确保它们在实际应用中的稳定性、可靠性和有效性。本文将详细介绍Ragas框架,一个专为AI大模型设计的智能评估工具。

寻道AI,探索AI无限可能! 5557

java quartz实例_Java任务调度框架Quartz教程实例

介绍Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java application - from the smallest stand-alone application tothe largest...

weixin_39998521的博客 215

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Quartz-2.2.1 任务调度框架在Java项目中的使用实例

《 Quartz-2.2.1 任务调度框架在Java项目中的使用实例 》 本实例是基于Quartz 2.2.1 版本为中心,也是目前最新的Quartz任务调度框架。 目前在 J2EE 项目开发的过程中用的最多的任务调度框架为 Quartz,项目完成后,总结一下,主要为一些核心 API 和接口的概念比较重要。 一、Quartz-2.2.1使用实例 下面是一个通用的项目实例,高

Spring Boot-Common On With You 1143

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

java timer demo_java中任务调度java.util.Timer,ScheduledExecutor,Quartz的机制说明和demo代码实例分享...

目前的 Web 应用,多数应用都具备任务调度的功能。这里就简单的介绍任务调度的Java 实现方法,主要包括 Timer,Scheduler, Quartz 以及 JCron Tab,目的在于给需要开发任务调度的牛牛们提供一个参考。1.Timer大部分已经非常熟悉 java.util.Timer 了,它是最简单的一种实现任务调度的方法,下面给出一个具体的例子:使用 Timer 实现任务调度的核心类是...

weixin_32958777的博客 108

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

linux java 调度_Java任务调度框架Quartz教程实例

介绍Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java application - from the smallest stand-alone application to the larges...

weixin_35015064的博客 156
上一篇: Java 任务调度
下一篇: Spring JdbcTemplate 实现自定义分页
WU-ZhiLe
博客等级 码龄18年 44粉丝 4原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值