001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.oozie.sla.listener;
019    
020    import java.util.Date;
021    
022    import org.apache.hadoop.conf.Configuration;
023    import org.apache.oozie.client.CoordinatorAction;
024    import org.apache.oozie.client.event.JobEvent;
025    import org.apache.oozie.event.BundleJobEvent;
026    import org.apache.oozie.event.CoordinatorActionEvent;
027    import org.apache.oozie.event.CoordinatorJobEvent;
028    import org.apache.oozie.event.WorkflowActionEvent;
029    import org.apache.oozie.event.WorkflowJobEvent;
030    import org.apache.oozie.event.listener.JobEventListener;
031    import org.apache.oozie.service.ServiceException;
032    import org.apache.oozie.service.Services;
033    import org.apache.oozie.sla.service.SLAService;
034    import org.apache.oozie.util.XLog;
035    
036    public class SLAJobEventListener extends JobEventListener {
037    
038        @Override
039        public void init(Configuration conf) {
040        }
041    
042        @Override
043        public void destroy() {
044        }
045    
046        @Override
047        public void onWorkflowJobEvent(WorkflowJobEvent event) {
048            sendEventToSLAService(event, event.getStatus().toString());
049        }
050    
051        @Override
052        public void onWorkflowActionEvent(WorkflowActionEvent event) {
053            sendEventToSLAService(event, event.getStatus().toString());
054        }
055    
056        @Override
057        public void onCoordinatorJobEvent(CoordinatorJobEvent event) {
058            sendEventToSLAService(event, event.getStatus().toString());
059        }
060    
061        @Override
062        public void onCoordinatorActionEvent(CoordinatorActionEvent event) {
063            sendEventToSLAService(event, event.getStatus().toString());
064        }
065    
066        @Override
067        public void onBundleJobEvent(BundleJobEvent wje) {
068        }
069    
070        private void sendEventToSLAService(JobEvent event, String status) {
071            Date startTime = event.getStartTime();
072            Date endTime = event.getEndTime();
073            try {
074                Services.get().get(SLAService.class)
075                        .addStatusEvent(event.getId(), status, event.getEventStatus(), startTime, endTime);
076            }
077            catch (ServiceException se) {
078                XLog.getLog(SLAService.class).error("Exception happened while sending Job-Status event for SLA", se);
079            }
080        }
081    
082    }