com.shengpay.commons.bp.zk.ZkTemplate.java Source code

Java tutorial

Introduction

Here is the source code for com.shengpay.commons.bp.zk.ZkTemplate.java

Source

/*
 * Copyright 2012 shengpay.com, Inc. All rights reserved.
 * shengpay.com PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * creator : kuguobing
 * create time : 2012-12-4 ?04:06:59
 */
package com.shengpay.commons.bp.zk;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import com.netflix.curator.framework.CuratorFramework;
import com.netflix.curator.framework.recipes.locks.InterProcessLock;
import com.netflix.curator.framework.recipes.locks.InterProcessMutex;
import com.netflix.curator.utils.ZKPaths;

/**
 * ??Zookeeper?
 * @author kuguobing
 * time : 2012-12-4 ?04:06:59
 */
public class ZkTemplate {
    private static final Logger logger = LoggerFactory.getLogger(ZkTemplate.class);

    /**
     * IOC Curator Zookeeper Client
     */
    private CuratorFramework curator;

    /**
     * ???
     */
    private InterProcessLock locker;

    /**
     * ?
     */
    private String lockPath;

    public void setCurator(CuratorFramework curator) {
        this.curator = curator;
    }

    public void setLockPath(String lockPath) {
        this.lockPath = lockPath;
    }

    public void init() throws Exception {
        Assert.notNull(curator, "ZooKeeperClient can't be null.");
        Assert.notNull(lockPath, "Lock Path can't be null.");

        //?Zookeeper
        if (!this.curator.isStarted()) {
            this.curator.start();
        }

        //??
        locker = new InterProcessMutex(curator, this.lockPath);
    }

    public void destroy() throws Exception {
        if (this.curator != null && this.curator.isStarted())
            this.curator.close();
    }

    /**
     * ZK???
     * @param <V>
     * @param callback
     * @return
     * @throws Exception
     */
    public <V> V doExecute(ZkCallBack<V> callback) throws Exception {
        if (callback == null)
            return null;

        logger.debug("Acquire the lock: {}", this.lockPath);

        if (!this.locker.acquire(30, TimeUnit.SECONDS)) {
            throw new IllegalStateException(" could not acquire the lock: " + this.lockPath);
        }

        try {

            return callback.doInZk(this.curator, this.lockPath);

        } finally {
            logger.debug("Releasing the lock: {}", this.lockPath);
            this.locker.release();
        }
    }

    /**
     * ZK??? - For Each Child Path
     * @param childPath
     * @param callback
     * @return
     * @throws Exception
     */
    public <V> V doExecute(final String childPath, ZkCallBack<V> callback) throws Exception {
        if (callback == null || childPath == null || childPath.trim().length() == 0)
            return null;

        //?Child Path
        String childLockPath = ZKPaths.makePath(lockPath, childPath);
        //??
        InterProcessMutex childLocker = new InterProcessMutex(curator, childLockPath);

        //??Child Path?
        logger.debug("Acquire the lock: {}", childLockPath);

        if (!childLocker.acquire(30, TimeUnit.SECONDS)) {
            throw new IllegalStateException(" could not acquire the lock: " + childLockPath);
        }

        try {

            return callback.doInZk(this.curator, childLockPath);

        } finally {
            logger.debug("Releasing the lock: {}", childLockPath);
            childLocker.release();
        }
    }

}