use of org.apache.curator.retry.RetryNTimes in project microservices by pwillhan.
the class ZookeeperServiceDiscovery method getGeolocationServiceProvider.
private static ServiceProvider<Object> getGeolocationServiceProvider() throws Exception {
if (geolocationServiceProvider == null) {
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("192.168.99.100:2181", new RetryNTimes(5, 1000));
curatorFramework.start();
ServiceDiscovery<Object> serviceDiscovery = ServiceDiscoveryBuilder.builder(Object.class).basePath("com.packt.microservices").client(curatorFramework).build();
serviceDiscovery.start();
geolocationServiceProvider = serviceDiscovery.serviceProviderBuilder().serviceName("geolocation").build();
geolocationServiceProvider.start();
}
return geolocationServiceProvider;
}
use of org.apache.curator.retry.RetryNTimes in project fabric8 by jboss-fuse.
the class ServiceFactoryTest method infraUp.
@Before
public void infraUp() throws Exception {
int tickTime = 500;
int numConnections = 5000;
File dir = new File("target", "zookeeper" + random.nextInt()).getAbsoluteFile();
server = new ZooKeeperServer(dir, dir, tickTime);
standaloneServerFactory = createFactory(0, numConnections);
zkPort = standaloneServerFactory.getLocalPort();
System.setProperty("zookeeper.url", "localhost:" + zkPort);
standaloneServerFactory.startup(server);
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + zkPort).sessionTimeoutMs(5000).retryPolicy(new RetryNTimes(5000, 1000));
curator = builder.build();
LOG.debug("Starting curator " + curator);
curator.start();
}
use of org.apache.curator.retry.RetryNTimes in project fabric8 by jboss-fuse.
the class ServiceFactoryTest method testDiscoveryOnRestartCurator.
@Test
public void testDiscoveryOnRestartCurator() throws Exception {
underTest = new ActiveMQServiceFactory();
underTest.curator = curator;
Properties props = new Properties();
props.put("config", "amq.xml");
props.put("broker-name", "amq");
props.put("group", "amq");
props.put("connectors", "openwire");
props.put("openwire-port", "0");
props.put("container.ip", "localhost");
underTest.updated("b", props);
final AtomicReference<CuratorFramework> curatorFrameworkAtomicReference = new AtomicReference<>(curator);
OsgiFabricDiscoveryAgent osgiFabricDiscoveryAgent = new OsgiFabricDiscoveryAgent(new BundleContext() {
@Override
public String getProperty(String s) {
return null;
}
@Override
public Bundle getBundle() {
return null;
}
@Override
public Bundle installBundle(String s, InputStream inputStream) throws BundleException {
return null;
}
@Override
public Bundle installBundle(String s) throws BundleException {
return null;
}
@Override
public Bundle getBundle(long l) {
return null;
}
@Override
public Bundle[] getBundles() {
return new Bundle[0];
}
@Override
public void addServiceListener(ServiceListener serviceListener, String s) throws InvalidSyntaxException {
}
@Override
public void addServiceListener(ServiceListener serviceListener) {
}
@Override
public void removeServiceListener(ServiceListener serviceListener) {
}
@Override
public void addBundleListener(BundleListener bundleListener) {
}
@Override
public void removeBundleListener(BundleListener bundleListener) {
}
@Override
public void addFrameworkListener(FrameworkListener frameworkListener) {
}
@Override
public void removeFrameworkListener(FrameworkListener frameworkListener) {
}
@Override
public ServiceRegistration<?> registerService(String[] strings, Object o, Dictionary<String, ?> dictionary) {
return null;
}
@Override
public ServiceRegistration<?> registerService(String s, Object o, Dictionary<String, ?> dictionary) {
return null;
}
@Override
public <S> ServiceRegistration<S> registerService(Class<S> aClass, S s, Dictionary<String, ?> dictionary) {
return null;
}
@Override
public ServiceReference<?>[] getServiceReferences(String s, String s1) throws InvalidSyntaxException {
return new ServiceReference<?>[0];
}
@Override
public ServiceReference<?>[] getAllServiceReferences(String s, String s1) throws InvalidSyntaxException {
return new ServiceReference<?>[0];
}
@Override
public ServiceReference<?> getServiceReference(String s) {
return null;
}
@Override
public <S> ServiceReference<S> getServiceReference(Class<S> aClass) {
return null;
}
@Override
public <S> Collection<ServiceReference<S>> getServiceReferences(Class<S> aClass, String s) throws InvalidSyntaxException {
return null;
}
@Override
public <S> S getService(ServiceReference<S> serviceReference) {
return (S) curatorFrameworkAtomicReference.get();
}
@Override
public boolean ungetService(ServiceReference<?> serviceReference) {
return false;
}
@Override
public File getDataFile(String s) {
return null;
}
@Override
public Filter createFilter(String s) throws InvalidSyntaxException {
return null;
}
@Override
public Bundle getBundle(String s) {
return null;
}
});
final LinkedBlockingQueue<DiscoveryEvent> discoveryEvents = new LinkedBlockingQueue<DiscoveryEvent>(10);
osgiFabricDiscoveryAgent.setDiscoveryListener(new DiscoveryListener() {
@Override
public void onServiceAdd(DiscoveryEvent discoveryEvent) {
discoveryEvents.offer(discoveryEvent);
}
@Override
public void onServiceRemove(DiscoveryEvent discoveryEvent) {
}
});
// will call into dummy bundle and get curator
osgiFabricDiscoveryAgent.addingService(null);
osgiFabricDiscoveryAgent.setGroupName("amq");
osgiFabricDiscoveryAgent.start();
DiscoveryEvent event = discoveryEvents.poll(5, TimeUnit.SECONDS);
LOG.info("event: " + event);
assertNotNull("got added service", event);
underTest.deleted("b");
// swap curator ref
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + zkPort).sessionTimeoutMs(15000).retryPolicy(new RetryNTimes(5000, 1000));
curator = builder.build();
LOG.debug("Starting new curator " + curator);
curator.start();
curatorFrameworkAtomicReference.get().close();
curatorFrameworkAtomicReference.set(curator);
// will call into dummy bundle and get new curator ref
osgiFabricDiscoveryAgent.addingService(null);
// start broker again
underTest.curator = curator;
underTest.updated("b", props);
event = discoveryEvents.poll(5, TimeUnit.SECONDS);
LOG.info("new event: " + event);
assertNotNull("got newly added service", event);
underTest.deleted("b");
}
use of org.apache.curator.retry.RetryNTimes in project xian by happyyangyuan.
the class TestFrameworkBackground method testListenerConnectedAtStart.
@Test
public void testListenerConnectedAtStart() throws Exception {
server.stop();
Timing timing = new Timing(2);
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryNTimes(0, 0));
try {
client.start();
final CountDownLatch connectedLatch = new CountDownLatch(1);
final AtomicBoolean firstListenerAction = new AtomicBoolean(true);
final AtomicReference<ConnectionState> firstListenerState = new AtomicReference<ConnectionState>();
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (firstListenerAction.compareAndSet(true, false)) {
firstListenerState.set(newState);
System.out.println("First listener state is " + newState);
}
if (newState == ConnectionState.CONNECTED) {
connectedLatch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
// due to CURATOR-72, this was causing a LOST event to precede the CONNECTED event
client.create().inBackground().forPath("/foo");
server.restart();
Assert.assertTrue(timing.awaitLatch(connectedLatch));
Assert.assertFalse(firstListenerAction.get());
ConnectionState firstconnectionState = firstListenerState.get();
Assert.assertEquals(firstconnectionState, ConnectionState.CONNECTED, "First listener state MUST BE CONNECTED but is " + firstconnectionState);
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.retry.RetryNTimes in project xian by happyyangyuan.
the class TestFrameworkBackground method testRetries.
@Test
public void testRetries() throws Exception {
final int SLEEP = 1000;
final int TIMES = 5;
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryNTimes(TIMES, SLEEP));
try {
client.start();
client.getZookeeperClient().blockUntilConnectedOrTimedOut();
final CountDownLatch latch = new CountDownLatch(TIMES);
final List<Long> times = Lists.newArrayList();
final AtomicLong start = new AtomicLong(System.currentTimeMillis());
((CuratorFrameworkImpl) client).debugListener = new CuratorFrameworkImpl.DebugBackgroundListener() {
@Override
public void listen(OperationAndData<?> data) {
if (data.getOperation().getClass().getName().contains("CreateBuilderImpl")) {
long now = System.currentTimeMillis();
times.add(now - start.get());
start.set(now);
latch.countDown();
}
}
};
server.stop();
client.create().inBackground().forPath("/one");
latch.await();
for (// first one isn't a retry
long elapsed : // first one isn't a retry
times.subList(1, times.size())) {
Assert.assertTrue(elapsed >= SLEEP, elapsed + ": " + times);
}
} finally {
CloseableUtils.closeQuietly(client);
}
}
Aggregations