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:
- org.javasavvy.{project-name}.api – this bundle will contains all interface exposed and can be imported in other bundles
- org.javasavvy.{project-name}.impl – This bundle provides implementation of “org.jasavvy.api”
- org.javasavvy.{project-name}.web- This is sample web project where it uses services laye
- 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.
- Impl Bundle will register as Service Providers
- OSGI Container Framework will register this service in Service Registry
- web bundle will get Service implementation at runtime from OSGI framework
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.
- Create BND Workspace if not created
- 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
- UserService.java will look like below
-
public interface UserService { public String getUserName(); public void addUser(); public void deleteUser(); }
-
- Now export pakage in bnd.bnd file in “org.javasavvy.demo.api” module
- 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.
- 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.
- Now the the project Structure will look like:
- 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
- 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() { } }
-
- @Component will register UserServiceImpl to OSGI Service Registry framework and no additional configuration is required.
- In the client web project, open the bnd.bnd settings file and add the below dependencies under build tab like show in below:
- 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.
hi ..this tutorial very useful for beginner
thanks …for creating
Thanks Kunal
Very good article for the OSGI beginner like me.
Thank you.