Jump to: navigation, search

EIG:Install TimeService Tutorial into Apache Karaf

Introduction

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

Karafinst4.png

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.

Running/Debugging the TimeService Client in Eclipse

For this part of the tutorial, the time service consumer will be a simple OSGi client, which will run in Eclipse. For this the prerequisites are:

  1. Java 1.8
  2. Eclipse (see here to download) with eGit installed
  3. ECF SDK for Eclipse (see here to download and install)

Once the above are installed, clone the ECF git repo in the Git Repositories View (only master branch is needed).

Karafinst5.png

and then Import Projects these two projects from the examples/plugins directory: com.mycorp.examples.timeservice.async and com.mycorp.examples.timeservice.consumer.ds.async

KarafInst6a.png

Open the TimeServiceComponentAsync.java source from the com.mycorp.examples.timeservice.consumer.ds.async package in the project of the same name, and place a break point on the first code line of the bindTimeService method.

KarafInst7a.png

The bindTimeService method will be called by Declarative Services when the ITimeServiceAsync is discovered via Zeroconf LAN-based remote services discovery. The instance of ITimeServiceAsync will be an ECF-constructed proxy service for accessing the remote ITimeService via the Java8 CompletableFuture.

To start the client running in the debugger, open the editor of this product file: com.mycorp.examples.timeservice.consumer.ds.async/TimeServiceConsumer.generic.zeroconf.product. When ready to launch the client, click on 'Launch an Eclipse Application in Debug Mode' in the lower-left part of the Overview tab.

KarafInst8a.png

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.

Karafinst9.png

KarafInst9a.png

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 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

KarafInst10a.png

Also, the Karaf console will show an indication that the remote service implementation method was invoked

Karafinst11.png