EIG:Install TimeService Tutorial into Apache Karaf
See the Building your first OSGi Remote Services Tutorial to understand how to design and build this example.
Prior to running this tutorial you must install ECF Remote Services into your Karaf server.
Exporting the Remote Service with Karaf
To export the remote time service host with Apache Karaf simply install the timeservice host example feature
feature:install -v ecf-remoteservices-examples-timeservice-host
This will install two bundles: the timeservice API bundle (com.mycorp.examples.timeservice) and the timeservice host bundle (com.mycorp.examples.timeservice.host). When Karaf start these bundles, they are exported, and will produce the following output on console
The output after the EXPORT_REGISTRATION indicates that the TimeService has been exported as a remote service and is ready for remote consumer discovery and usage.
For reference, this output is produced by the TimeService Examples Host activator class com.mycorp.examples.timeservice.host.Activator. The source for the entire bundle can be found in the ECF primary git repo, project path: examples/bundles/com.mycorp.examples.timeservice.host.
Importing and using the Remote Service Using Eclipse
For this tutorial, the time service consumer will be a simple OSGi client, which will run in debug mode using Eclipse. For this the prerequisites are:
- Java 1.7 or Java 1.8
- Eclipse Mars (see here to download) with eGit
- ECF (see here to download and install)
Once the above are installed, add the ECF primary git repo to the git repos view
and import from the examples/plugins directory the two projects: com.mycorp.examples.timeservice and com.mycorp.examples.timeservice.consumer.ds
Open the TimeServiceComponent.java source from the com.mycorp.examples.timeservice.consumer.ds package in the project of the same name, and place a break point on the first code line of the bindTimeService method.
The bindTimeService method will be called by Declarative Services when the ITimeService is discovered via remote services discovery. The instance of ITimeService will be an ECF-constructed proxy for accessing the remote ITimeService.
To start the client running in the debugger, open the editor of this product file: com.mycorp.examples.timeservice.consumer.ds/launch/TimeServiceConsumer.ds.generic.noreg.product. When ready to launch the client, click on Launch an Eclipse application in debug mode.
After a few seconds the service should be discovered and the proxy injected into the bindTimeService method by declarative services, causing the Eclipse debugger to stop the client execution at the breakpoint set above.
NOTE: For this localhost example, the discovery provider used is Zeroconf (in bundle org.eclipse.ecf.provider.jmdns). Zeroconf uses multicast on a LAN. If you are not attached to a LAN, or the LAN you are on has restricted use of Zeroconf, the the remote service may not be automatically discovered by the client.
Allow the execution to continue and the remote method will be invoked, and the current time reported by the Karaf server's remote service will be shown in the consumer's console
Also, the Karaf console will show an indication that the remote service implementation method was invoked