`
y806839048
  • 浏览: 1108391 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

quart动态定时

阅读更多
<bean id="simple" lazy-init="false" class="com.ces.interfaceSchdule.utils.InterfaceJobTrigger" init-method="run" >
</bean>




trigger:

/*
* Copyright 2005 - 2009 Terracotta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*  
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/

package com.ces.interfaceSchdule.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ces.zwww.entity.ScheduleInfo;
import com.ces.zwww.service.ScheduleInfoLogService;
import com.ces.zwww.service.ScheduleInfoService;

/**
* This Example will demonstrate all of the basics of scheduling capabilities of
* Quartz using Simple Triggers.
*
* @author Bill Kratzer
*/
@Component
public class InterfaceJobTrigger {

@Autowired
private ScheduleInfoService shser;

@Autowired
private ScheduleInfoLogService shserver;

@Autowired
private InterfaceJob simpleJob;

public void run() throws Exception {
Logger log = LoggerFactory.getLogger(InterfaceJobTrigger.class);
List<ScheduleInfo> shIn = new ArrayList<ScheduleInfo>();
shIn = shser.getScheduleInfo();

//First we must get a reference to a scheduler
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
long ts = 0;

ts = TriggerUtils.getNextGivenSecondDate(null, 15).getTime();

for (int i = 0; i < shIn.size(); i++) {
int zt =Integer.parseInt(shIn.get(i).getCfqzt());
if(zt==1){
String jobName = shIn.get(i).getRwmc();
String jobgroup = shIn.get(i).getRwfz();
String triggerName = shIn.get(i).getCfqmc();
String triggergroup = shIn.get(i).getCfqmc();

Long pl = Long.parseLong(shIn.get(i).getDspl());
JobDetail job = new JobDetail(jobName, jobgroup, simpleJob.getClass());
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("interfaceName", shIn.get(i).getInterfacename());
jobDataMap.put("interfaceMethod", shIn.get(i).getMethod());
job.setJobDataMap(jobDataMap);

SimpleTrigger trigger = new SimpleTrigger(triggerName,
triggergroup, jobName, jobgroup, new Date(ts), null,
SimpleTrigger.REPEAT_INDEFINITELY, pl * 1000);
// CronTrigger trigger = new CronTrigger(triggerName,triggergroup,jobName
// ,jobgroup,new Date(ts),null,shIn.get(i).getDspl());
Date ft = sched.scheduleJob(job, trigger);
log.info(job.getFullName() + " will run at: " + ft
+ " and CronExpression: " + trigger.getJobName()
+ " Name " + trigger.getName());
log.info("job7 rescheduled to run at: " + ft);
sched.start();
// ScheduleInfoLog schLog = new ScheduleInfoLog();
// schLog.setId(i+5+"");
// schLog.setRwid(shIn.get(i));
// shserver.save(schLog);
}
}
}


}



job:



/*
* Copyright 2005 - 2009 Terracotta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*  
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/

package com.ces.interfaceSchdule.utils;

import java.lang.reflect.Method;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
* <p>
* This is just a simple job that gets fired off many times by example 1
* </p>
*
* @author Bill Kratzer
*/
@Component
public class InterfaceJob implements Job {

private static Logger _log = LoggerFactory.getLogger(InterfaceJob.class);

private String interfaceName;
private String interfaceMethod;

public InterfaceJob() {

}

/**
* <p>
* Called by the <code>{@link org.quartz.Scheduler}</code> when a
* <code>{@link org.quartz.Trigger}</code> fires that is associated with the
* <code>Job</code>.
* </p>
*
* @throws JobExecutionException
*             if there is an exception while executing the job.
*/
public void execute(JobExecutionContext context)
throws JobExecutionException {
//
setJobParam(context);
// This job simply prints out its job name and the
// date and time that it is running
try {
WebApplicationContext ctx = ContextLoader
.getCurrentWebApplicationContext();
Object object = WebApplicationContextUtils
.getWebApplicationContext(ctx.getServletContext()).getBean(
getInterfaceClass());
if(getInterfaceMethod()!=null&&object!=null)
getInterfaceMethod().invoke(object);

// netManagerTaskService.getTicketMsg();
} catch (Exception e) {
e.printStackTrace();
}

}

/**
     *
     */
private void setJobParam(JobExecutionContext context) {
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
interfaceName = jobDataMap.getString("interfaceName");
interfaceMethod = jobDataMap.getString("interfaceMethod");
}

/**
* 获取当前参数的类
*
* @return
*/
private Class getInterfaceClass() {
try {
return Class.forName(interfaceName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}

/**
*
* @return
*/
private Method getInterfaceMethod() {
try {
return getInterfaceClass().getMethod(interfaceMethod);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}

}
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Quart定时任务.zip

    Quart定时任务是一种基于Java的开源作业调度框架,它允许应用程序定义和执行重复的任务。Quartz的核心特性包括灵活的调度,支持多种触发器(如SimpleTrigger、CronTrigger)以及可扩展的工作(Job)。在本项目中,...

    结合quart完成动态定时任务源码(数据库持久化)

    本资源主要探讨如何利用Spring Boot与Quart库相结合,实现动态的定时任务,并将定时任务信息存储到数据库中进行持久化。下面将详细阐述这一技术方案。 首先,Spring Boot是一个基于Java的微服务框架,提供了简化...

    java动态管理定时任务

    Spring框架与Quartz库的整合提供了强大的动态管理定时任务的能力。下面我们将深入探讨这个主题。 Spring框架是Java开发中的核心组件,它提供了一个全面的编程和配置模型,使得开发过程更加简洁高效。Spring通过其...

    springboot整合Quartz实现动态配置定时任务源码

    本篇文章将详细探讨如何在SpringBoot项目中整合Quartz,实现动态配置定时任务。 首先,我们需要在SpringBoot项目中引入Quartz的相关依赖。在`pom.xml`文件中添加以下Maven依赖: ```xml &lt;groupId&gt;org.spring...

    spring、Quart 源码

    标题中的“spring、Quart 源码”指的是Spring框架和Quartz库的源代码,这两个都是Java领域中用于构建应用程序的重要组件。Spring是全面的企业级应用开发框架,而Quartz则是一个强大的任务调度库。 Spring框架是Java...

    quart-z 两次执行问题

    在处理Quart-Z定时任务时,可能会遇到一个常见的问题:定时任务被意外执行了两次。此问题的出现通常与部署环境有关,尤其是当应用程序部署在Tomcat服务器上时更为显著。本文将根据提供的文档链接以及上下文信息,...

    Quart simple

    【标题】:“Quart 简单介绍” 在IT领域,Quartz是一个非常著名的开源作业调度框架,主要用于在Java应用程序中执行定时任务。它提供了一个全面且可高度定制化的调度解决方案,使得开发者可以方便地安排和执行周期性...

    spring quart 例子

    我们也可以通过Spring的`Scheduler`接口动态添加、修改或删除任务。 5. **日志和监控** 为了调试和监控任务的执行情况,通常会在Job中添加日志记录。同时,Quartz还提供了Web界面(如`Quartz Enterprise Scheduler...

    spring管理quart实现定时器功能

    本示例讲解了如何利用Spring管理Quartz实现定时任务,无需在`web.xml`中配置,使定时任务的管理和监控更加方便。 首先,我们要理解Quartz的核心概念。Quartz是一个开源的作业调度框架,允许程序创建和管理作业以及...

    APScheduler基于Quartz的一个Python定时任务框架

    APScheduler是Python编程语言中的一款强大且灵活的定时任务库,它允许开发者安排任务在特定的时间点执行,或者按照一定的时间间隔重复执行。基于Quartz的设计理念,APScheduler提供了多种调度策略,包括基于日期、...

    quartzDaoImp.rar_quartz_spring定时_定时器

    Quartz 是一个开源的作业调度框架,常用于Java应用程序中实现定时任务的管理。Spring框架则提供了对Quartz的集成,使得在Spring应用中配置和使用Quartz变得更加方便。本项目"quartzDaoImp.rar_quartz_spring定时_...

    spring+quary定时存储jar包

    本文将深入探讨这两个框架以及如何结合使用它们实现定时存储功能。 首先,让我们了解一下 Spring 框架。Spring 是一个开源的 Java 应用程序框架,它提供了全面的编程和配置模型,用于简化 Java 应用开发。Spring 的...

    spring quartz定时任务demo

    在Java开发中,Spring框架与Quartz库的结合使用能够帮助开发者实现强大的定时任务功能。本文将详细解析“spring quartz定时任务demo”这个项目,它提供了实现Java定时任务的实例,便于快速理解和应用。 首先,...

    spring quartz通过xml配置实现定时任务demo

    在Java开发中,Spring框架与Quartz库的结合使用能够帮助开发者轻松地创建和管理定时任务。本示例将详细讲解如何通过XML配置文件来实现Spring与Quartz的集成,从而构建一个定时任务Demo。 首先,Quartz是开源的作业...

    spring2.0学习笔记+spring定时任务

    在提供的压缩包文件“prj_quart”中,我们可以预期找到与Spring 2.0定时任务相关的代码示例、配置文件或教程文档。这些资源可以帮助读者深入理解如何在Spring 2.0中配置和使用Quartz,以及如何编写和调度定时任务。...

    基于 springboot+mybatis_+shiro + redis+activiti+quarts+quart76.zip

    【标题】"基于 springboot+mybatis_+shiro + redis+activiti+quarts+quart76.zip" 涵盖了多个关键的技术栈,这些技术在现代企业级应用开发中扮演着重要角色。下面将分别详细介绍这些技术及其在实际应用中的作用。 *...

    Storm Executor Task QuartzJob流式框架定时任务处理框架

    在IT行业中,定时任务处理是不可或缺的一部分,尤其是在大数据和分布式计算领域。本篇文章将深入探讨“Storm Executor Task QuartzJob流式框架定时任务处理框架”,它结合了强大的Storm流处理系统和QuartzJob的...

    基于 springboot+mybatis_+shiro + redis+activiti+quarts+quart.zip

    它允许开发者创建、调度和执行作业,以实现定时任务或周期性任务的自动化。在项目中,Quartz可能用于计划和执行后台任务,如数据同步、报表生成等。 **7. Quart** 这里可能是打错了,正确应该是Quartz,因为"quarts...

    SpringBoot多数据源连接,Druid监控双数据源 Demo

    自己临时搭建的SpringBoot多数据源连接报表,同时连接Mysql,Oracle数据库Mybatis连接,Druid监控双数据源,Quart执行定时任务,PageHelper分页,前端延用Layui

    数据库实现本地备份

    在Spring Quart定时任务中,我们可以编写一个方法,使用Java的`ProcessBuilder`或`Runtime.getRuntime().exec()`来调用`mysqldump`命令,参数包括数据库名、用户名、密码等,生成备份文件。例如: ```java Process...

Global site tag (gtag.js) - Google Analytics