All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taobao.metamorphosis.metaslave.MetamorphosisSlaveBroker Maven / Gradle / Ivy

/*
 * (C) 2007-2012 Alibaba Group Holding Limited.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * Authors:
 *   wuhua 
 */
package com.taobao.metamorphosis.metaslave;

import java.util.Properties;

import org.apache.commons.lang.StringUtils;

import com.taobao.metamorphosis.AbstractBrokerPlugin;
import com.taobao.metamorphosis.exception.MetaClientException;
import com.taobao.metamorphosis.server.assembly.MetaMorphosisBroker;
import com.taobao.metamorphosis.server.utils.SlaveConfig;


/**
 * ????һ????meta masterͬ????Ϣ???ݵ?slaver
 * 
 * @author ?޻?
 * @since 2011-6-23 ????01:54:11
 */

public class MetamorphosisSlaveBroker extends AbstractBrokerPlugin {

    private SubscribeHandler subscribeHandler;


    @Override
    public void init(final MetaMorphosisBroker metaMorphosisBroker, final Properties props) {
        this.broker = metaMorphosisBroker;
        this.props = props;

        this.putSlaveProperties(this.broker, this.props);

        if (!this.broker.getMetaConfig().isSlave()) {
            throw new SubscribeMasterMessageException("Could not start as a slave broker");
        }

        try {
            this.subscribeHandler = new SubscribeHandler(this.broker);
        }
        catch (final MetaClientException e) {
            throw new SubscribeMasterMessageException("Create subscribeHandler failed", e);
        }
    }


    private void putSlaveProperties(final MetaMorphosisBroker broker, final Properties props) {
        SlaveConfig slaveConfig = new SlaveConfig();
        slaveConfig.setSlaveId(Integer.parseInt(props.getProperty("slaveId")));
        if (StringUtils.isNotBlank(props.getProperty("slaveGroup"))) {
            slaveConfig.setSlaveGroup(props.getProperty("slaveGroup"));
        }
        else {
            // set default slave group
            slaveConfig.setSlaveGroup(slaveConfig.getSlaveGroup() + "_" + slaveConfig.getSlaveId());
        }
        if (StringUtils.isNotBlank(props.getProperty("slaveMaxDelayInMills"))) {
            slaveConfig.setSlaveMaxDelayInMills(Integer.parseInt(props.getProperty("slaveMaxDelayInMills")));
        }
        if (StringUtils.isNotBlank(props.getProperty("autoSyncMasterConfig"))) {
            slaveConfig.setAutoSyncMasterConfig(Boolean.valueOf(props.getProperty("autoSyncMasterConfig")));
        }
        broker.getMetaConfig().setSlaveConfig(slaveConfig);

        // ????????BrokerIdPath???Ա?ע?ᵽslave??·??
        broker.getBrokerZooKeeper().resetBrokerIdPath();
    }


    @Override
    public String name() {
        return "metaslave";
    }


    @Override
    public void start() {
        this.subscribeHandler.start();
    }


    @Override
    public void stop() {
        this.subscribeHandler.shutdown();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy