SSM 整合 quartz JDBC方式实现job动态增删改查记录
虽然网上有很多资料,但是都不够系统,本文记录下自己的整合过程.
1. 搭建一个SSM项目,此处略.
2. 按照quartz官方要求,建立quartz相关的数据库和表,相关sql语句如下:
- /*
- Navicat MySQL Data Transfer
- Source Server : local_mysql
- Source Server Version : 50537
- Source Host : localhost:3306
- Source Database : quartz_test
- Target Server Type : MYSQL
- Target Server Version : 50537
- File Encoding : 65001
- Date: 2017-03-07 15:11:44
- */
- SET FOREIGN_KEY_CHECKS=0;
- -- ----------------------------
- -- Table structure for qrtz_blob_triggers
- -- ----------------------------
- create database quartz_test;
- DROP TABLE IF EXISTS `qrtz_blob_triggers`;
- CREATE TABLE `qrtz_blob_triggers` (
- `sched_name` varchar(120) NOT NULL,
- `trigger_name` varchar(80) NOT NULL,
- `trigger_group` varchar(80) NOT NULL,
- `blob_data` blob,
- PRIMARY KEY (`sched_name`,`trigger_name`,`trigger_group`),
- CONSTRAINT `qrtz_blob_triggers_ibfk_1` FOREIGN KEY (`sched_name`, `trigger_name`, `trigger_group`) REFERENCES `qrtz_triggers` (`sched_name`, `trigger_name`, `trigger_group`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of qrtz_blob_triggers
- -- ----------------------------
- -- ----------------------------
- -- Table structure for qrtz_calendars
- -- ----------------------------
- DROP TABLE IF EXISTS `qrtz_calendars`;
- CREATE TABLE `qrtz_calendars` (
- `sched_name` varchar(120) NOT NULL,
- `calendar_name` varchar(80) NOT NULL,
- `calendar` blob NOT NULL,
- PRIMARY KEY (`calendar_name`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of qrtz_calendars
- -- ----------------------------
- -- ----------------------------
- -- Table structure for qrtz_cron_triggers
- -- ----------------------------
- DROP TABLE IF EXISTS `qrtz_cron_triggers`;
- CREATE TABLE `qrtz_cron_triggers` (
- `sched_name` varchar(120) NOT NULL,
- `trigger_name` varchar(80) NOT NULL,
- `trigger_group` varchar(80) NOT NULL,
- `cron_expression` varchar(120) NOT NULL,
- `time_zone_id` varchar(80) DEFAULT NULL,
- PRIMARY KEY (`sched_name`,`trigger_name`,`trigger_group`),
- CONSTRAINT `qrtz_cron_triggers_ibfk_1` FOREIGN KEY (`sched_name`, `trigger_name`, `trigger_group`) REFERENCES `qrtz_triggers` (`sched_name`, `trigger_name`, `trigger_group`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of qrtz_cron_triggers
- -- ----------------------------
- -- ----------------------------
- -- Table structure for qrtz_fired_triggers
- -- ----------------------------
- DROP TABLE IF EXISTS `qrtz_fired_triggers`;
- CREATE TABLE `qrtz_fired_triggers` (
- `sched_name` varchar(120) NOT NULL,
- `entry_id` varchar(95) NOT NULL,
- `trigger_name` varchar(80) NOT NULL,
- `trigger_group` varchar(80) NOT NULL,
- `instance_name` varchar(80) NOT NULL,
- `fired_time` bigint(20) NOT NULL,
- `sched_time` bigint(20) NOT NULL,
- `priority` int(11) NOT NULL,
- `state` varchar(16) NOT NULL,
- `job_name` varchar(80) DEFAULT NULL,
- `job_group` varchar(80) DEFAULT NULL,
- `is_nonconcurrent` int(11) DEFAULT NULL,
- `requests_recovery` int(11) DEFAULT NULL,
- PRIMARY KEY (`sched_name`,`entry_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of qrtz_fired_triggers
- -- ----------------------------
- INSERT INTO `qrtz_fired_triggers` VALUES ('dufy_test', 'NON_CLUSTERED1487230171387', 'trigger1', 'group1', 'NON_CLUSTERED', '', '', '', 'ACQUIRED', null, null, '', '');
- -- ----------------------------
- -- Table structure for qrtz_job_details
- -- ----------------------------
- DROP TABLE IF EXISTS `qrtz_job_details`;
- CREATE TABLE `qrtz_job_details` (
- `sched_name` varchar(120) NOT NULL,
- `job_name` varchar(80) NOT NULL,
- `job_group` varchar(80) NOT NULL,
- `description` varchar(120) DEFAULT NULL,
- `job_class_name` varchar(128) NOT NULL,
- `is_durable` int(11) NOT NULL,
- `is_nonconcurrent` int(11) NOT NULL,
- `is_update_data` int(11) NOT NULL,
- `requests_recovery` int(11) NOT NULL,
- `job_data` blob,
- PRIMARY KEY (`sched_name`,`job_name`,`job_group`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of qrtz_job_details
- -- ----------------------------
- -- ----------------------------
- -- Table structure for qrtz_locks
- -- ----------------------------
- DROP TABLE IF EXISTS `qrtz_locks`;
- CREATE TABLE `qrtz_locks` (
- `sched_name` varchar(120) NOT NULL,
- `lock_name` varchar(40) NOT NULL,
- PRIMARY KEY (`sched_name`,`lock_name`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of qrtz_locks
- -- ----------------------------
- INSERT INTO `qrtz_locks` VALUES ('dufy_test', 'TRIGGER_ACCESS');
- INSERT INTO `qrtz_locks` VALUES ('quartzScheduler', 'TRIGGER_ACCESS');
- INSERT INTO `qrtz_locks` VALUES ('scheduler', 'TRIGGER_ACCESS');
- -- ----------------------------
- -- Table structure for qrtz_paused_trigger_grps
- -- ----------------------------
- DROP TABLE IF EXISTS `qrtz_paused_trigger_grps`;
- CREATE TABLE `qrtz_paused_trigger_grps` (
- `sched_name` varchar(120) NOT NULL,
- `trigger_group` varchar(80) NOT NULL,
- PRIMARY KEY (`sched_name`,`trigger_group`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of qrtz_paused_trigger_grps
- -- ----------------------------
- -- ----------------------------
- -- Table structure for qrtz_scheduler_state
- -- ----------------------------
- DROP TABLE IF EXISTS `qrtz_scheduler_state`;
- CREATE TABLE `qrtz_scheduler_state` (
- `sched_name` varchar(120) NOT NULL,
- `instance_name` varchar(80) NOT NULL,
- `last_checkin_time` bigint(20) NOT NULL,
- `checkin_interval` bigint(20) NOT NULL,
- PRIMARY KEY (`sched_name`,`instance_name`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of qrtz_scheduler_state
- -- ----------------------------
- -- ----------------------------
- -- Table structure for qrtz_simple_triggers
- -- ----------------------------
- DROP TABLE IF EXISTS `qrtz_simple_triggers`;
- CREATE TABLE `qrtz_simple_triggers` (
- `sched_name` varchar(120) NOT NULL,
- `trigger_name` varchar(80) NOT NULL,
- `trigger_group` varchar(80) NOT NULL,
- `repeat_count` bigint(20) NOT NULL,
- `repeat_interval` bigint(20) NOT NULL,
- `times_triggered` bigint(20) NOT NULL,
- PRIMARY KEY (`sched_name`,`trigger_name`,`trigger_group`),
- CONSTRAINT `qrtz_simple_triggers_ibfk_1` FOREIGN KEY (`sched_name`, `trigger_name`, `trigger_group`) REFERENCES `qrtz_triggers` (`sched_name`, `trigger_name`, `trigger_group`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of qrtz_simple_triggers
- -- ----------------------------
- -- ----------------------------
- -- Table structure for qrtz_simprop_triggers
- -- ----------------------------
- DROP TABLE IF EXISTS `qrtz_simprop_triggers`;
- CREATE TABLE `qrtz_simprop_triggers` (
- `sched_name` varchar(120) NOT NULL,
- `TRIGGER_NAME` varchar(200) NOT NULL,
- `TRIGGER_GROUP` varchar(200) NOT NULL,
- `STR_PROP_1` varchar(512) DEFAULT NULL,
- `STR_PROP_2` varchar(512) DEFAULT NULL,
- `STR_PROP_3` varchar(512) DEFAULT NULL,
- `INT_PROP_1` int(11) DEFAULT NULL,
- `INT_PROP_2` int(11) DEFAULT NULL,
- `LONG_PROP_1` bigint(20) DEFAULT NULL,
- `LONG_PROP_2` bigint(20) DEFAULT NULL,
- `DEC_PROP_1` decimal(13,4) DEFAULT NULL,
- `DEC_PROP_2` decimal(13,4) DEFAULT NULL,
- `BOOL_PROP_1` varchar(1) DEFAULT NULL,
- `BOOL_PROP_2` varchar(1) DEFAULT NULL,
- PRIMARY KEY (`sched_name`,`TRIGGER_NAME`,`TRIGGER_GROUP`),
- CONSTRAINT `qrtz_simprop_triggers_ibfk_1` FOREIGN KEY (`sched_name`, `TRIGGER_NAME`, `TRIGGER_GROUP`) REFERENCES `qrtz_triggers` (`sched_name`, `trigger_name`, `trigger_group`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of qrtz_simprop_triggers
- -- ----------------------------
- -- ----------------------------
- -- Table structure for qrtz_triggers
- -- ----------------------------
- DROP TABLE IF EXISTS `qrtz_triggers`;
- CREATE TABLE `qrtz_triggers` (
- `sched_name` varchar(120) NOT NULL,
- `trigger_name` varchar(80) NOT NULL,
- `trigger_group` varchar(80) NOT NULL,
- `job_name` varchar(80) NOT NULL,
- `job_group` varchar(80) NOT NULL,
- `description` varchar(120) DEFAULT NULL,
- `next_fire_time` bigint(20) DEFAULT NULL,
- `prev_fire_time` bigint(20) DEFAULT NULL,
- `priority` int(11) DEFAULT NULL,
- `trigger_state` varchar(16) NOT NULL,
- `trigger_type` varchar(8) NOT NULL,
- `start_time` bigint(20) NOT NULL,
- `end_time` bigint(20) DEFAULT NULL,
- `calendar_name` varchar(80) DEFAULT NULL,
- `misfire_instr` smallint(6) DEFAULT NULL,
- `job_data` blob,
- PRIMARY KEY (`sched_name`,`trigger_name`,`trigger_group`),
- KEY `sched_name` (`sched_name`,`job_name`,`job_group`),
- CONSTRAINT `qrtz_triggers_ibfk_1` FOREIGN KEY (`sched_name`, `job_name`, `job_group`) REFERENCES `qrtz_job_details` (`sched_name`, `job_name`, `job_group`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of qrtz_triggers
- -- ----------------------------
- -- ----------------------------
- -- Table structure for user_t
- -- ----------------------------
- DROP TABLE IF EXISTS `user_t`;
- CREATE TABLE `user_t` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `user_name` varchar(40) NOT NULL,
- `password` varchar(255) NOT NULL,
- `age` int(4) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of user_t
- -- ----------------------------
- INSERT INTO `user_t` VALUES ('', 'admin', '', '');
3. 在resources目录下放入 quartz.properties 文件
相关内容:
- # Default Properties file for use by StdSchedulerFactory
- # to create a Quartz Scheduler Instance, if a different
- # properties file is not explicitly specified.
- #
- #============================================================================
- # Configure Main Scheduler Properties
- #============================================================================
- org.quartz.scheduler.instanceName: quartzScheduler
- org.quartz.scheduler.instanceId = AUTO
- org.quartz.scheduler.rmi.export: false
- org.quartz.scheduler.rmi.proxy: false
- org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
- #============================================================================
- # Configure ThreadPool
- #============================================================================
- org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
- org.quartz.threadPool.threadCount: 2
- org.quartz.threadPool.threadPriority: 5
- org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
- org.quartz.jobStore.misfireThreshold: 60000
- #============================================================================
- # Configure JobStore
- #============================================================================
- #default config
- #org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
- #\u6301\u4e45\u5316\u914d\u7f6e
- org.quartz.jobStore.class:org.quartz.impl.jdbcjobstore.JobStoreTX
- #org.quartz.jobStore.driverDelegateClass:org.quartz.impl.jdbcjobstore.StdJDBCDelegate
- #org.quartz.jobStore.useProperties:true
- #============================================================================
- #havent cluster spring
- #============================================================================
- org.quartz.jobStore.isClustered = false
- #\u6570\u636e\u5e93\u8868\u524d\u7f00
- #org.quartz.jobStore.tablePrefix:qrtz_
- org.quartz.jobStore.dataSource:qzDS
- #============================================================================
- # Configure Datasources
- #============================================================================
- #JDBC\u9a71\u52a8 Sping\u53bb\u7ba1\u7406dataSource \uff0c\u8fd9\u91cc\u4e0d\u5728\u914d\u7f6e\u6570\u636e\u6e90\u4fe1\u606f
- org.quartz.dataSource.qzDS.driver:com.mysql.jdbc.Driver
- #下面的数据库配置换成自己的
- org.quartz.dataSource.qzDS.URL:jdbc:mysql://xx.xx.xx.xx:3306/quartz_test
- org.quartz.dataSource.qzDS.user:root
- org.quartz.dataSource.qzDS.password:123456
- #该配置启用会报错
- #org.quartz.dataSource.qzDS.maxConnection:10
3. 自定义jobfactory---此处主要是为了让Job实例能够被纳入spring容器,从而使用诸如@Service @Repository之类的spring组件
- @Component("customJobFactory")
- public class CustomJobFactory extends SpringBeanJobFactory {
- @Autowired
- private AutowireCapableBeanFactory beanFactory;
- @Override
- protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
- Object instance = super.createJobInstance(bundle);
- beanFactory.autowireBean(instance);
- return instance;
- }
- }
4. spring-quartz.xml 此配置文件主要是提供一个scheduler对象供容器里的bean引用
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- 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-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
- <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<!-- 此处要配置可用datasource-->- <property name="dataSource" ref ="dataSource" />
- <property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
- <property name="configLocation" value="classpath:quartz.properties"/>
- <!--可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了 -->
- <property name="overwriteExistingJobs" value="true" />
- <!--必须的,QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动 -->
- <property name="startupDelay" value="10" />
- <!-- 设置自动启动
- <property name="autoStartup" value="true" />
- -->
<!-- 配置自定义的jobfactory -->
- <property name="jobFactory" ref="customJobFactory"/>
- </bean>
- </beans>
5. spring-mybatis.xml 此文件引入spring-quartz.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd">
- <!-- 扫描有@component @service的类包并将其归入bean内 -->
- <context:component-scan base-package="com.jd.ptest.urlmonitor">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
- <!--数据源-->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"
- p:driverClassName="${jdbc.driver}"
- p:url="${jdbc.url}"
- p:username="${jdbc.username}"
- p:password="${jdbc.password}">
- </bean>
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="mapperLocations" value="classpath:mybatis/mapping/*"/>
- <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
- </bean>
- <!-- 扫描直接使用mapper接口类操作dao -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.xx.xx.xx.dao"/>
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
- </bean>
- <!-- 使用提供的template -->
- <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
- <constructor-arg ref="sqlSessionFactory"></constructor-arg>
- </bean>
- <!-- 配置事务管理器-->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <!-- 配置基于注解的声明式事务-->
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <!-- 加载计划任务类 -->
- <import resource="spring-quartz.xml"/>
- <!-- 加载spring中的资源文件 必须放在最后! -->
- <import resource="spring-profiles.xml"/>
- </beans>
6. job类 用于干活的类
- public class ScheduleRegionJob {
- Log log = LogFactory.getLog(this.getClass());
- @Value("${baseUrl}")
- private String baseUrl;
- @Value("${token}")
- private String token;
- @Value("${pageSize}")
- private String pageSize;
- @Value("${region}")
- private String region;
- //此处可以引用spring容器的bean
- @Autowired
- @SuppressWarnings("all")
- private IRegionService regionService;
- @Autowired
- @SuppressWarnings("all")
- private IMailService mailService;
- /**
- * 定时同步机房信息的方法
- */
- public void sheduledSyncJob() {
- try
- {
- log.info("开始启动计划任务sheduledSyncJob!" );
- regionService.syncRegionByUrl(baseUrl, token,Integer.parseInt(pageSize));
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public void sheduledMonitorJob(){
- boolean isSuc =false;
- try
- {
- String summaryId = regionService.doRegionMonitor(region);
- //发送报警邮件
- mailService.productMail(summaryId);
- isSuc = true;
- } catch (Exception e)
- {
- isSuc = false;
- e.printStackTrace();
- }finally
- {
- if(isSuc){
- log.error( "Region monitor successful!");
- }else{
- log.error("Region monitor failed!");
- }
- }
- }
- }
7. QuartzServiceImpl quartz job 操作类
- @Service("quartzService")
- public class QuartzServiceImpl implements IQuartzService {
- @Autowired
- @SuppressWarnings("all")
- private Scheduler quartzScheduler;
- @Override
- public void addJob(String jobName, String jobGroupName, String triggerName,
- String triggerGroupName, Class cls, String cron) {
- try
- {
- // 获取调度器
- Scheduler sched = quartzScheduler;
- // 创建一项作业
- JobDetail job = JobBuilder.newJob(cls)
- .withIdentity(jobName, jobGroupName).build();
- // 创建一个触发器
- CronTrigger trigger = TriggerBuilder.newTrigger()
- .withIdentity(triggerName, triggerGroupName)
- .withSchedule(CronScheduleBuilder.cronSchedule(cron))
- .build();
- // 告诉调度器使用该触发器来安排作业
- sched.scheduleJob(job, trigger);
- // 启动
- if (!sched.isShutdown())
- {
- sched.start();
- }
- } catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
- /**
- * 修改定时器任务信息
- */
- @Override
- public boolean modifyJob(String oldjobName, String oldjobGroup, String oldtriggerName, String oldtriggerGroup, String jobName, String jobGroup,
- String triggerName, String triggerGroup, String cron) {
- try
- {
- Scheduler sched = quartzScheduler;
- CronTrigger trigger = (CronTrigger) sched.getTrigger(TriggerKey
- .triggerKey(oldtriggerName, oldtriggerGroup));
- if (trigger == null)
- {
- return false;
- }
- JobKey jobKey = JobKey.jobKey(oldjobName, oldjobGroup);
- TriggerKey triggerKey = TriggerKey.triggerKey(oldtriggerName,
- oldtriggerGroup);
- JobDetail job = sched.getJobDetail(jobKey);
- Class jobClass = job.getJobClass();
- // 停止触发器
- sched.pauseTrigger(triggerKey);
- // 移除触发器
- sched.unscheduleJob(triggerKey);
- // 删除任务
- sched.deleteJob(jobKey);
- addJob(jobName, jobGroup, triggerName, triggerGroup, jobClass,
- cron);
- return true;
- } catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
- @Override
- public boolean modifyJobCron(String triggerName, String triggerGroupName,
- String cronExpression) throws Exception {
- boolean flag = false;
- try
- {
- Scheduler sched = quartzScheduler;
- TriggerKey triggerKey = TriggerKey.triggerKey(triggerName,triggerGroupName);
- CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);
- if (trigger == null)
- {
- flag = false;
- }
- String oldExpression = trigger.getCronExpression();
- if (!oldExpression.equalsIgnoreCase(cronExpression))
- {
- TriggerBuilder builder = TriggerBuilder
- .newTrigger()
- .withIdentity(triggerName,triggerGroupName);
- CronTrigger newTrigger = (CronTrigger)builder
- .startNow()
- .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
- .build();
- // 重启触发器
- sched.rescheduleJob(triggerKey,newTrigger);
- flag = true;
- }
- } catch (Exception e)
- {
- flag = false;
- throw e;
- }finally
- {
- return flag;
- }
- }
- @Override
- public void removeJob(String jobName, String jobGroupName,
- String triggerName, String triggerGroupName) {
- try
- {
- Scheduler sched = quartzScheduler;
- // 停止触发器
- sched.pauseTrigger(TriggerKey.triggerKey(triggerName,
- triggerGroupName));
- // 移除触发器
- sched.unscheduleJob(TriggerKey.triggerKey(triggerName,
- triggerGroupName));
- // 删除任务
- sched.deleteJob(JobKey.jobKey(jobName, jobGroupName));
- } catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
- @Override
- public void startSchedule() {
- try
- {
- Scheduler sched = quartzScheduler;
- sched.start();
- } catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
- @Override
- public void shutdownSchedule() {
- try
- {
- Scheduler sched = quartzScheduler;
- if (!sched.isShutdown())
- {
- sched.shutdown();
- }
- } catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
- @Override
- public void pauseJob(String jobName, String jobGroupName) {
- try
- {
- quartzScheduler.pauseJob(JobKey.jobKey(jobName, jobGroupName));
- } catch (SchedulerException e)
- {
- e.printStackTrace();
- }
- }
- @Override
- public void resumeJob(String jobName, String jobGroupName) {
- try
- {
- quartzScheduler.resumeJob(JobKey.jobKey(jobName, jobGroupName));
- } catch (SchedulerException e)
- {
- e.printStackTrace();
- }
- }
- }
上面基本演示了quartz相关的一些核心代码和配置,其他的controller及jsp比较简单就不贴了...
SSM 整合 quartz JDBC方式实现job动态增删改查记录的更多相关文章
- java jdbc 连接mysql数据库 实现增删改查
好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打 ...
- JDBC实现最简单的增删改查
好久没写博客了,今天刚进入一家公司实习,在实习这段期间想把自己所学的东西通过博客记录下来 今天上午简单回顾了一下用JDBC实现最简单的增删改查 废话不多说,接下来就说明怎么用JDBC实现最简单的增删改 ...
- 通过jdbc连接MySql数据库的增删改查操作
一.获取数据库连接 要对MySql数据库内的数据进行增删改查等操作,首先要获取数据库连接 JDBC:Java中连接数据库方式 具体操作如下: 获取数据库连接的步骤: 1.先定义好四个参数 String ...
- Maven+SSM框架,实现单表简单的增删改查
目录 1.创建web Maven项目 2.创建java源码文件和resources资源文件 3.创建数据库配置文件:jdbc.properties 4.项目总体目录: 5.添加spring配置文件:a ...
- JDBC概述及JDBC完成对Oracle的增删改查
什么是JDBC JDBC(Java Data Base Connectivity,Java数据库连接),是一种用于执行SQL语句的Java API,为多种关系数据库提供统一访问.它由一组用Java语言 ...
- SSM框架之MyBatis框架实现简单的增删改查
MyBatis框架介绍 MyBatis是一个优秀的数据持久层框架,在实体类和SQL语句之间建立映射关系是一种半自动化的ORM实现,其封装性要低于Hibernate,性能优越,并且小巧,简单易学,应用也 ...
- 【转载】通过JDBC对MySQL数据库的增删改查
通过JDBC进行简单的增删改查(以MySQL为例) 目录 前言:什么是JDBC 一.准备工作(一):MySQL安装配置和基础学习 二.准备工作(二):下载数据库对应的jar包并导入 三.JDBC基本操 ...
- MYSQL 之 JDBC(七):增删改查(五) DAO设计模式
Data Access Object,数据访问对象 what:访问数据信息的类.包含了对数据的CRUD(create.read.update.delete,增删改查)操作,而不包含任何业务相关的信息. ...
- 针对MySql封装的JDBC通用框架类(包含增删改查、JavaBean反射原理)
package com.DBUtils; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.Dri ...
随机推荐
- 无可奈何的开始了jquery的“奇淫技巧”
转载请注明出处: https://home.cnblogs.com/u/zhiyong-ITNote/ 修改一个已有的项目,主要是前端方面,一般的项目后端都是处理好了的,不需要改也不能改,除非特殊需求 ...
- Xamarin Essentials教程设备信息DeviceInfo
Xamarin Essentials教程设备信息DeviceInfo 设备信息包括设备类型.设备种类和操作系统.应用程序通过获取设备信息,可以判断当前程序是否适合在该设备上运行.例如,优酷提供TV ...
- javascript的严格模式和正常模式
在js代码中加入"use strict";就相当于进入严格模式,严格模式改变了语法和运行时行为,将隐藏的问题转化为错误. js的严格模式有以下特点: 一.变量声明 全局变量必须显示 ...
- gcc 各种参数
1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Compilation) 2.3汇编(Assembly) 2.4连接(Linking) 3多个程序文件的编译 4检错 5库文件连接 5.1编译成 ...
- django rest_framework 序列化组件详解
为什么要用序列化组件 当我们做前后端分离的项目,我们前后端交互一般都选择JSON数据格式,JSON是一个轻量级的数据交互格式. 那么我们给前端数据的时候都要转成json格式,那就需要对我们从数据库拿到 ...
- centos7下使用yum安装pip
centos7下使用yum安装pip 首先安装epel扩展源: yum -y install epel-release 更新完成之后,就可安装pip: yum -y install python-pi ...
- [P1005][NOIP2007] 矩阵取数游戏 (DP+高精)
我不会高精…… 也不会DP…… 这道题即考高精又考DP…… 我要死了 给一个不是高精的代码(当然不能满分) #include<cstdio> #include<iostream> ...
- Java 基础 集合框架
Java中的集合从类的继承和接口的实现结构来说,可以分为两大类: 1 继承自Collection接口,包含List.Set和Queue等接口和实现类. 2 继承自Map接口,主要包含哈希表相关的集合类 ...
- PAT-Top1001. Battle Over Cities - Hard Version (35)
在敌人占领之前由城市和公路构成的图是连通图.在敌人占领某个城市之后所有通往这个城市的公路就会被破坏,接下来可能需要修复一些其他被毁坏的公路使得剩下的城市能够互通.修复的代价越大,意味着这个城市越重要. ...
- keras用vgg16做图像分类
实际上我只是提供一个模版而已,代码应该很容易看得懂,label是存在一个csv里面的,图片是在一个文件夹里面的 没GPU的就不用尝试了,训练一次要很久很久... ## import libaries ...