We will now go into the details of the Component
interface. It contains methods for the following:
- Providing a synchronous service
- Removing a provided synchronous service
- Registering interest in using a synchronous service
- Discarding a used synchronous service
- Distributing a component
- Publishing an asynchronous service
- Subscribing to an asynchronous service
- Unsubscribing from an asynchronous service
- Using a service
Providing a synchronous service. The provideService()
method in the Component interface makes a synchronous service
available to other components. To provide a service, the method
takes in an interface and a distributed object. A distributed
object is an object that can be moved to another program, but
any calls made on the object take effect in the original program.
The method produces a unique ID that uniquely identifies the service.
To see example code that provides a service, see HelloWorldProvider.java.
UniqueID serviceID = component.provideService(someServiceSynchronous.class,
distributedObject);
Removing a provided synchronous service. When a
synchronous service is removed, Openwings Component Services
identifies the component that is being removed by its unique
service ID. Note that a distributed service object is not
disconnected from any clients. The undistributeObject() method
must be called to disconnect the service.
removeProvidedService(serviceID);
Registering interest in using a synchronous service.
Services are used via their interfaces. Openwings Component Services
finds a synchronous (remote method) service provided by another
component (lookup). Components may register an interest in the
synchronous service of provider components by using the useService()
method of the Component interface. An example of this is in HelloWorldUser.java.
try { component.useService(someServiceSynchronous.class,
this); }
catch (InvalidServiceException e) {}
Discarding a used synchronous service. When a
synchronous service is discarded, Openwings Component Services
identifies the component that is being discarded by its unique
service ID. Component Services then provides shutdown and any
cleanup tasks that may be needed.
discardUsedService(serviceID)
Distributing a component. When the distributeObject()
method is called on an object, Openwings Component Services is
actually wrapping the object in a connector. A connector consists
of two proxies that can communicate using some protocol. The
distributeObject() method initializes the receiver or
"provider" proxy so that incoming calls are mapped to
your object. The object returned from distributeObject() is the
sender or "user" proxy of the connector. This user proxy
is a serializable object that can be moved to another program.
This is what makes a service of an object available remotely.
Object distributedObject = component.distributeObject(someServiceSynchronous.class,
someProvider);
Publishing an asynchronous service. The method publishService()
is used to indicate that the component will be published as an
asynchronous (event or messaging) service. With an asynchronous
service, the client implements the interface and simply publishes
the asynchronous service. The object does not have to be distributed.
Openwings Container Services handles that part for you. In the
Connector section of
this tutorial trail, asynchronous connections are described as
"push" connections - data is pushed through to the subscriber
of this service. An example can be seen in HelloWorldPublisher.java.
service = (someServiceAsynchronous)
component.publishService(someServiceAsynchronous.class);
Subscribing to an asynchronous service. The method subscribeService()
is used to indicate interest in subscribing to an asynchronous
(event or messaging) service. The class using this method represents
the client side of the connection and actually implements the
service interface. An example can be seen in HelloWorldSubscriber.java.
ComponentFactory.getComponent().subscribeService(someServiceAsynchronous.class,
this);
Unsubscribing from an asynchronous
service. When an asynchronous service is unsubscribed, it
means the component is no longer interested in receiving
asynchronous messages (events). Component Services identifies the
component that is being unsubscribed by its service interface.
unsubscribeService(someServiceAsynchronous.class)
Using a service. The useService() method is overloaded.
All variations take a service interface as the first parameter.
This method can find multiple synchronous (remote method) services
provided by another component (lookup). Another variant of this
method uses a UseServiceListener
object as a parameter in addition to the service interface parameter.
A UseServiceListener can be used to register ongoing interest
in certain services. Example code using the useService method
can be seen in HelloWorldUser.java.
try { component.useService(someServiceSynchronous.class,
this); }
catch (InvalidServiceException e) {}