001/*-
002 * #%L
003 * HAPI FHIR JPA Server - Batch2 Task Processor
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.batch2.config;
021
022import ca.uhn.fhir.IHapiBootOrder;
023import ca.uhn.fhir.batch2.coordinator.JobDefinitionRegistry;
024import ca.uhn.fhir.batch2.model.JobDefinition;
025import ca.uhn.fhir.util.Logs;
026import org.slf4j.Logger;
027import org.springframework.beans.factory.annotation.Autowired;
028import org.springframework.context.ApplicationContext;
029import org.springframework.context.event.ContextRefreshedEvent;
030import org.springframework.context.event.EventListener;
031import org.springframework.core.annotation.Order;
032
033import java.util.Map;
034
035public class Batch2JobRegisterer {
036        private static final Logger ourLog = Logs.getBatchTroubleshootingLog();
037
038        @Autowired
039        private ApplicationContext myApplicationContext;
040
041        // The timing of this call is sensitive.  It needs to be called after all the job definition beans have been created
042        // but before any jobs are run.  E.g. ValidationDataInitializerSvcImpl can start a REINDEX job, so we use an
043        // EventListener
044        // so we know all the JobDefinition beans have been created, but we use @Order(IHapiBootOrder.ADD_JOB_DEFINITIONS)
045        // to ensure it is called
046        // before any other EventListeners that might start a job.
047        @EventListener(classes = ContextRefreshedEvent.class)
048        @Order(IHapiBootOrder.ADD_JOB_DEFINITIONS)
049        public void start() {
050                Map<String, JobDefinition> batchJobs = myApplicationContext.getBeansOfType(JobDefinition.class);
051                JobDefinitionRegistry jobRegistry = myApplicationContext.getBean(JobDefinitionRegistry.class);
052
053                for (Map.Entry<String, JobDefinition> next : batchJobs.entrySet()) {
054                        JobDefinition<?> jobDefinition = next.getValue();
055                        ourLog.info(
056                                        "Registering Batch2 Job Definition: {} / {}",
057                                        jobDefinition.getJobDefinitionId(),
058                                        jobDefinition.getJobDefinitionVersion());
059                        jobRegistry.addJobDefinition(jobDefinition);
060                }
061        }
062}