/* * 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 ro.nextreports.server.aop; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.quartz.SchedulerException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Required; import ro.nextreports.server.domain.SchedulerJob; import ro.nextreports.server.schedule.QuartzJobHandler; /** * @author Decebal Suiu */ @Aspect public class SchedulerJobModifiedAdvice { private static final Logger LOG = LoggerFactory.getLogger(SchedulerJobModifiedAdvice.class); private QuartzJobHandler quartzJobHandler; @Pointcut("target(ro.nextreports.server.service.StorageService)") public void inStorageService() { } @Pointcut("execution(* modifyEntity(..))") public void modifyEntity() { } @Pointcut("args(job, ..)") public void isSchedulerJob(SchedulerJob job) { } @Pointcut("inStorageService() && modifyEntity() && isSchedulerJob(job)") public void schedulerJobModified(SchedulerJob job) { } @AfterReturning("schedulerJobModified(job)") public void onSchedulerJobModified(SchedulerJob job) { try { // TODO it's possible to change only the job name, trigger, ... ? LOG.info("Remove job '" + job.getPath() + "' from quartz"); quartzJobHandler.removeJob(job.getPath()); LOG.info("Add job '" + job.getPath() + "' to quartz"); quartzJobHandler.addJob(job); } catch (SchedulerException e) { LOG.error(e.getMessage(), e); if ((e.getMessage() != null) && e.getMessage().contains("the given trigger will never fire")) { // job will not fire : wrong definition // but we create the job (do not throw exception) // in UI we will see an active job but without nextRun date } else { throw new RuntimeException(e); } } catch (Exception e) { LOG.error(e.getMessage(), e); throw new RuntimeException(e); } } @Required public void setQuartzJobHandler(QuartzJobHandler quartzJobHandler) { this.quartzJobHandler = quartzJobHandler; } }