001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.lifecycle;
017
018import org.kuali.rice.core.api.config.property.ConfigContext;
019import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
020import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
021import org.kuali.rice.kew.batch.XmlPollerService;
022import org.kuali.rice.kew.service.KEWServiceLocator;
023
024import java.util.concurrent.Executors;
025import java.util.concurrent.ScheduledExecutorService;
026import java.util.concurrent.ScheduledFuture;
027import java.util.concurrent.TimeUnit;
028
029public class XmlPipelineLifeCycle extends BaseLifecycle {
030
031        protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(XmlPipelineLifeCycle.class);
032
033        private ScheduledExecutorService scheduledExecutor;
034        private ScheduledFuture future;
035
036        public void start() throws Exception {
037                LOG.info("Configuring XML ingestion pipeline...");
038                scheduledExecutor = Executors.newScheduledThreadPool(1);
039                final XmlPollerService xmlPoller = KEWServiceLocator.getXmlPollerService();
040                LOG.info("Starting XML data loader.  Polling at " + xmlPoller.getPollIntervalSecs() + "-second intervals");
041                if (!ConfigContext.getCurrentContextConfig().getDevMode()) {
042                        future = scheduledExecutor.scheduleWithFixedDelay(xmlPoller, xmlPoller.getInitialDelaySecs(), xmlPoller.getPollIntervalSecs(), TimeUnit.SECONDS);
043                        super.start();
044                }
045        }
046
047        public void stop() throws Exception {
048                if (isStarted()) {
049                        LOG.warn("Shutting down XML file polling timer");
050                        try {
051                                if (future != null) {
052                                        if (!future.cancel(false)) {
053                                                LOG.warn("Failed to cancel the XML Poller service.");
054                                        }
055                                        future = null;
056                                }
057                                if (scheduledExecutor != null) {
058                                        scheduledExecutor.shutdownNow();
059                                        scheduledExecutor = null;
060                                }
061                        } finally {
062                                super.stop();
063                        }
064                }
065        }
066
067}