How to use servlet with OSGi

偶尔善良 提交于 2019-11-30 23:27:36

问题


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?


回答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.




回答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




回答3:


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




回答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.




回答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.




回答6:


I guess you need a servlet bridge to access the service. Your service should be implemented as an OSGI bundle; servlet bridge has to have embedded OSGI framework. Follow this sample for details: http://vbashur.blogspot.kr/2014/07/osgi-servlet-bridge-sample.html



来源:https://stackoverflow.com/questions/16313650/how-to-use-servlet-with-osgi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!