org.talend.esb.servicelocator.client.internal
Class ServiceLocatorImpl

java.lang.Object
  extended by org.talend.esb.servicelocator.client.internal.ServiceLocatorImpl
All Implemented Interfaces:
ServiceLocator

public class ServiceLocatorImpl
extends Object
implements ServiceLocator

This is the entry point for clients of the Service Locator. To access the Service Locator clients have to first connect to the Service Locator to get a session assigned. Once the connection is established the client will periodically send heart beats to the server to keep the session alive.

The Service Locator provides the following operations.


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.talend.esb.servicelocator.client.ServiceLocator
ServiceLocator.PostConnectAction
 
Constructor Summary
ServiceLocatorImpl()
           
 
Method Summary
 void connect()
          Establish a connection to the Service Locator.
protected  org.apache.zookeeper.ZooKeeper createZooKeeper(CountDownLatch connectionLatch)
           
 void disconnect()
          Disconnects from a Service Locator server.
 SLEndpoint getEndpoint(QName serviceName, String endpoint)
          Return the complete endpoint information for the given endpoint registered for the given service.
 List<String> getEndpointNames(QName serviceName)
          For the given service return all endpoints that currently registered at the Service Locator Service.
 List<SLEndpoint> getEndpoints(QName serviceName)
          Return the complete endpoint informations for all endpoints registered for the given service.
 List<QName> getServices()
          Return all services for which endpoints are registered at the Service Locator Service.
 List<String> lookup(QName serviceName)
          For the given service return all endpoints that are currently registered at the Service Locator.
 List<String> lookup(QName serviceName, SLPropertiesMatcher matcher)
          For the given service return all endpoints that are currently registered at the Service Locator and where the custom properties match the given matcher.
 void register(Endpoint epProvider)
          For a given service register the endpoint as defined in the given EndpointProvider.
 void register(Endpoint epProvider, boolean persistent)
          For a given service register the endpoint as defined in the given EndpointProvider.
 void register(QName serviceName, String endpoint)
          For a given service register the endpoint of a concrete provider of this service.
 void register(QName serviceName, String endpoint, boolean persistent)
          
 void register(QName serviceName, String endpoint, SLProperties properties)
          For a given service register the endpoint of a concrete provider of this service.
 void register(QName serviceName, String endpoint, SLProperties properties, boolean persistent)
          
 void removeEndpoint(QName serviceName, String endpoint)
          Remove the given endpoint from the list of endpoints of the given service.
 void setBackend(ServiceLocatorBackend backend)
           
 void setConnectionTimeout(int timeout)
          Specify the time this client waits for a connection to get established.
 void setEndpointTransformer(EndpointTransformer endpointTransformer)
           
 void setLocatorEndpoints(String endpoints)
          Specify the endpoints of all the instances belonging to the service locator ensemble this object might potentially be talking to when connecting.
 void setName(String name)
           
 void setPassword(String passWord)
           
 void setPostConnectAction(ServiceLocator.PostConnectAction postConnectAction)
          Specify the action to be be executed after the Service Locator has connected to the server.
 void setSessionTimeout(int timeout)
          Specify the time out of the session established at the server.
 void unregister(Endpoint epProvider)
          For a given service unregister a previously registered endpoint.
 void unregister(QName serviceName, String endpoint)
          For a given service unregister a previously registered endpoint.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServiceLocatorImpl

public ServiceLocatorImpl()
Method Detail

connect

public void connect()
             throws InterruptedException,
                    ServiceLocatorException
Establish a connection to the Service Locator. After successful connection the specified ServiceLocator.PostConnectAction is run. If the session to the server expires because the server could not be reached within the specified time, a reconnect is automatically executed as soon as the server can be reached again. Because after a session time out all registered endpoints are removed it is important to specify a ServiceLocator.PostConnectAction that re-registers all endpoints.

Specified by:
connect in interface ServiceLocator
Throws:
InterruptedException - the current Thread was interrupted when waiting for a successful connection to the ServiceLocator
ServiceLocatorException - the connect operation failed

disconnect

public void disconnect()
                throws InterruptedException,
                       ServiceLocatorException
Disconnects from a Service Locator server. All endpoints that were registered before are removed from the server. To be able to communicate with a Service Locator server again the client has to connect again.

Specified by:
disconnect in interface ServiceLocator
Throws:
InterruptedException - the current Thread was interrupted when waiting for the disconnect to happen
ServiceLocatorException

register

public void register(QName serviceName,
                     String endpoint)
              throws ServiceLocatorException,
                     InterruptedException
For a given service register the endpoint of a concrete provider of this service. The endpoint is marked as live as long as this client is connected and the session on the server is valid.

The endpoint is categorized as being a SOAP / HTTP endpoint.

Specified by:
register in interface ServiceLocator
Parameters:
serviceName - the name of the service the endpoint is registered for, must not be null
endpoint - the endpoint to register, must not be null
Throws:
ServiceLocatorException - the server returned an error
InterruptedException - the current Thread was interrupted when waiting for a response of the ServiceLocator

register

public void register(QName serviceName,
                     String endpoint,
                     boolean persistent)
              throws ServiceLocatorException,
                     InterruptedException

Specified by:
register in interface ServiceLocator
Throws:
ServiceLocatorException
InterruptedException

register

public void register(QName serviceName,
                     String endpoint,
                     SLProperties properties)
              throws ServiceLocatorException,
                     InterruptedException
For a given service register the endpoint of a concrete provider of this service. The endpoint is marked as live as long as this client is connected and the session on the server is valid. In addition a set of custom properties is defined for the endpoint. They may be used as additional selection criteria during the lookup.

Specified by:
register in interface ServiceLocator
Parameters:
serviceName - the name of the service the endpoint is registered for, must not be null
endpoint - the endpoint to register, must not be null
properties - custom properties of the endpoint that can be used as additional selection criteria when doing a lookup.
Throws:
ServiceLocatorException - the server returned an error
InterruptedException - the current Thread was interrupted when waiting for a response of the ServiceLocator

register

public void register(QName serviceName,
                     String endpoint,
                     SLProperties properties,
                     boolean persistent)
              throws ServiceLocatorException,
                     InterruptedException

Specified by:
register in interface ServiceLocator
Throws:
ServiceLocatorException
InterruptedException

register

public void register(Endpoint epProvider)
              throws ServiceLocatorException,
                     InterruptedException
For a given service register the endpoint as defined in the given EndpointProvider. The endpoint is marked as live as long as this client is connected and the session on the server is valid.

Specified by:
register in interface ServiceLocator
Parameters:
epProvider - provides all the necessary information to register an endpoint like name of the service for which to register the endpoint, the endpoint URL, must not be null.
Throws:
ServiceLocatorException - the server returned an error
InterruptedException - the current Thread was interrupted when waiting for a response of the ServiceLocator

register

public void register(Endpoint epProvider,
                     boolean persistent)
              throws ServiceLocatorException,
                     InterruptedException
Description copied from interface: ServiceLocator
For a given service register the endpoint as defined in the given EndpointProvider. If the given persistent flag is not set the endpoint is marked as live as long as this client is connected and the session on the server is valid. Otherwise it is marked live independent of the sesion state.

Specified by:
register in interface ServiceLocator
Parameters:
epProvider - provides all the necessary information to register an endpoint like name of the service for which to register the endpoint, the endpoint URL, must not be null.
persistent - iff true the endpoint is marked live until it is explicitly unregistered.
Throws:
ServiceLocatorException - the server returned an error
InterruptedException - the current Thread was interrupted when waiting for a response of the ServiceLocator

unregister

public void unregister(Endpoint epProvider)
                throws ServiceLocatorException,
                       InterruptedException
Description copied from interface: ServiceLocator
For a given service unregister a previously registered endpoint.

Specified by:
unregister in interface ServiceLocator
Throws:
ServiceLocatorException - the server returned an error
InterruptedException - the current Thread was interrupted when waiting for a response of the ServiceLocator

unregister

public void unregister(QName serviceName,
                       String endpoint)
                throws ServiceLocatorException,
                       InterruptedException
For a given service unregister a previously registered endpoint.

Specified by:
unregister in interface ServiceLocator
Parameters:
serviceName - the name of the service the endpoint is unregistered for, must not be null
endpoint - the endpoint to unregister, must not be null
Throws:
ServiceLocatorException - the server returned an error
InterruptedException - the current Thread was interrupted when waiting for a response of the ServiceLocator

removeEndpoint

public void removeEndpoint(QName serviceName,
                           String endpoint)
                    throws ServiceLocatorException,
                           InterruptedException
Remove the given endpoint from the list of endpoints of the given service. When calling getEndpoint(serviceName) the given endpoint will not be contained in the returned list. In case no service with the given name exists or the given endpoint is not registered with the service the state of the Service Locator remians unchanged.

Specified by:
removeEndpoint in interface ServiceLocator
Parameters:
serviceName - the name of the service the endpoint is removed from, must not be null
endpoint - the endpoint to remove, must not be null
Throws:
ServiceLocatorException - the server returned an error
InterruptedException - the current Thread was interrupted when waiting for a response of the ServiceLocator

getServices

public List<QName> getServices()
                        throws InterruptedException,
                               ServiceLocatorException
Return all services for which endpoints are registered at the Service Locator Service.

Specified by:
getServices in interface ServiceLocator
Returns:
a possibly empty list of services
Throws:
InterruptedException - the current Thread was interrupted when waiting for a response of the ServiceLocator
ServiceLocatorException - the server returned an error

getEndpoints

public List<SLEndpoint> getEndpoints(QName serviceName)
                              throws ServiceLocatorException,
                                     InterruptedException
Return the complete endpoint informations for all endpoints registered for the given service.

Specified by:
getEndpoints in interface ServiceLocator
Parameters:
serviceName - the name of the service for which the endpoints ares registered, must not be null
Returns:
the list of end point informations, may be empty if no the endpoints are registered for the service.
Throws:
ServiceLocatorException - the server returned an error
InterruptedException - the current Thread was interrupted when waiting for a response of the ServiceLocator

getEndpoint

public SLEndpoint getEndpoint(QName serviceName,
                              String endpoint)
                       throws ServiceLocatorException,
                              InterruptedException
Return the complete endpoint information for the given endpoint registered for the given service.

Specified by:
getEndpoint in interface ServiceLocator
Parameters:
serviceName - the name of the service for which the endpoint is registered, must not be null
endpoint - the endpoint for which to return the information, must not be null
Returns:
the end point information, may be null if the endpoint was not registered before
Throws:
ServiceLocatorException - the server returned an error
InterruptedException - the current Thread was interrupted when waiting for a response of the ServiceLocator

getEndpointNames

public List<String> getEndpointNames(QName serviceName)
                              throws ServiceLocatorException,
                                     InterruptedException
For the given service return all endpoints that currently registered at the Service Locator Service.

Specified by:
getEndpointNames in interface ServiceLocator
Parameters:
serviceName - the name of the service for which to get the endpoints, must not be null
Returns:
a possibly empty list of endpoints
Throws:
ServiceLocatorException - the server returned an error
InterruptedException - the current Thread was interrupted when waiting for a response of the ServiceLocator

lookup

public List<String> lookup(QName serviceName)
                    throws ServiceLocatorException,
                           InterruptedException
For the given service return all endpoints that are currently registered at the Service Locator.

Specified by:
lookup in interface ServiceLocator
Parameters:
serviceName - the name of the service for which to get the endpoints, must not be null
Returns:
a possibly empty list of endpoints
Throws:
ServiceLocatorException - the server returned an error
InterruptedException - the current Thread was interrupted when waiting for a response of the ServiceLocator

lookup

public List<String> lookup(QName serviceName,
                           SLPropertiesMatcher matcher)
                    throws ServiceLocatorException,
                           InterruptedException
For the given service return all endpoints that are currently registered at the Service Locator and where the custom properties match the given matcher.

Specified by:
lookup in interface ServiceLocator
Parameters:
serviceName - the name of the service for which to get the endpoints, must not be null
matcher - custom properties of the endpoints returned match the criterias specified by this matcher
Returns:
a possibly empty list of endpoints
Throws:
ServiceLocatorException - the server returned an error
InterruptedException - the current Thread was interrupted when waiting for a response of the ServiceLocator

setLocatorEndpoints

public void setLocatorEndpoints(String endpoints)
Specify the endpoints of all the instances belonging to the service locator ensemble this object might potentially be talking to when connecting. The object will one by one pick an endpoint (the order is non-deterministic) to connect to the service locator until a connection is established.

Parameters:
endpoints - comma separated list of endpoints,each corresponding to a service locator instance. Each endpoint is specified as a host:port pair. At least one endpoint must be specified. Valid exmaples are: "127.0.0.1:2181" or "sl1.example.com:3210, sl2.example.com:3210, sl3.example.com:3210"

setSessionTimeout

public void setSessionTimeout(int timeout)
Specify the time out of the session established at the server. The session is kept alive by requests sent by this client object. If the session is idle for a period of time that would timeout the session, the client will send a PING request to keep the session alive.

Parameters:
sessionTimeout - timeout in milliseconds, must be greater than zero and less than 60000.

setConnectionTimeout

public void setConnectionTimeout(int timeout)
Specify the time this client waits for a connection to get established.

Parameters:
connectionTimeout - timeout in milliseconds, must be greater than zero

setBackend

public void setBackend(ServiceLocatorBackend backend)

setName

public void setName(String name)

setPassword

public void setPassword(String passWord)

setEndpointTransformer

public void setEndpointTransformer(EndpointTransformer endpointTransformer)

setPostConnectAction

public void setPostConnectAction(ServiceLocator.PostConnectAction postConnectAction)
Specify the action to be be executed after the Service Locator has connected to the server.

Specified by:
setPostConnectAction in interface ServiceLocator
Parameters:
postConnectAction - the action to be executed, must not be null.

createZooKeeper

protected org.apache.zookeeper.ZooKeeper createZooKeeper(CountDownLatch connectionLatch)
                                                  throws ServiceLocatorException
Throws:
ServiceLocatorException


Copyright © 2011–2013 Talend Inc.. All rights reserved.