public class ServiceRegistrationInfo
extends java.lang.Object
DXL Services are exposed to the DXL fabric and are invoked in a fashion similar to RESTful web services. Communication between an invoking client and the DXL service is one-to-one (request/response).
Each service is identified by the "topics" it responds to. Each of these "topics" can be thought of as a method that is being "invoked" on the service by the remote client.
Multiple service "instances" can be registered with the DXL fabric that respond to the same "topics". When this occurs (unless explicitly overridden by the client) the fabric will select the particular instance to route the request to (by default round-robin). Multiple service instances can be used to increase scalability and fault-tolerance.
The following demonstrates registering a service that responds to a single topic with the DXL fabric:
RequestCallback myRequestCallback = request -> { try { // Extract information from request System.out.println("Service received request payload: " + new String(request.getPayload(), Message.CHARSET_UTF8)); // Create the response message final Response res = new Response(request); // Populate the response payload res.setPayload("pong".getBytes(Message.CHARSET_UTF8)); // Send the response client.sendResponse(res); } catch (Exception ex) { ex.printStackTrace(); } }; // Create service registration object ServiceRegistrationInfo info = new ServiceRegistrationInfo(client, "myService"); // Add a topic for the service to respond to info.addTopic("/mycompany/myservice", myRequestCallback); // Register the service with the fabric (wait up to 10 seconds for registration to complete) client.registerServiceSync(info, 10 * 1000);
The following demonstrates a client that is invoking the service in the example above:
// Create the request message final Request req = new Request("/mycompany/myservice"); // Populate the request payload req.setPayload("ping".getBytes(Message.CHARSET_UTF8)); // Send the request and wait for a response (synchronous) final Response res = client.syncRequest(req); // Extract information from the response (output error message if applicable) if (res.getMessageType() != Message.MESSAGE_TYPE_ERROR) { System.out.println("Client received response payload: " + new String(res.getPayload(), Message.CHARSET_UTF8)); } else { System.out.println("Error: " + ((ErrorResponse) res).getErrorMessage()); }
Constructor and Description |
---|
ServiceRegistrationInfo(DxlClient client,
java.lang.String serviceType)
Constructor for
ServiceRegistrationInfo |
Modifier and Type | Method and Description |
---|---|
void |
addMetadata(java.lang.String name,
java.lang.String value)
Adds a name-value pair to the meta-data associated with this service
|
void |
addTopic(java.lang.String topic,
RequestCallback callback)
Registers a topic for the service to respond to along with the
RequestCallback that will be invoked. |
java.util.Map<java.lang.String,java.util.Set<RequestCallback>> |
getCallbacksByTopic()
Returns the a
Map containing the RequestCallback instances by their associated topic. |
java.util.Set<java.lang.String> |
getDestTenantGuids()
Returns the set of tenant identifiers that the service will be available to.
|
java.util.Map<java.lang.String,java.lang.String> |
getMetadata()
Returns the map of meta data associated with this service
|
java.lang.String |
getServiceId()
Returns a unique identifier for the service instance (automatically generated when the
ServiceRegistrationInfo object is constructed |
java.lang.String |
getServiceType()
Returns the a textual name for the service.
|
java.util.Set<java.lang.String> |
getTopics()
Returns a
Set containing the topics that the service responds to |
long |
getTtl()
Returns the interval (in minutes unless the resolution has been modified) at which the client will automatically
re-register the service with the DXL fabric (defaults to
60 minutes). |
long |
getTtlMins()
Returns the interval (in minutes) at which the client will automatically re-register the service with the DXL
fabric (defaults to
60 minutes). |
long |
getTtlResolution()
Returns the Time-To-Live (TTL) resolution factor (by default is
1 , minutes) |
void |
notifyRegistrationSucceeded()
Invoked when the service has been successfully registered with a broker
|
void |
notifyUnregistrationSucceeded()
Invoked when the service has been successfully unregistered with a broker
|
void |
removeTopic(java.lang.String topic,
RequestCallback callback)
Removes a request topic and associated callback from the service.
|
void |
setDestTenantGuids(java.util.Set<java.lang.String> destTenantGuids)
Sets the tenant identifiers that the service will be available to.
|
void |
setMetadata(java.util.Map<java.lang.String,java.lang.String> metadata)
A
Map of name-value pairs that are sent as part of the service registration. |
void |
setTtl(long ttl)
Sets the interval (in minutes unless the resolution has been modified) at which the client will automatically
re-register the service with the DXL fabric (defaults to
60 minutes). |
void |
waitForRegistration(long timeout)
Waits for the service to be registered with the broker for the first time
|
void |
waitForUnregistration(long timeout)
Waits for the service to be unregistered with the broker for the first time
|
public ServiceRegistrationInfo(DxlClient client, java.lang.String serviceType)
ServiceRegistrationInfo
client
- The DxlClient
instance that will expose this serviceserviceType
- textual name for the service. For example, "/mycompany/myservice"public java.lang.String getServiceType()
public java.lang.String getServiceId()
ServiceRegistrationInfo
object is constructedServiceRegistrationInfo
object is constructedpublic long getTtlMins()
60
minutes).60
minutes).public long getTtl()
60
minutes).60
minutes).public void setTtl(long ttl) throws java.lang.IllegalArgumentException
60
minutes).ttl
- The interval (in minutes unless the resolution has been modified) at which the client will
automatically re-register the service with the DXL fabric (defaults to 60
minutes).java.lang.IllegalArgumentException
public long getTtlResolution()
1
, minutes)1
, minutes)public java.util.Set<java.lang.String> getTopics()
Set
containing the topics that the service responds toSet
containing the topics that the service responds topublic java.util.Map<java.lang.String,java.util.Set<RequestCallback>> getCallbacksByTopic()
Map
containing the RequestCallback
instances by their associated topic.Map
containing the RequestCallback
instances by their associated topic.public void addTopic(java.lang.String topic, RequestCallback callback)
RequestCallback
that will be invoked.topic
- The topic for the service to respond tocallback
- The RequestCallback
that will be invoked when a Request
message is receivedpublic void removeTopic(java.lang.String topic, RequestCallback callback)
topic
- The topic to removecallback
- The RequestCallback
to removepublic java.util.Map<java.lang.String,java.lang.String> getMetadata()
public void setMetadata(java.util.Map<java.lang.String,java.lang.String> metadata)
Map
of name-value pairs that are sent as part of the service registration. Brokers provide a registry
service that allows for registered services and their associated meta-information to be inspected. The meta-data
is typically used to include information such as the versions for products that are exposing DXL services, etc.metadata
- A Map
of name-value pairs that are sent as part of the service registration.public void addMetadata(java.lang.String name, java.lang.String value)
name
- Name of the meta-data propertyvalue
- Value of the meta-data propertypublic void waitForRegistration(long timeout) throws DxlException
timeout
- The amount of time to wait for the registration to occur.DxlException
- If a DXL exception occurspublic void notifyRegistrationSucceeded()
public void waitForUnregistration(long timeout) throws DxlException
timeout
- The amount of time to wait for the unregistration to occur.DxlException
- If a DXL exception occurspublic void notifyUnregistrationSucceeded()
public java.util.Set<java.lang.String> getDestTenantGuids()
public void setDestTenantGuids(java.util.Set<java.lang.String> destTenantGuids)
destTenantGuids
- The set of tenant identifiers that the service will be available to