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