Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7147bad
commit 282e394
Showing
4 changed files
with
254 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
spring.elasticjob.serverList=127.0.0.1:2181 | ||
simpleJob.cron=0 0 /1 * * ? * | ||
simpleJob.cron=0/5 * * * * ? | ||
simpleJob.shardingTotalCount=2 | ||
simpleJob.shardingParameters=0=A,1=B | ||
spring.elasticjob.namespace=elastic-job |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package zookeeper; | ||
|
||
import org.apache.curator.RetryPolicy; | ||
import org.apache.curator.framework.CuratorFramework; | ||
import org.apache.curator.framework.CuratorFrameworkFactory; | ||
import org.apache.curator.framework.recipes.cache.TreeCache; | ||
import org.apache.curator.framework.recipes.leader.LeaderLatch; | ||
import org.apache.curator.retry.ExponentialBackoffRetry; | ||
import org.apache.zookeeper.CreateMode; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Hello 管理员. | ||
* | ||
* @author skywalker | ||
*/ | ||
public class ZookeeperTest { | ||
|
||
private CuratorFramework client; | ||
|
||
@Before | ||
public void before() { | ||
RetryPolicy retryPolicy = new ExponentialBackoffRetry(500, 4); | ||
client = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy); | ||
client.start(); | ||
System.out.println("Zookeeper连接成功."); | ||
} | ||
|
||
/** | ||
* 当有一个事件产生时,每一个Listener是否都会收到通知?答案:是. | ||
*/ | ||
@Test | ||
public void testListener() throws Exception { | ||
prepareTestData(); | ||
System.out.println("创建数据成功"); | ||
|
||
createListeners(); | ||
System.out.println("创建Listener成功"); | ||
|
||
client.delete().forPath("/skywalker/age"); | ||
System.out.println("删除节点成功"); | ||
|
||
TimeUnit.SECONDS.sleep(10); | ||
} | ||
|
||
@Test | ||
public void testLeaderElection() throws Exception { | ||
LeaderLatch latch = new LeaderLatch(client, "/skywalker/latch1"); | ||
latch.start(); | ||
latch.await(); | ||
System.out.println("Latch1选举结果: " + latch.hasLeadership()); | ||
|
||
LeaderLatch latch2 = new LeaderLatch(client, "/skywalker/latch2"); | ||
latch2.start(); | ||
latch2.await(); | ||
System.out.println("Latch2选举结果: " + latch.hasLeadership()); | ||
|
||
latch.close(); | ||
latch2.close(); | ||
} | ||
|
||
private void prepareTestData() throws Exception { | ||
client.create().creatingParentContainersIfNeeded() | ||
.withMode(CreateMode.EPHEMERAL) | ||
.forPath("/skywalker/name", "name".getBytes()); | ||
|
||
client.create().creatingParentContainersIfNeeded() | ||
.withMode(CreateMode.EPHEMERAL) | ||
.forPath("/skywalker/age", new byte[] {20}); | ||
} | ||
|
||
private void createListeners() throws Exception { | ||
TreeCache treeCache = new TreeCache(client, "/skywalker"); | ||
|
||
treeCache.getListenable().addListener((client, event) -> System.out.println("Listener1-事件类型: " + event)); | ||
|
||
treeCache.getListenable().addListener(((client, event) -> System.out.println("Listener2-事件类型: " + event))); | ||
|
||
treeCache.start(); | ||
} | ||
|
||
@After | ||
public void after() { | ||
this.client.close(); | ||
} | ||
|
||
} |