Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.migo.utils; import com.migo.entity.ScheduleJobEntity; import com.migo.entity.ScheduleJobLogEntity; import com.migo.service.ScheduleJobLogService; import org.apache.commons.lang.StringUtils; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.quartz.QuartzJobBean; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; /** * * * @author * @email fei6751803@163.com */ public class ScheduleJob extends QuartzJobBean { private Logger logger = LoggerFactory.getLogger(getClass()); private ExecutorService service = Executors.newSingleThreadExecutor(); @Override protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { ScheduleJobEntity scheduleJob = (ScheduleJobEntity) jobExecutionContext.getMergedJobDataMap() .get(ScheduleJobEntity.JOB_PARAM_KEY); //?spring bean ScheduleJobLogService scheduleJobLogService = (ScheduleJobLogService) SpringContextUtils .getBean("scheduleJobLogService"); //?? ScheduleJobLogEntity log = new ScheduleJobLogEntity(); log.setJobId(scheduleJob.getJobId()); log.setBeanName(scheduleJob.getBeanName()); log.setMethodName(scheduleJob.getMethodName()); log.setParams(scheduleJob.getParams()); log.setCreateTime(new Date()); // long startTime = System.currentTimeMillis(); try { // logger.info("ID" + scheduleJob.getJobId()); ScheduleRunnable task = new ScheduleRunnable(scheduleJob.getBeanName(), scheduleJob.getMethodName(), scheduleJob.getParams()); Future<?> future = service.submit(task); future.get(); // long times = System.currentTimeMillis() - startTime; log.setTimes((int) times); //? 0? 1 log.setStatus(0); logger.info("ID" + scheduleJob.getJobId() + " " + times + ""); } catch (Exception e) { logger.error("ID" + scheduleJob.getJobId(), e); // long times = System.currentTimeMillis() - startTime; log.setTimes((int) times); //? 0? 1 log.setStatus(1); log.setError(StringUtils.substring(e.toString(), 0, 2000)); } finally { scheduleJobLogService.save(log); } } }