這篇文章主要為大家展示了Spring怎么整合Quartz開發(fā),內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來看看吧。
我們提供的服務(wù)有:網(wǎng)站建設(shè)、網(wǎng)站制作、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、秀山土家族苗族ssl等。為超過千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的秀山土家族苗族網(wǎng)站制作公司
xml文件配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!--加載數(shù)據(jù)庫連接的配置文件--> <!--<context:property-placeholder location="jdbc.properties"></context:property-placeholder>--> <!-- c3p0:數(shù)據(jù)源配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.MySQL.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/quartz?Unicode=true&characterEncoding=UTF-8"/> <property name="user" value="root"/> <property name="password" value="root"/> <property name="initialPoolSize" value="3"/> <property name="minPoolSize" value="2"/> <property name="maxPoolSize" value="10"/> <property name="maxIdleTime" value="60"/> <property name="acquireRetryDelay" value="1000"/> <property name="acquireRetryAttempts" value="10"/> <property name="preferredTestQuery" value="SELECT 1"/> </bean> <bean id="quartzScheduler" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="quartz.properties"></property> <!-- <property name="triggers"></property>--> </bean> </beans>
public class SimpleJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println(new Date()+"執(zhí)行SimpleJob"); } }
public class ApplicationContextTest { public static Scheduler scheduler; public static void main(String[] args) throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext-trigger.xml"); scheduler = (Scheduler) applicationContext.getBean("quartzScheduler"); //SimpleJob simpleJob = new SimpleJob(); scheduler.start(); //從數(shù)據(jù)庫中獲取相應(yīng)的job及調(diào)度信息 //JobDetail jobDetail = scheduler.getJobDetail(new JobKey("trigger1", "trigger1")); //resumeJob(jobDetail.getKey().getName(), jobDetail.getKey().getGroup()); //添加job執(zhí)行 addJob("trigger1", "trigger1", "job1", "job2", "0/20 * * * * ?", SimpleJob.class, new HashMap<>()); Thread.sleep(60 * 1000); //重新設(shè)置調(diào)度時(shí)間 System.out.println("重新設(shè)置調(diào)度時(shí)間"); rescheduleJob("trigger1","trigger1","0/10 * * * * ?"); Thread.sleep(60 * 1000); //暫停調(diào)度 System.out.println("暫停調(diào)度"); pauseJob("trigger1","trigger1"); Thread.sleep(60 * 1000); System.out.println("恢復(fù)調(diào)度"); resumeJob("trigger1","trigger1"); Thread.sleep(60 * 1000); System.out.println("刪除調(diào)度"); removeJob("trigger1","trigger1"); Thread.sleep(60 * 1000); System.out.println(scheduler); } /** * 添加job執(zhí)行 * * @param triggerKeyName * @param triggerKeyGroup * @param jobName * @param jobGroup * @param cronExpression * @param jobClass * @param jobData * @return * @throws Exception */ public static boolean addJob(String triggerKeyName, String triggerKeyGroup, String jobName, String jobGroup, String cronExpression, Class<? extends Job> jobClass, Map<String, Object> jobData) throws Exception { JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(triggerKeyName, triggerKeyGroup).build(); Trigger trigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).withIdentity(triggerKeyName, triggerKeyGroup).build(); if (jobData != null && jobData.size() > 0) { JobDataMap jobDataMap = jobDetail.getJobDataMap(); jobDataMap.putAll(jobData); // JobExecutionContext context.getMergedJobDataMap().get("mailGuid"); } scheduler.scheduleJob(jobDetail, trigger); // if (!scheduler.isShutdown()) { // scheduler.start(); // } return true; } /** * 重新設(shè)置job執(zhí)行 * @param triggerKeyName * @param triggerKeyGroup * @param cronExpression * @return * @throws SchedulerException */ public static boolean rescheduleJob(String triggerKeyName, String triggerKeyGroup, String cronExpression) throws SchedulerException { TriggerKey triggerKey = TriggerKey.triggerKey(triggerKeyName, triggerKeyGroup); if (scheduler.checkExists(triggerKey)) { Trigger trigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).withIdentity(triggerKey).build(); scheduler.rescheduleJob(triggerKey, trigger); } return true; } /** * 刪除job * @param triggerKeyName * @param triggerKeyGroup * @return * @throws SchedulerException */ public static boolean removeJob(String triggerKeyName, String triggerKeyGroup) throws SchedulerException { // TriggerKey : name + group TriggerKey triggerKey = TriggerKey.triggerKey(triggerKeyName, triggerKeyGroup); boolean result = false; if (scheduler.checkExists(triggerKey)) { result = scheduler.unscheduleJob(triggerKey); } return result; } /** * 暫停job * @param triggerKeyName * @param triggerKeyGroup * @return * @throws SchedulerException */ public static boolean pauseJob(String triggerKeyName, String triggerKeyGroup) throws SchedulerException { // TriggerKey : name + group TriggerKey triggerKey = TriggerKey.triggerKey(triggerKeyName, triggerKeyGroup); boolean result = false; if (scheduler.checkExists(triggerKey)) { scheduler.pauseTrigger(triggerKey); result = true; } else { } return result; } /** * 重啟job * @param triggerKeyName * @param triggerKeyGroup * @return * @throws SchedulerException */ public static boolean resumeJob(String triggerKeyName, String triggerKeyGroup) throws SchedulerException { TriggerKey triggerKey = TriggerKey.triggerKey(triggerKeyName, triggerKeyGroup); boolean result = false; if (scheduler.checkExists(triggerKey)) { scheduler.resumeTrigger(triggerKey); result = true; } else { } return result; } }
quart.properties正常配置信息,然后點(diǎn)擊運(yùn)行即可。
本實(shí)例中當(dāng)運(yùn)行的任務(wù)在暫停的情況下,一旦重新恢復(fù),會(huì)將暫停期間的任務(wù)運(yùn)行如圖:
以上就是關(guān)于Spring怎么整合Quartz開發(fā)的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。
當(dāng)前名稱:Spring怎么整合Quartz開發(fā)
URL地址:http://muchs.cn/article48/piophp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計(jì)、企業(yè)網(wǎng)站制作、App設(shè)計(jì)、微信小程序、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)