Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Restful service and deploy it OSGi container?

My goal is to create a Restful service Maven project with Eclipse. Then package it as a bundle and deploy it to Fuse ESB karaf OSGi container. So far what I know is how to use the JAX-RS API annotations, @Path @GET:

package com.restfultest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/example")
public class ExampleService {

@GET
public String sayHello() {
    return "Hello Restful service";
 }
}

My question is that: 1. what maven archetype should I use? maven-archetype-webapp or quickstart?

2.How to implement Activator? Like this?

public class Activator implements BundleActivator {

private ServiceRegistration<?> registration;

public void start(BundleContext context) throws Exception {
    // TODO Auto-generated method stub
    ExampleService exampleService = new ExampleService();
    registration = context.registerService( ExampleService.class.getName(), exampleService, null );
}

public void stop(BundleContext context) throws Exception {
    // TODO Auto-generated method stub
    registration.unregister();
}

}

3. How to register and publish the service (like how to configure the Endpoint address and port)?

I am new to osgi. Does anyone can provide me some resources or a detailed tutorial?

like image 278
Li' Avatar asked Apr 03 '13 20:04

Li'


2 Answers

  1. You can use Maven bundle plugin
  2. and 3. Apache CXF DOSGi helps you to publish OSGi services as WS/REST.
like image 163
Dmytro Pishchukhin Avatar answered Nov 15 '22 01:11

Dmytro Pishchukhin


Here is my 5 cent:

1. What Maven archetype should you use?

  • I used the OSGI HTTP Service (Quick Start), check this sample, I considered it was more natural.

2. How to implement Activator?

  • Go ahead and use the Activator of the sample, it worked for me.
  • If you use it, you also will need to implement the the Application and the Rest Service

3. How to register and publish the service?

  • I recommend you to download the latest jersey-ALL-bundle and install it on your OSGI.
  • And then, in my particular case, I used the Maven Bundle Plugin to handle the imports on Runtime and packing, so, my pom.xml looks something like this (Please notice the dependencies):

    ...
    <packaging>bundle</packaging>
    <build>
        <plugins>
            <!--+
                + OSGi Bundle-Manifiest Generator.
                + -->
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.3.7</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Import-Package>javax.servlet.*;version="[2.4,4.0)",*</Import-Package>
                        <Bundle-Activator>com.sample.api.Activator</Bundle-Activator>
                        <Implementation-Title>jersey-osgi-http-service-bundle</Implementation-Title>
                        <Implementation-Version>${project.version}</Implementation-Version>
                    </instructions>
                    <unpackBundle>true</unpackBundle>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
    <dependencies>
        <!--+======================+-->
        <!--+ REST Dependencies    +-->
        <!--+======================+-->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.22.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.http.bundle</artifactId>
            <version>2.2.0</version>
        </dependency>
        <!--+=========================================+-->
        <!--+ Apache Felix Framework (OSGi framework) +-->
        <!--+=========================================+-->
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.framework</artifactId>
            <version>4.6.0</version>
        </dependency>
    </dependencies>
    
  • In my specific case I have also the WebConsole installed, then my bundles look like this:

     0|Active     |    0|System Bundle (4.6.0)
     1|Active     |    1|Apache Felix Bundle Repository (2.0.2)
     2|Active     |    1|Apache Felix Gogo Command (0.14.0)
     3|Active     |    1|Apache Felix Gogo Runtime (0.12.1)
     4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
     5|Active     |    1|com.sample-api (1.3.0.SNAPSHOT)
     6|Active     |    1|jersey-all (2.22.1)
     7|Active     |    1|Apache Felix Log Service (1.0.0)
     8|Active     |    1|Apache Felix Configuration Admin Service (1.2.4)
     9|Active     |    1|Apache Felix Shell Service (1.4.2)    
    10|Active     |    1|Apache Felix Http Bundle (2.0.4)    
    11|Active     |    1|HTTP Service (1.0.0)    
    12|Active     |    1|Apache Felix Web Management Console (3.1.2)
    
  • For Rest it's important to have the bundles 6, 10 and 11.

  • And the port is configurable in the "config.properties" of the OSGI (I used Felix) just by doing this:

    org.osgi.service.http.port=28370
    
  • Then, to reach the service just use the following path:

    http://localhost:28370/jersey-http-service/status
    
like image 24
Marco Vargas Avatar answered Nov 15 '22 00:11

Marco Vargas