一、程式如下:
package com.runyi.dataprocessing.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.runyi.common.pojo.RunyiResult;
import com.runyi.dataprocessing.common.TaskInfo;
import com.runyi.dataprocessing.service.DataprocessingService;
@Service
public class DataprocessingServiceImpl implements DataprocessingService{
@Autowired
private Scheduler scheduler;
@Override
public RunyiResult workJob(String dataprocessingchoice) {
//任務調度器
/**
* 開始處理
*/
if(dataprocessingchoice.equals("01.客戶申請資料處理啟動")) {
//判斷系統是否已經有任務啟動
//1.先獲取任務資訊
try {
List<TaskInfo> List_message=getAllJobsInfo();
//假如記錄不為空,執行以下操作
TaskInfo set_data=https://bbs.csdn.net/topics/new TaskInfo();
set_data.setJobName("");
if(List_message!=null&&List_message.size()>0) {
//(1)從獲取陣列中查找是否有jobName為"job1",jobGroup為"group1"的記錄
for(int i=0;i<List_message.size();i++) {
TaskInfo data_list=List_message.get(i);//遍歷記錄
if(data_list.getJobName().equals("job1")&&data_list.getJobGroup().equals("group1")) {
set_data=https://bbs.csdn.net/topics/List_message.get(i); //(2)假如找到job1和group1,則不重復啟動客戶申請資料處理
System.out.print("已經進入假設條件,查看set_data.getJobName:"+((TaskInfo) set_data).getJobGroup()+"\n");
}
}
}
//set_data初始賦值未變,則執行啟動
if(!(set_data.getJobName().equals("job1"))) {
//假如記錄為空,則啟動客戶申請資料處理
try {
openJob(scheduler);
//啟動任務
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
//拋例外
} catch (Exception e) {
e.printStackTrace();
}//獲取任務資訊結束
}else if(dataprocessingchoice.equals("02.客戶申請資料處理關閉")){
try {
String name="job1";
String group="group1";
deleteJob(name,group);
} catch (Exception e) {
e.printStackTrace();
}
}else if(dataprocessingchoice.equals("03.調整處理間融時間為1秒")) {
try {
String name="trigger1";
String group="triggergroup1";
String newTime="0/1 * * * * ?";
modifyJob(name, group, newTime);
} catch (Exception e) {
e.printStackTrace();
}
}else if(dataprocessingchoice.equals("04.調整處理間融時間為2秒")) {
try {
String name="trigger1";
String group="triggergroup1";
String newTime="0/2 * * * * ?";
modifyJob(name, group, newTime);
} catch (Exception e) {
e.printStackTrace();
}
}else if(dataprocessingchoice.equals("05.調整處理間融時間為3秒")) {
try {
String name="trigger1";
String group="triggergroup1";
String newTime="0/3 * * * * ?";
modifyJob(name, group, newTime);
} catch (Exception e) {
e.printStackTrace();
}
}else if(dataprocessingchoice.equals("06.調整處理間融時間為5秒")) {
try {
String name="trigger1";
String group="triggergroup1";
String newTime="0/5 * * * * ?";
modifyJob(name, group, newTime);
} catch (Exception e) {
e.printStackTrace();
}
}else if(dataprocessingchoice.equals("07.調整處理間融時間為7秒")) {
try {
String name="trigger1";
String group="triggergroup1";
String newTime="0/7 * * * * ?";
modifyJob(name, group, newTime);
} catch (Exception e) {
e.printStackTrace();
}
}else if(dataprocessingchoice.equals("08.調整處理間融時間為9秒")) {
try {
String name="trigger1";
String group="triggergroup1";
String newTime="0/9 * * * * ?";
modifyJob(name, group, newTime);
} catch (Exception e) {
e.printStackTrace();
}
}//客戶申請資料處理關閉結束
return RunyiResult.ok();
}
/**
* 01.開啟一個任務
* @param scheduler
*/
private void openJob(Scheduler scheduler){
try {
//1.創建一個JobDetail
JobDetail jobDetail = JobBuilder.newJob(Autoworkaplication.class).withIdentity("job1", "group1").build();
//2.觸發器運算式物件
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/3 * * * * ?");
//3.準備一個觸發器物件
CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "triggergroup1")
.withSchedule(cronScheduleBuilder).build();
//4.開始調度
scheduler.scheduleJob(jobDetail,cronTrigger);
} catch (SchedulerException e) {
e.printStackTrace();
} finally {
}
}
/**
*一般容易忽略,向Spring容器中初始注入scheduler
*/
@Bean
public Scheduler scheduler() throws SchedulerException {
SchedulerFactory schedulerFactoryBean = new StdSchedulerFactory();
return schedulerFactoryBean.getScheduler();
}
}
二、報錯:
嚴重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataprocessingServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.quartz.Scheduler com.runyi.dataprocessing.service.impl.DataprocessingServiceImpl.scheduler; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scheduler' defined in com.runyi.dataprocessing.service.impl.DataprocessingServiceImpl: No matching factory method found: factory bean 'dataprocessingServiceImpl'; factory method 'scheduler()'. Check that a method with the specified name exists and that it is non-static.
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.c
uj5u.com熱心網友回復:
已經解決。在applicationContext-service.xml中添加<bean id ="scheduler" class ="org.springframework.scheduling.quartz.SchedulerFactoryBean"/>,并將 @Beanpublic Scheduler scheduler() throws SchedulerException {
SchedulerFactory schedulerFactoryBean = new StdSchedulerFactory();
return schedulerFactoryBean.getScheduler();
}洗掉,啟動正常,問題解決。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/147282.html
標籤:Java SE
