Aharoni, Pavel (pa0916) | 8c70f07 | 2018-11-18 00:07:12 +0200 | [diff] [blame^] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 6 | * ================================================================================
|
| 7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 | * you may not use this file except in compliance with the License.
|
| 9 | * You may obtain a copy of the License at
|
| 10 | *
|
| 11 | * http://www.apache.org/licenses/LICENSE-2.0
|
| 12 | *
|
| 13 | * Unless required by applicable law or agreed to in writing, software
|
| 14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 | * See the License for the specific language governing permissions and
|
| 17 | * limitations under the License.
|
| 18 | * ============LICENSE_END=========================================================
|
| 19 | */
|
| 20 | package org.onap.osam.job.impl;
|
| 21 |
|
| 22 | import com.google.common.collect.ImmutableMap;
|
| 23 | import lombok.extern.slf4j.Slf4j;
|
| 24 | import org.onap.osam.common.exception.GenericUncheckedException;
|
| 25 | import org.onap.osam.job.dao.job.JobStatus;
|
| 26 | import org.onap.osam.job.IJobsDataAccessService;
|
| 27 | import org.onap.osam.job.command.JobCommandFactory;
|
| 28 | import org.quartz.JobBuilder;
|
| 29 | import org.quartz.JobDataMap;
|
| 30 | import org.quartz.JobDetail;
|
| 31 | import org.quartz.Scheduler;
|
| 32 | import org.quartz.SchedulerException;
|
| 33 | import org.quartz.SimpleTrigger;
|
| 34 | import org.quartz.TriggerBuilder;
|
| 35 | import org.springframework.beans.factory.annotation.Autowired;
|
| 36 | import org.springframework.scheduling.quartz.SchedulerFactoryBean;
|
| 37 | import org.springframework.stereotype.Component;
|
| 38 |
|
| 39 | import javax.annotation.PostConstruct;
|
| 40 |
|
| 41 | import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
|
| 42 |
|
| 43 | @Slf4j
|
| 44 | @Component
|
| 45 | public class JobSchedulerInitializer {
|
| 46 |
|
| 47 | private IJobsDataAccessService jobsDataAccessService;
|
| 48 | private SchedulerFactoryBean schedulerFactoryBean;
|
| 49 | private JobCommandFactory jobCommandFactory;
|
| 50 |
|
| 51 | @Autowired
|
| 52 | public JobSchedulerInitializer(
|
| 53 | IJobsDataAccessService jobsDataAccessService,
|
| 54 | SchedulerFactoryBean schedulerFactoryBean,
|
| 55 | JobCommandFactory JobCommandFactory
|
| 56 | ) {
|
| 57 | this.jobsDataAccessService = jobsDataAccessService;
|
| 58 | this.schedulerFactoryBean = schedulerFactoryBean;
|
| 59 | this.jobCommandFactory = JobCommandFactory;
|
| 60 |
|
| 61 | }
|
| 62 |
|
| 63 | @PostConstruct
|
| 64 | public void init() {
|
| 65 | scheduleJobWorker(JobStatus.PENDING, 1);
|
| 66 | scheduleJobWorker(JobStatus.CREATING, 1);
|
| 67 | scheduleJobWorker(JobStatus.IN_PROGRESS, 1);
|
| 68 | scheduleJobWorker(JobStatus.RESOURCE_IN_PROGRESS, 1);
|
| 69 | }
|
| 70 |
|
| 71 | private void scheduleJobWorker(JobStatus topic, int intervalInSeconds) {
|
| 72 | final Scheduler scheduler = schedulerFactoryBean.getScheduler();
|
| 73 | JobDetail jobDetail = JobBuilder.newJob().ofType(JobWorker.class)
|
| 74 | .withIdentity("AsyncWorkersJob" + topic)
|
| 75 | .withDescription("Job that run async worker for " + topic)
|
| 76 | .setJobData(new JobDataMap(ImmutableMap.of(
|
| 77 | "jobsDataAccessService", jobsDataAccessService,
|
| 78 | "jobCommandFactory", jobCommandFactory,
|
| 79 | "topic", topic
|
| 80 | )))
|
| 81 | .build();
|
| 82 | SimpleTrigger asyncWorkerTrigger = TriggerBuilder.newTrigger().forJob(jobDetail)
|
| 83 | .withIdentity("AsyncWorkersTrigger" + topic)
|
| 84 | .withDescription("Trigger to run async worker for " + topic)
|
| 85 | .withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(intervalInSeconds))
|
| 86 | .build();
|
| 87 | try {
|
| 88 | scheduler.scheduleJob(jobDetail, asyncWorkerTrigger);
|
| 89 | } catch (SchedulerException e) {
|
| 90 | log.error("Failed to schedule trigger for async worker jobs: {}", e.getMessage());
|
| 91 | throw new GenericUncheckedException(e);
|
| 92 | }
|
| 93 | }
|
| 94 | }
|