I will show you how to build OSGi server serving HTTP using Eclipse IDE bndtools, I assume that the readers has at least one year of practical experience in writing JAVA programs.
1. Installing Eclipse Bndtools Plugin
Choose “Help –>Eclipse Marketplace”
2. Creating a Bnd Configuration Project
Open “Bndtools perspective”, and Eclipse will open a create bnd configuration project dialog automatically. This configuration project stores a repository with all external project dependencies and global build configuration.
Then Choose “Create a configuration project(recommended)”, and you will see next dialog:
Select “Standard Configuration ” and click “finish”, when you create configuration project successfully, you will find a “cnf” project in your Eclipse:
3. Create a new bnd project
Create a new bnd project using “File –> New –> Bndtools OSGi Project“:
Named the project something like that”com.felixbundle.test” and choose “Component Development(Declarative Service)” as a default template:
Then you’ll find a new
bnd project in Eclipse, and you have two project now, one is the cnf project, another is the bundle project. The bundle project have a bnd.bnd configuration file, and the MANIFEST.MF will be created automatically from this configuration:
4. Add the felix.jetty bundle into your project
Choose “Run” tab, and click “＋” in Run Bundles block:
Choose “Run As –> OSGi Run”:
This will start up Felix framework with Jetty serving HTTP request:
5. Build a servlet path and add a servlet component
If you need servlet API, you need add jetty into your path. Choose “Build” tab, and click “＋” in Build Path block:
Add “org.apache.felix.http.jetty” bundle into your build path:
You should download Apache Felix HTTP whiteboard bundle and install it into your local repositories, because the Apache Felix HTTP Service Whiteboard bundle makes it very easy to publish Servlets using OSGi services.
When you install Apache Felix HTTP Service Whiteboard into local repositories already, add org.apache.felix.webconsole and org.apache.felix.whiteboard into your run bundles.
Create a servlet class and annotate it with Bnd’s @Component annotation to make the servlet a declarative service component:
6. Run the OSGi server again, and open http://localhost:8080/, http://localhost:8080/system/console/bundles in your broswer
The bundle jar is automatically built and deployed when classes are changed. So try to change the servlet, save and reload the page. You should see your changes without restarting the server.
(Visited 60 times, 1 visits today)