Consuming REST services in OSGi the cool way

I recently introduced you to the JAX-RS Consumer. If you’ve read that post you might remember that I promised to write about the OSGi integration of the consumer. This is the topic of this short post.

As you might know, a while ago I created the OSGI-JAX-RS connector. This connector gives you the opportunity to publish your @Path/@Provider resources by registering them as OSGi services. So, why not consume a REST service as an OSGi service? That’s exactly what the JAX-RS Consumer OSGi integration provides. It registers an OSGi service called ConsumerPublisher. Using this service you can publish your @Path interfaces. The ConsumerPublisher creates consumer objects out of them and registers them as OSGi services. Afterwards you will be able to consume them as you do with all other OSGi services.

This is best explained with a small example. Let’s say we are using Declarative Services. So, this gives us the option to register a component and inject the ConsumerPublisher as in the code below.

public class Publisher {
  public void addConsumerPublisher( ConsumerPublisher publisher ) {
    publisher.publishConsumers( "http://server:port/root", 
                                new Class[] { MyResource.class }, 
                                new Object[] {new CustomProvider() } );

When the JAX-RS consumer bundle is started, the ConsumerPublisher service will be registered and is injected into our component. What we have to do additionally, is to add a base url, our @Path interfaces and the custom @Providers we want to use. The ConsumerPublisher takes care of creating the consumer objects out of the interfaces and registers them as OSGi services. In this example we can afterwards get/call a service of the type MyResource.

If you would like to try this out yourself I have created a p2 repository containing the consumer and it’s dependencies. A working example can also be found on GitHub. Any feedback is very welcome ;).


4 Responses to “Consuming REST services in OSGi the cool way”

  1. dave says:

    I’m wondering how you created the cool looking chart (consumer.jpg)? Is is handmade or computer based somehow. Thxs

  2. Dirk Zander says:

    Hi Holger,
    is it possible to include the jax-rs-consumer in the jax-rs-connector P2 repo as well?

  3. Hi Dirk,
    yes it’s possible but not done yet. I will release a new version when Jersey 2.0 will be released. This should be in early 2013. In the meantime you can download the p2 repo from the downloads section in github repo.

    Hope this helps.

    Cheers Holger

4 responses so far

Written by . Published in Categories: Planet Eclipse, Planet OSGi

Looking for a job?

Karlsruhe / Remote
Karlsruhe / Victoria / Remote
Karlsruhe / Victoria / Remote