package cn.superdesk.uniorder.common.lock.zookeeper.config.curator;

import cn.superdesk.uniorder.common.lock.zookeeper.config.ZookeeperConfigurer;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CuratorFrameworkConfigurer {

	@Autowired
	private ZookeeperConfigurer zookeeperConfigurer;

	@Bean(initMethod = "start", destroyMethod = "close")
	public CuratorFramework curatorFramework() {
		return CuratorFrameworkFactory.newClient(
				zookeeperConfigurer.getServers(),
				zookeeperConfigurer.getSessionTimeoutMs(),
				zookeeperConfigurer.getConnectionTimeoutMs(),
				new RetryNTimes(zookeeperConfigurer.getRetryCount(), zookeeperConfigurer.getElapsedTimeMs()));
	}

}
