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?
Here is my 5 cent:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With