001/**
002 * Copyright 2005-2017 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.lifecycle.BaseLifecycle;
019import org.kuali.rice.kew.batch.ExternalActnListNotificationService;
020import org.kuali.rice.kew.service.KEWServiceLocator;
021
022import java.util.concurrent.Executors;
023import java.util.concurrent.ScheduledExecutorService;
024import java.util.concurrent.ScheduledFuture;
025import java.util.concurrent.TimeUnit;
026
027/**
028 * This lifecycle will only be started if the rice.kew.externalActnListNotification.enabled parameter is set to true.
029 * If it is set up to run, it will notify an external action list if anything changes on the KREW_ACTN_ITM_T table.
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public class ExternalActnListNotificationLifecycle extends BaseLifecycle {
034
035        protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ExternalActnListNotificationLifecycle.class);
036
037        private ScheduledExecutorService scheduledExecutor;
038        private ScheduledFuture future;
039
040        public void start() throws Exception {
041                scheduledExecutor = Executors.newScheduledThreadPool(1);
042                final ExternalActnListNotificationService alcPoller = KEWServiceLocator.getExternalActnListNotificationService();
043                LOG.info("Starting the external action list notification service.  Polling at " +
044                                alcPoller.getExternalActnListNotificationPollIntervalSeconds() + " second intervals!");
045
046                future = scheduledExecutor.scheduleWithFixedDelay(alcPoller,
047                                alcPoller.getExternalActnListNotificationInitialDelaySeconds(),
048                                alcPoller.getExternalActnListNotificationPollIntervalSeconds(), TimeUnit.SECONDS);
049
050                super.start();
051        }
052
053        public void stop() throws Exception {
054                if (isStarted()) {
055                        LOG.warn("Shutting down the external action list notification service");
056                        try {
057                                if (future != null) {
058                                        if (!future.cancel(false)) {
059                                                LOG.warn("Failed to cancel the external action list notification service.");
060                                        }
061                                        future = null;
062                                }
063                                if (scheduledExecutor != null) {
064                                        scheduledExecutor.shutdownNow();
065                                        scheduledExecutor = null;
066                                }
067                        } finally {
068                                super.stop();
069                        }
070                }
071        }
072
073}