Class ServiceFactory
- java.lang.Object
-
- com.attivio.service.ServiceFactory
-
public class ServiceFactory extends java.lang.Object
Class which encapsulates the creation of cluster service classes based on Curator Service Discovery.Cleanup
Any factory, client, or service facility that uses the ServiceFactory can make sure its resources are closed by callingaddCloseable(Closeable)
. All closeables will be called by the ServiceFactory during thereset(boolean)
method.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ServiceFactory.AieServiceCacheListener
-
Constructor Summary
Constructors Constructor Description ServiceFactory()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addCloseable(java.io.Closeable closeable)
Adds thecloseable
to a list that will be closed upon call to thereset(boolean)
method.static <T> void
addServiceListener(java.lang.Class<T> clazz, ServiceListener listener)
Adds a listener that is called whenever the service availability changes.static <T> T
getFactoryParams(java.lang.Class<T> clazz)
Returns parameters of typeT
if previously set ornull
if not set.static <T> T
getService(java.lang.Class<T> clazz)
static <T> T
getService(java.lang.Class<T> clazz, long timeout)
static <T> T
getService(java.lang.Class<T> clazz, long timeout, boolean logErrorOnTimeout)
static <T> T
getService(java.lang.Class<T> clazz, java.lang.String nodeName)
static <T> T
getService(java.lang.Class<T> clazz, java.lang.String nodeName, long timeout)
static <T> T
getService(java.lang.Class<T> clazz, java.util.Map<java.lang.String,java.lang.String> attributes)
static <T> T
getService(java.lang.Class<T> clazz, java.util.Map<java.lang.String,java.lang.String> attributes, long timeout)
static <T> T
getService(java.lang.Class<T> clazz, java.util.Map<java.lang.String,java.lang.String> attributes, long timeout, boolean logErrorOnTimeout)
Returns an implementation of the desired service API.static <T> org.apache.curator.x.discovery.ServiceInstance<AieServiceInfo>
getServiceInstance(java.lang.Class<T> clazz, java.lang.String nodeName)
static <T> AieServiceInfo
prepareServiceInfo(java.lang.String description, java.lang.Class<? extends RemoteClientApiFactory> customFactory, java.lang.Class<? extends java.util.function.Predicate<java.lang.Throwable>>... predicates)
TheregisterService(java.lang.Class<T>, java.lang.String, int, java.lang.Class<? extends com.attivio.service.RemoteClientApiFactory>, java.lang.Class<? extends java.util.function.Predicate<java.lang.Throwable>>...)
method utilizes this method to prepare the basic fields forAieServiceInfo
.static org.apache.curator.x.discovery.ServiceInstanceBuilder<AieServiceInfo>
prepareServiceInstanceBuilder(java.lang.String apiClassName, AieServiceInfo serviceInfo, int port)
static <T> org.apache.curator.x.discovery.ServiceInstance<AieServiceInfo>
registerService(java.lang.Class<T> api, java.lang.String description, int port, java.lang.Class<? extends RemoteClientApiFactory> customFactory, java.lang.Class<? extends java.util.function.Predicate<java.lang.Throwable>>... predicates)
static org.apache.curator.x.discovery.ServiceInstance<AieServiceInfo>
registerService(org.apache.curator.x.discovery.ServiceInstance<AieServiceInfo> service)
static void
reset(boolean clearServices)
Resets all local caches of service registrations, mock services, curator and zookeeper connections.static void
setFactoryParams(java.lang.Object params)
Sets the factory parameters for the typeparams.getClass()
and all interfaces provided by the type for this client process.static void
unregisterService(org.apache.curator.x.discovery.ServiceInstance<AieServiceInfo> service)
-
-
-
Method Detail
-
registerService
@SafeVarargs public static <T> org.apache.curator.x.discovery.ServiceInstance<AieServiceInfo> registerService(java.lang.Class<T> api, java.lang.String description, int port, java.lang.Class<? extends RemoteClientApiFactory> customFactory, java.lang.Class<? extends java.util.function.Predicate<java.lang.Throwable>>... predicates) throws AttivioException
- Throws:
AttivioException
-
registerService
public static org.apache.curator.x.discovery.ServiceInstance<AieServiceInfo> registerService(org.apache.curator.x.discovery.ServiceInstance<AieServiceInfo> service) throws AttivioException
- Throws:
AttivioException
-
unregisterService
public static void unregisterService(org.apache.curator.x.discovery.ServiceInstance<AieServiceInfo> service) throws AttivioException
- Throws:
AttivioException
-
getService
public static <T> T getService(java.lang.Class<T> clazz, long timeout) throws AttivioException
SeegetService(Class, String, long)
. Node will default tonull
.- Throws:
AttivioException
-
getService
public static <T> T getService(java.lang.Class<T> clazz, long timeout, boolean logErrorOnTimeout) throws AttivioException
- Throws:
AttivioException
-
getService
public static <T> T getService(java.lang.Class<T> clazz) throws AttivioException
- Throws:
AttivioException
-
getService
public static <T> T getService(java.lang.Class<T> clazz, java.lang.String nodeName) throws AttivioException
SeegetService(Class, String, long)
. Timeout will default to-1L
.- Throws:
AttivioException
-
getService
public static <T> T getService(java.lang.Class<T> clazz, java.util.Map<java.lang.String,java.lang.String> attributes) throws AttivioException
- Throws:
AttivioException
-
getService
public static <T> T getService(java.lang.Class<T> clazz, java.lang.String nodeName, long timeout) throws AttivioException
- Throws:
AttivioException
-
getService
public static <T> T getService(java.lang.Class<T> clazz, java.util.Map<java.lang.String,java.lang.String> attributes, long timeout) throws AttivioException
- Throws:
AttivioException
-
getService
public static <T> T getService(java.lang.Class<T> clazz, java.util.Map<java.lang.String,java.lang.String> attributes, long timeout, boolean logErrorOnTimeout) throws AttivioException
Returns an implementation of the desired service API.Services come in two flavors. Some services are registered by a service provider and provide services through remote client implementations. Other services are built on an external platform such as a database. The latter are referred to as client-base services since from the AIE point of view, only a client is required. In either case, this method returns a (generally proxied) implementation that provides the service API.
The timeout parameter is applicable only to registered services. This is because client-based services are provided externally and the ServiceFactory has no way of knowing if those services are up or not. For registered services, the timeout applies to the time for that service to be registered with the system. This lets callers wait a bit (say during a startup situation) for the service to come online but not wait indefinitely.
When the timeout is in use, the service will be checked for registration every 100ms. A timeout < 0 will wait indefinitely. Regardless of the timeout, the availability of the service will always be tried at least once. That is, a low timeout or timeout of 0 will still succeed if the service is registered or is provided by a client-based API.
- Parameters:
clazz
- the class type of the desired APIattributes
- map of attributes that must match the service, ornull
or an empty map if any instance is fine.timeout
- timeout in mslogErrorOnTimeout
- iftrue
an ERROR message will be logged in the event of a timeout- Returns:
- an implementation of the API or
null
if unavailable within the timeout. - Throws:
AttivioException
-
getServiceInstance
public static <T> org.apache.curator.x.discovery.ServiceInstance<AieServiceInfo> getServiceInstance(java.lang.Class<T> clazz, java.lang.String nodeName) throws AttivioException
- Returns:
- an instance or throw an exception
- Throws:
AttivioException
-
addServiceListener
public static <T> void addServiceListener(java.lang.Class<T> clazz, ServiceListener listener) throws AttivioException
Adds a listener that is called whenever the service availability changes. If the service is currently available, the listener will be called.- Parameters:
clazz
-listener
-- Throws:
AttivioException
-
getFactoryParams
public static <T> T getFactoryParams(java.lang.Class<T> clazz)
Returns parameters of typeT
if previously set ornull
if not set. Note, only the exact class supplied is checked; any desired interfaces implemented byclazz
must be individually checked.- Parameters:
clazz
-- Returns:
- the parameters
-
setFactoryParams
public static void setFactoryParams(java.lang.Object params)
Sets the factory parameters for the typeparams.getClass()
and all interfaces provided by the type for this client process. There can be only one set of parameters per type.- Parameters:
params
-
-
reset
public static void reset(boolean clearServices)
Resets all local caches of service registrations, mock services, curator and zookeeper connections. All Closeables added via theaddCloseable(Closeable)
method are closed.- Parameters:
clearServices
- if true, registered services list is cleared otherwise they are re-registered
-
prepareServiceInfo
@SafeVarargs public static <T> AieServiceInfo prepareServiceInfo(java.lang.String description, java.lang.Class<? extends RemoteClientApiFactory> customFactory, java.lang.Class<? extends java.util.function.Predicate<java.lang.Throwable>>... predicates) throws AttivioException
TheregisterService(java.lang.Class<T>, java.lang.String, int, java.lang.Class<? extends com.attivio.service.RemoteClientApiFactory>, java.lang.Class<? extends java.util.function.Predicate<java.lang.Throwable>>...)
method utilizes this method to prepare the basic fields forAieServiceInfo
.Developers can take the AieServiceInfo returned and set other fields, and call
prepareServiceInfo(String, Class, Class...)
to get service builder.Sample code:
AieServiceInfo
serviceInfo =ServiceFactory
.prepareServiceInfo(java.lang.String, java.lang.Class<? extends com.attivio.service.RemoteClientApiFactory>, java.lang.Class<? extends java.util.function.Predicate<java.lang.Throwable>>...)
("AIE Ingestion Service", RemoteIngestApiFactory.class, TransientTransportErrorPredicate.class); serviceInfo.setLocalClass(LocalIngestApiImpl.class); if (HTTPUtils.isAuthRequired()) { serviceInfo.addRequiredParams(AuthParams.class); }ServiceInstanceBuilder
<AieServiceInfo
> serviceBuilder =ServiceFactory
.prepareServiceInstanceBuilder(IngestApi.class.getName(), serviceInfo, Platform.instance.getPort(Ports.RECEIVER_PORT_OFFSET)); serviceBuilder.sslPort(ssl ? Platform.instance.getPort(Ports.SECURE_RECEIVER_PORT_OFFSET) : 0);ServiceInstance
<AieServiceInfo
> service = serviceBuilder.build(); try {ServiceFactory
.registerService(java.lang.Class<T>, java.lang.String, int, java.lang.Class<? extends com.attivio.service.RemoteClientApiFactory>, java.lang.Class<? extends java.util.function.Predicate<java.lang.Throwable>>...)
(service); } catch (Exception e) { throw new AttivioException(PlatformError.SERVICE_REGISTRATION_FAILURE, e, "Failed to register service %s", IngestApi.class); }- Parameters:
description
-customFactory
-predicates
-- Returns:
- Throws:
AttivioException
-
prepareServiceInstanceBuilder
public static org.apache.curator.x.discovery.ServiceInstanceBuilder<AieServiceInfo> prepareServiceInstanceBuilder(java.lang.String apiClassName, AieServiceInfo serviceInfo, int port) throws AttivioException
- Throws:
AttivioException
-
addCloseable
public static void addCloseable(java.io.Closeable closeable)
Adds thecloseable
to a list that will be closed upon call to thereset(boolean)
method. This call is thread-safe.- Parameters:
closeable
-
-
-