OSGI Services Tutorial


OSGI Services Tutorial

OSGI Services Tutorial will drive you on creation of OSGI services with declarative services. The “OSGI Bundles Tutorial” explained on setting up OSGI development and runtime environment and go through the tutorial if you are new to OSGI.

How ever, each and every project is required to follow layered architecture with view,services, persistence layers.  In OSGI context, we will convey them as bundles like below:

  1. org.javasavvy.{project-name}.api –  this bundle will contains all interface exposed and can be imported in other bundles
  2. org.javasavvy.{project-name}.impl –   This bundle provides implementation of “org.jasavvy.api”
  3. org.javasavvy.{project-name}.web-  This is sample web project where it uses services laye
  4. org.javasavvy.{project-name}.webservices –

web and web services bundles are dependent on  impl services. How to inject impl services into OSGI web module?  As you all know Spring offers dependency injection to inject beans run time. The same way OSGI also provides dynamic lookup of objects via Publish-Find-Bind modal.

  1. Impl Bundle   will register as Service Providers
  2. OSGI Container Framework will register this service in Service Registry
  3. web bundle will get Service implementation at runtime from OSGI framework

 OSGI Declarative Services

Lets jump into creating exercise on OSGI Services with Declarative annotations

The prerequisite for this is to BND plugin installation in eclipse. Go through this tutorial for installation and BND workspace creation.

  1. Create BND Workspace if not created
  2. Create empty API project (File -> New -> Bnd OSGI Project) “org.javasavvy.demo.api”  as shown in below and update version as “1.0.0” in bnd.bnd file. This is plain interface OSGI bundle and not required to add additional OSGI dependencies. Create UserService,ContactsService and CustomerService interfaces
  3. UserService.java will look like below
    • public interface UserService {
       public String getUserName();
       public void addUser();
       public void deleteUser();
      }
  4. Now export pakage in bnd.bnd file  in “org.javasavvy.demo.api” module
  5.  Create “org.javasavvy.demo.impl”  Impl Project (File -> New -> Bnd OSGI Project)  as same way above by selecting Empty and  update version as “1.0.0” in content section in bnd.bnd settings file.  Add the dependencies as shown in below.
    • osgi.core, osgi.cmpn and demo.api dependencies in bundle: “org.javasavvy.demo.impl” 
  6. Create Servlet OSGI module (File -> New -> Bnd OSGI Project)  as a client to consume the demo module services. In the Project Template section select “Servlet Component” as shown in below and leave the rest As-Is.OSGI Servlet Component
  7. Now the the project Structure will look  like: OSGI Services Demo
  8. Now create UserServiceImpl in  “org.javasavvy.demo.impl” module that implements UserService in “org.javasavvy.demo.api”. Make sure that you added demo.api as build dependency in bnd.bnd file
  9. UserServiceImpl class will look like:
    • @Component(
       immediate=true
       )
      public class UserServiceImpl implements UserService {
       @Override
       public String getUserName() {
       return "Javasavvy: Demo Services Tutorial";
       }
       @Override
       public void addUser() {
       }
       @Override
       public void deleteUser() {
       }
      }
  10. @Component will register UserServiceImpl to OSGI Service Registry framework and no additional configuration is required.
  11. In the client web project, open the bnd.bnd settings file and add the below dependencies under build tab like show in below:
  12. In Client Web Project, Rename the ExampleServlet to UserServlet  and change the path “example” to “/demo” as shown in below and inject the UserService.

Web Project:

@Component(
 property = {
 HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN + "=/demo/*"
 })
public class UserServlet extends HttpServlet implements Servlet {
 private static final long serialVersionUID = 1L;
 private UserService userService;
 
 @Reference
 public void setUserService(UserService userService) {
 this.userService = userService;
 }
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.getWriter().write(userService.getUserName());
 }
 
}

How to run the OSGI Bundles now?

The Servlet template has created launch.bndrun file also that will used to run the OSGI bundles. Open that file and and add the  impl and api module in Run Requirements and Run Bundles Sections.

Select “Auto-Resolve on Save” to avoid errors and click on Run OSGI button

in the browser, enter URL  localhost:8080/demo and you can see the below page:

Hope this helps!!!!

Download Code from here

3 thoughts on “OSGI Services Tutorial”

Comments are closed.