Search in sources :

Example 1 with ServiceCacheListener

use of org.apache.curator.x.discovery.details.ServiceCacheListener in project xian by happyyangyuan.

the class TestServiceCache method testUpdate.

@Test
public void testUpdate() throws Exception {
    List<Closeable> closeables = Lists.newArrayList();
    try {
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).build();
        closeables.add(discovery);
        discovery.start();
        final CountDownLatch latch = new CountDownLatch(1);
        ServiceCache<String> cache = discovery.serviceCacheBuilder().name("test").build();
        closeables.add(cache);
        ServiceCacheListener listener = new ServiceCacheListener() {

            @Override
            public void cacheChanged() {
                latch.countDown();
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }
        };
        cache.addListener(listener);
        cache.start();
        instance = ServiceInstance.<String>builder().payload("changed").name("test").port(10064).id(instance.getId()).build();
        discovery.updateService(instance);
        Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
        Assert.assertEquals(cache.getInstances().size(), 1);
        Assert.assertEquals(cache.getInstances().get(0).getPayload(), instance.getPayload());
    } finally {
        Collections.reverse(closeables);
        for (Closeable c : closeables) {
            CloseableUtils.closeQuietly(c);
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Closeable(java.io.Closeable) ServiceCacheListener(org.apache.curator.x.discovery.details.ServiceCacheListener) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 2 with ServiceCacheListener

use of org.apache.curator.x.discovery.details.ServiceCacheListener in project xian by happyyangyuan.

the class TestServiceCache method testInitialLoad.

@Test
public void testInitialLoad() throws Exception {
    List<Closeable> closeables = Lists.newArrayList();
    try {
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/discovery").client(client).build();
        closeables.add(discovery);
        discovery.start();
        ServiceCache<String> cache = discovery.serviceCacheBuilder().name("test").build();
        closeables.add(cache);
        final CountDownLatch latch = new CountDownLatch(3);
        ServiceCacheListener listener = new ServiceCacheListener() {

            @Override
            public void cacheChanged() {
                latch.countDown();
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }
        };
        cache.addListener(listener);
        cache.start();
        ServiceInstance<String> instance1 = ServiceInstance.<String>builder().payload("test").name("test").port(10064).build();
        ServiceInstance<String> instance2 = ServiceInstance.<String>builder().payload("test").name("test").port(10065).build();
        ServiceInstance<String> instance3 = ServiceInstance.<String>builder().payload("test").name("test").port(10066).build();
        discovery.registerService(instance1);
        discovery.registerService(instance2);
        discovery.registerService(instance3);
        Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
        ServiceCache<String> cache2 = discovery.serviceCacheBuilder().name("test").build();
        closeables.add(cache2);
        cache2.start();
        Assert.assertEquals(cache2.getInstances().size(), 3);
    } finally {
        Collections.reverse(closeables);
        for (Closeable c : closeables) {
            CloseableUtils.closeQuietly(c);
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Closeable(java.io.Closeable) ServiceCacheListener(org.apache.curator.x.discovery.details.ServiceCacheListener) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 3 with ServiceCacheListener

use of org.apache.curator.x.discovery.details.ServiceCacheListener in project xian by happyyangyuan.

the class TestServiceCache method testExecutorServiceIsInvoked.

@Test
public void testExecutorServiceIsInvoked() throws Exception {
    List<Closeable> closeables = Lists.newArrayList();
    try {
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/discovery").client(client).build();
        closeables.add(discovery);
        discovery.start();
        ExecuteCalledWatchingExecutorService exec = new ExecuteCalledWatchingExecutorService(Executors.newSingleThreadExecutor());
        Assert.assertFalse(exec.isExecuteCalled());
        ServiceCache<String> cache = discovery.serviceCacheBuilder().name("test").executorService(exec).build();
        closeables.add(cache);
        cache.start();
        final Semaphore semaphore = new Semaphore(0);
        ServiceCacheListener listener = new ServiceCacheListener() {

            @Override
            public void cacheChanged() {
                semaphore.release();
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }
        };
        cache.addListener(listener);
        ServiceInstance<String> instance1 = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        discovery.registerService(instance1);
        Assert.assertTrue(semaphore.tryAcquire(10, TimeUnit.SECONDS));
        Assert.assertTrue(exec.isExecuteCalled());
    } finally {
        Collections.reverse(closeables);
        for (Closeable c : closeables) {
            CloseableUtils.closeQuietly(c);
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) ExecuteCalledWatchingExecutorService(org.apache.curator.test.ExecuteCalledWatchingExecutorService) Closeable(java.io.Closeable) ServiceCacheListener(org.apache.curator.x.discovery.details.ServiceCacheListener) Semaphore(java.util.concurrent.Semaphore) ConnectionState(org.apache.curator.framework.state.ConnectionState) Test(org.testng.annotations.Test)

Example 4 with ServiceCacheListener

use of org.apache.curator.x.discovery.details.ServiceCacheListener in project xian by happyyangyuan.

the class TestServiceCache method testCache.

@Test
public void testCache() throws Exception {
    List<Closeable> closeables = Lists.newArrayList();
    try {
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/discovery").client(client).build();
        closeables.add(discovery);
        discovery.start();
        ServiceCache<String> cache = discovery.serviceCacheBuilder().name("test").build();
        closeables.add(cache);
        cache.start();
        final Semaphore semaphore = new Semaphore(0);
        ServiceCacheListener listener = new ServiceCacheListener() {

            @Override
            public void cacheChanged() {
                semaphore.release();
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }
        };
        cache.addListener(listener);
        ServiceInstance<String> instance1 = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        ServiceInstance<String> instance2 = ServiceInstance.<String>builder().payload("thing").name("test").port(10065).build();
        discovery.registerService(instance1);
        Assert.assertTrue(semaphore.tryAcquire(10, TimeUnit.SECONDS));
        discovery.registerService(instance2);
        Assert.assertTrue(semaphore.tryAcquire(3, TimeUnit.SECONDS));
        ServiceInstance<String> instance3 = ServiceInstance.<String>builder().payload("thing").name("another").port(10064).build();
        discovery.registerService(instance3);
        // should not get called for a different getGroup
        Assert.assertFalse(semaphore.tryAcquire(3, TimeUnit.SECONDS));
    } finally {
        Collections.reverse(closeables);
        for (Closeable c : closeables) {
            CloseableUtils.closeQuietly(c);
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Closeable(java.io.Closeable) ServiceCacheListener(org.apache.curator.x.discovery.details.ServiceCacheListener) Semaphore(java.util.concurrent.Semaphore) ConnectionState(org.apache.curator.framework.state.ConnectionState) Test(org.testng.annotations.Test)

Aggregations

Closeable (java.io.Closeable)4 CuratorFramework (org.apache.curator.framework.CuratorFramework)4 ConnectionState (org.apache.curator.framework.state.ConnectionState)4 RetryOneTime (org.apache.curator.retry.RetryOneTime)4 ServiceCacheListener (org.apache.curator.x.discovery.details.ServiceCacheListener)4 Test (org.testng.annotations.Test)4 CountDownLatch (java.util.concurrent.CountDownLatch)2 Semaphore (java.util.concurrent.Semaphore)2 ExecuteCalledWatchingExecutorService (org.apache.curator.test.ExecuteCalledWatchingExecutorService)1