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
019package org.apache.oozie.dependency.hcat;
020
021import java.util.List;
022import java.util.Map;
023
024import javax.jms.Message;
025
026import org.apache.hive.hcatalog.messaging.AddPartitionMessage;
027import org.apache.hive.hcatalog.messaging.HCatEventMessage;
028import org.apache.hive.hcatalog.messaging.jms.MessagingUtils;
029import org.apache.oozie.jms.MessageHandler;
030import org.apache.oozie.service.PartitionDependencyManagerService;
031import org.apache.oozie.service.Services;
032import org.apache.oozie.util.XLog;
033
034public class HCatMessageHandler implements MessageHandler {
035
036    private static XLog LOG = XLog.getLog(HCatMessageHandler.class);
037
038    private final String server;
039    private final PartitionDependencyManagerService pdmService;
040
041    public HCatMessageHandler(String server) {
042        this.server = server;
043        this.pdmService = Services.get().get(PartitionDependencyManagerService.class);
044    }
045
046    /**
047     * Process JMS message produced by HCat.
048     *
049     * @param msg : to be processed
050     */
051    @Override
052    public void process(Message msg) {
053        try {
054            HCatEventMessage hcatMsg = MessagingUtils.getMessage(msg);
055            if (hcatMsg.getEventType().equals(HCatEventMessage.EventType.ADD_PARTITION)) {
056                // Parse msg components
057                AddPartitionMessage partMsg = (AddPartitionMessage) hcatMsg;
058                String db = partMsg.getDB();
059                String table = partMsg.getTable();
060                LOG.info("Partition available event: db [{0}]  table [{1}] partitions [{2}]", db, table,
061                        partMsg.getPartitions());
062                List<Map<String, String>> partitions = partMsg.getPartitions();
063                for (int i = 0; i < partitions.size(); i++) {
064                    pdmService.partitionAvailable(this.server, db, table, partitions.get(i));
065                }
066            }
067            else {
068                LOG.debug("Ignoring message of event type [{0}] ", hcatMsg.getEventType());
069            }
070        }
071        catch (Exception e) {
072            LOG.warn("Error processing JMS message", e);
073        }
074    }
075
076}