Java Code Examples for org.apache.curator.x.discovery.ServiceInstance#getServiceType()

The following examples show how to use org.apache.curator.x.discovery.ServiceInstance#getServiceType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InstanceCleanup.java    From curator with Apache License 2.0 6 votes vote down vote up
private void checkService(String name)
{
    try
    {
        Collection<ServiceInstance<Object>>     instances = discovery.queryForInstances(name);
        for ( ServiceInstance<Object> instance : instances )
        {
            if ( instance.getServiceType() == ServiceType.STATIC )
            {
                if ( (System.currentTimeMillis() - instance.getRegistrationTimeUTC()) > instanceRefreshMs )
                {
                    discovery.unregisterService(instance);
                }
            }
        }
    }
    catch ( Exception e )
    {
        ThreadUtils.checkInterrupted(e);
        log.error(String.format("GC for service: %s", name), e);
    }
}
 
Example 2
Source File: ServiceDiscoveryImpl.java    From xian with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
protected void internalRegisterService(ServiceInstance<T> service) throws Exception
{
    byte[] bytes = serializer.serialize(service);
    String path = pathForInstance(service.getName(), service.getId());

    final int MAX_TRIES = 2;
    boolean isDone = false;
    for ( int i = 0; !isDone && (i < MAX_TRIES); ++i )
    {
        try
        {
CreateMode mode;
switch (service.getServiceType()) {
case DYNAMIC:
	mode = CreateMode.EPHEMERAL;
	break;
case DYNAMIC_SEQUENTIAL:
	mode = CreateMode.EPHEMERAL_SEQUENTIAL;
	break;
default:
	mode = CreateMode.PERSISTENT;
	break;
}
            client.create().creatingParentContainersIfNeeded().withMode(mode).forPath(path, bytes);
client.setData().forPath(pathForName(service.getName()), serviceDefinitionSerializer.serialize(service.getPayload()));
            isDone = true;
        }
        catch ( KeeperException.NodeExistsException e )
        {
            client.delete().forPath(path);  // must delete then re-create so that watchers fire
        }
    }
}
 
Example 3
Source File: JsonInstanceSerializer.java    From curator with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(ServiceInstance<T> instance) throws Exception
{
    if ( compatibleSerializationMode )
    {
        OldServiceInstance<T> compatible = new OldServiceInstance<T>(instance.getName(), instance.getId(), instance.getAddress(), instance.getPort(), instance.getSslPort(), instance.getPayload(), instance.getRegistrationTimeUTC(), instance.getServiceType(), instance.getUriSpec());
        return mapper.writeValueAsBytes(compatible);
    }
    return mapper.writeValueAsBytes(instance);
}
 
Example 4
Source File: ServiceDiscoveryImpl.java    From curator with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
protected void internalRegisterService(ServiceInstance<T> service) throws Exception
{
    byte[] bytes = serializer.serialize(service);
    String path = pathForInstance(service.getName(), service.getId());

    final int MAX_TRIES = 2;
    boolean isDone = false;
    for ( int i = 0; !isDone && (i < MAX_TRIES); ++i )
    {
        try
        {
CreateMode mode;
switch (service.getServiceType()) {
case DYNAMIC:
	mode = CreateMode.EPHEMERAL;
	break;
case DYNAMIC_SEQUENTIAL:
	mode = CreateMode.EPHEMERAL_SEQUENTIAL;
	break;
default:
	mode = CreateMode.PERSISTENT;
	break;
}
            client.create().creatingParentContainersIfNeeded().withMode(mode).forPath(path, bytes);
            isDone = true;
        }
        catch ( KeeperException.NodeExistsException e )
        {
            client.delete().forPath(path);  // must delete then re-create so that watchers fire
        }
    }
}