使用curator實現(xiàn)zookeeper鎖服務(wù)的示例分享
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("所有任務(wù)執(zhí)行完畢");
client.close();
System.out.println("客戶端關(guān)閉。。。。");
}
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();
}
}
}
}
相關(guān)文章
Java結(jié)合Vue項目打包并進(jìn)行服務(wù)器部署
本文主要介紹了Java結(jié)合Vue項目打包并進(jìn)行服務(wù)器部署,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
SpringData Repository Bean方法定義規(guī)范代碼實例
這篇文章主要介紹了SpringData Repository Bean方法定義規(guī)范代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
Spring模塊詳解之Spring ORM和Spring Transaction詳解
Spring ORM 是 Spring 框架的模塊之一,旨在簡化與 JPA、Hibernate、JDO 等 ORM 工具的集成,通過提供統(tǒng)一的 API 和模板類,如 HibernateTemplate 和 JpaTemplate,Spring ORM 使開發(fā)者可以更便捷地執(zhí)行數(shù)據(jù)庫操作,感興趣的朋友跟隨小編一起看看吧2024-09-09
Java中replace與replaceAll的區(qū)別與測試
replace和replaceAll是JAVA中常用的替換字符的方法,下面這篇文章主要給大家介紹了關(guān)于Java中replace與replaceAll的區(qū)別與測試,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
使用自定義注解進(jìn)行restful請求參數(shù)的校驗方式
這篇文章主要介紹了使用自定義注解進(jìn)行restful請求參數(shù)的校驗方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

