使用curator实现zookeeper锁服务的示例分享

所属分类: 软件编程 / java 阅读数: 56
收藏 0 赞 0 分享

复制代码 代码如下:

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import com.netflix.curator.RetryPolicy;
import com.netflix.curator.framework.CuratorFramework;
import com.netflix.curator.framework.CuratorFrameworkFactory;
import com.netflix.curator.framework.recipes.locks.InterProcessMutex;
import com.netflix.curator.retry.ExponentialBackoffRetry;

public class TestCuratorLock {

 /**
  * @param args
  * @throws InterruptedException
  */
 public static void main(String[] args) throws InterruptedException {
  // TODO Auto-generated method stub

  CountDownLatch latch = new CountDownLatch(5);

  String zookeeperConnectionString = "localhost:2181,localhost:2182,localhost:2183";
  RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
  CuratorFramework client = CuratorFrameworkFactory.newClient(
    zookeeperConnectionString, retryPolicy);
  client.start();
  System.out.println("客户端启动。。。。");
  ExecutorService exec = Executors.newCachedThreadPool();

  for (int i = 0; i < 5; i++) {
   exec.submit(new MyLock("client" + i, client, latch));
  }

  exec.shutdown();
  latch.await();
  System.out.println("所有任务执行完毕");

  client.close();

  System.out.println("客户端关闭。。。。");

 }

 static class MyLock implements Runnable {

  private String name;

  private CuratorFramework client;

  private CountDownLatch latch;

  public MyLock(String name, CuratorFramework client, CountDownLatch latch) {
   this.name = name;
   this.client = client;
   this.latch = latch;
  }

  public String getName() {
   return name;
  }

  public void setName(String name) {
   this.name = name;
  }

  @Override
  public void run() {
   // TODO Auto-generated method stub
   InterProcessMutex lock = new InterProcessMutex(client,
     "/test_group");
   try {
    if (lock.acquire(120, TimeUnit.SECONDS)) {
     try {
      // do some work inside of the critical section here
      System.out.println("----------" + this.name
        + "获得资源----------");
      System.out.println("----------" + this.name
        + "正在处理资源----------");
      Thread.sleep(10 * 1000);
      System.out.println("----------" + this.name
        + "资源使用完毕----------");
      latch.countDown();
     } finally {
      lock.release();
      System.out.println("----------" + this.name
        + "释放----------");
     }
    }
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   }
 }
 }

更多精彩内容其他人还在看

java 中maven pom.xml文件教程详解

这篇文章主要介绍了java 中maven pom.xml文件教程详解,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

spring boot整合netty的实现方法

这篇文章主要介绍了spring boot整合netty的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Netty与Spring Boot的整合实现

这篇文章主要介绍了Netty与Spring Boot的整合的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring动态加载bean后调用实现方法解析

这篇文章主要介绍了Spring动态加载bean后调用实现方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现画图板上画一条直线

这篇文章主要为大家详细介绍了java实现画图板上画一条直线,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java通过python命令执行DataX任务的实例

今天小编就为大家分享一篇Java通过python命令执行DataX任务的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

springBoot集成redis的key,value序列化的相关问题

这篇文章主要介绍了springBoot集成redis的key,value序列化的相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

java实现登录案例

这篇文章主要为大家详细介绍了java实现登录案例的相关代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java解决请求跨域的两种方法

这篇文章主要为大家详细介绍了java解决请求跨域的两种方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringBoot集成Beetl后统一处理页面异常的方法

这篇文章主要介绍了SpringBoot集成Beetl后统一处理页面异常的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多