How to use servlet with OSGi

10,504

Solution 1

If you use bndtools, create a Declarative Services project and add this annotation to your servlet:

 @Component(provide = Servlet.class, properties = {"alias=/hello"})
 public class HelloWorldServlet extends HttpServlet { ... }

Then create a bnd run descriptor with 'Apache Felix 4 with Web Console and Gogo', just add the Apache Felix Http whiteboard bundle and you're good to go. You can find your servlet at http://localhost:8080/hello

How it works. The @Component annotation makes your class a service (a Servlet service in this case due to the provide attribute). This is registered with the service property 'alias'. The Apache Felix Http Whiteboard bundle picks up these services and registers them as servlets. I do not think it can get any simpler than this.

Solution 2

I would like to follow up on the answer of Peter Kriens. With @Component annotations available in the OSGi specification, the example could look like this:

@Component(service = Servlet.class, property = { "osgi.http.whiteboard.servlet.pattern = /hello" })
public class HelloWorldServlet extends HttpServlet { ... }

The @Component annotation is imported from org.osgi.service.component and the property that specifies the implemented service has changed its name to service.

Despite its name, property can hold multiple properties for example

@Component(service = ..., property = { "a=b", "c=d" })

or you could use properties to specify one or more properties files like so:

@Component(service = ..., properties = { "OSGI-INF/servlet.properties" } )

The above has been tested with the HttpService that comes with Apache Felix. The documentation of the Apache Felix HTTP Service can be found here: http://felix.apache.org/documentation/subprojects/apache-felix-http-service.html

Solution 3

Check this, may be can help you Create a servlet that accesses an OSGi service

Solution 4

You may find the following tutorial helpful: http://www.javabeat.net/2011/11/writing-an-osgi-web-application/. It's based on chapter two of Enterprise OSGi in Action. Chapter eight also has a discussion of how to use build tools like maven to get the right bundle structure, and http://coding.alasdair.info/2011/01/creating-web-application-bundle-using.html also has really helpful maven instructions.

At a high level, your best route is probably to take advantage of something like Apache Aries or Eclipse Gemini to allow you to run a WAB (a web bundle). A WAB is structured almost exactly like a WAR, except that the manifest has OSGi metadata in it. Your servlet class itself would be identical to the non-OSGi case. The framework will handle discovering and launching your servlet.

Solution 5

To answer your question, since Karaf (FUSE ESB) uses Pax Web as it's default Web-Container take a look at Pax Web for more details how it works and probably best for you at the more than 100 integration tests of Pax Web to give you an Idea on how to use it. There are also samples available to show you how to use either std. Http-Service, through Whiteboard-Extender or as WAR/WAB.

Share:
10,504
Li'
Author by

Li'

Recently interested in android development

Updated on June 11, 2022

Comments

  • Li'
    Li' about 2 years

    I want to create and deploy a web service to OSGi container. For example, publish the service to the address:

    http://localhost:8080/testservice. 
    

    The service generate HTML response in a servlet.

    I have searched a lot and got:

    public class HelloWorldServlet extends HttpServlet {
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hola</title>");
        out.println("</head>");
        out.println("<body bgcolor=\"white\">");
        out.println("</body>");
        out.println("</html>");
      }
    }
    

    The tool I need to use:

    1. maven to create the project

    2. Fuse ESB karaf as OSGi container

    The question is that I do not know how to use Maven to create and implement such web service, like:

    • how to specify webapp/web.xml

    • how to specify pom.xml: dependencies, package type, plugin

    • how to register service: implement BundlActivator or configure Spring xml file

    Can anyone help me with this? Is there a detailed tutorial for newbie?

  • ilikeorangutans
    ilikeorangutans about 11 years
    Is @Compoment enough? I always thought the Servlet would have to be declared as a \@Service as well.
  • Li'
    Li' about 11 years
    I need to use FUSE ESB Enterprise. Can not switch to Apache Felix. Is Apache Felix similar to Apache Karaf?
  • Neil Bartlett
    Neil Bartlett about 11 years
    @ilikeorangutans Yes it's enough. Notice the "provide=Servlet.class" attribute.
  • Neil Bartlett
    Neil Bartlett about 11 years
    @Li' Both FUSE ESB and Karaf are built on top of Apache Felix. So you don't have to switch at all. Anyway all this works on ANY framework such as Equinox and Knopflerfish as well. Welcome to OSGi, we take reusability and portability seriously!!
  • ilikeorangutans
    ilikeorangutans about 11 years
    @NeilBartlett I was unaware of this attribute. It's not documented on felix.apache.org/documentation/subprojects/… . Is it a new enhancement?
  • Li'
    Li' about 11 years
    @NeilBartlett I want to try what you said. But I have some problems: what package/jar does @ Component need? By bndtools, do you mean maven bundle plugin? I need to add Apache Felix Http whiteboard bundle to pom.xml or deploy it to osgi container? Can you give more details? Thanks
  • Neil Bartlett
    Neil Bartlett about 11 years
    @ilikeorangutans You're reading the wrong document, see aqute.biz/Bnd/Components instead.
  • Achim Nierbeck
    Achim Nierbeck about 11 years
    FUSE ESB and Karaf use Pax Web as the underlying implementation of the HTTPService, still the answer of Peter is correct also for this implementation of the HTTPService. Pax Web though does give you a couple of more features which aren't available with the std. HTTPService, e.g. WhiteBoard Extender which also is capable of beeing used for Filters, JSPs and other Resources. Not to mention that you're able to deploy std. wars in a Karaf.
  • ilikeorangutans
    ilikeorangutans about 11 years
    @NeilBartlett now that makes much more sense. Thanks!