Java Code Examples for org.apache.curator.x.discovery.ServiceProvider#getInstance()

The following examples show how to use org.apache.curator.x.discovery.ServiceProvider#getInstance() . 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: ZooKeeperDiscovery.java    From soabase with Apache License 2.0 6 votes vote down vote up
@Override
public DiscoveryInstance getInstance(String serviceName)
{
    ServiceInstance<Payload> instance;
    try
    {
        // TODO - validate service name
        ServiceProvider<Payload> provider = providers.get(serviceName);
        instance = provider.getInstance();
        return toSoaInstance(instance);
    }
    catch ( Exception e )
    {
        log.error("Could not service instance: " + serviceName, e);
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: DiscoveryExample.java    From ZKRecipesByExample with Apache License 2.0 6 votes vote down vote up
private static void listRandomInstance(String[] args, ServiceDiscovery<InstanceDetails> serviceDiscovery,
		Map<String, ServiceProvider<InstanceDetails>> providers, String command) throws Exception {
	// this shows how to use a ServiceProvider
	// in a real application you'd create the ServiceProvider early for the
	// service(s) you're interested in
	if (args.length != 1) {
		System.err.println("syntax error (expected random <name>): " + command);
		return;
	}
	String serviceName = args[0];
	ServiceProvider<InstanceDetails> provider = providers.get(serviceName);
	if (provider == null) {
		provider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName).providerStrategy(new RandomStrategy<InstanceDetails>()).build();
		providers.put(serviceName, provider);
		provider.start();
		Thread.sleep(2500); // give the provider time to warm up - in a real
							// application you wouldn't need to do this
	}
	ServiceInstance<InstanceDetails> instance = provider.getInstance();
	if (instance == null) {
		System.err.println("No instances named: " + serviceName);
	} else {
		outputInstance(instance);
	}
}
 
Example 3
Source File: DiscoveryExample.java    From xian with Apache License 2.0 5 votes vote down vote up
private static void listRandomInstance(String[] args, ServiceDiscovery<InstanceDetails> serviceDiscovery, Map<String, ServiceProvider<InstanceDetails>> providers, String command) throws Exception
{
    // this shows how to use a ServiceProvider
    // in a real application you'd create the ServiceProvider early for the group(s) you're interested in

    if ( args.length != 1 )
    {
        System.err.println("syntax error (expected random <name>): " + command);
        return;
    }

    String                              serviceName = args[0];
    ServiceProvider<InstanceDetails>    provider = providers.get(serviceName);
    if ( provider == null )
    {
        provider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName).providerStrategy(new RandomStrategy<InstanceDetails>()).build();
        providers.put(serviceName, provider);
        provider.start();

        Thread.sleep(2500); // give the provider time to warm up - in a real application you wouldn't need to do this
    }

    ServiceInstance<InstanceDetails>    instance = provider.getInstance();
    if ( instance == null )
    {
        System.err.println("No instances named: " + serviceName);
    }
    else
    {
        outputInstance(instance);
    }
}
 
Example 4
Source File: DiscoveryExample.java    From curator with Apache License 2.0 5 votes vote down vote up
private static void listRandomInstance(String[] args, ServiceDiscovery<InstanceDetails> serviceDiscovery, Map<String, ServiceProvider<InstanceDetails>> providers, String command) throws Exception
{
    // this shows how to use a ServiceProvider
    // in a real application you'd create the ServiceProvider early for the service(s) you're interested in

    if ( args.length != 1 )
    {
        System.err.println("syntax error (expected random <name>): " + command);
        return;
    }

    String                              serviceName = args[0];
    ServiceProvider<InstanceDetails>    provider = providers.get(serviceName);
    if ( provider == null )
    {
        provider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName).providerStrategy(new RandomStrategy<InstanceDetails>()).build();
        providers.put(serviceName, provider);
        provider.start();

        Thread.sleep(2500); // give the provider time to warm up - in a real application you wouldn't need to do this
    }

    ServiceInstance<InstanceDetails>    instance = provider.getInstance();
    if ( instance == null )
    {
        System.err.println("No instances named: " + serviceName);
    }
    else
    {
        outputInstance(instance);
    }
}