问题
I'm using the Apache Felix HTTP Jetty bundle. I can start the servlet container / HttpService and assign it to a port using the system property
org.osgi.service.http.port=8080
But I would like to have two such services, bound to two different ports, eg my application API should be served on 8080, but some static web pages should be served on port 80.
- How can I start the HTTP service a second time on a different port?
- When registering servlets, how can I know which service is bound to which port?
Edit: for that last question, the service comes with the port value set in a property org.osgi.service.http.port: 8080
回答1:
If you switch over to Pax-Web you'll be able to do something like it. Pax-Web supports Virtual Hosts, and let's you bind a certain application to a single connection. Some more details on how to use it you'll find at my two blog posts:
Bind web applications to specific http connectors - part 1
Bind web applications to specific http connectors - part 2
回答2:
As far as I know this is not possible with the Http servers in OSGi I've worked with. Creating a little forwarder between the ports is of course quite easy to do in Java.
回答3:
I believe you can if the Apache Felix HTTP Jetty bundle exposes a ManagedServiceFactory. (EDIT: It does not)
How to support virtual hosts with OSGI HttpService
http://www.codeaffine.com/2011/10/05/raprwt-osgi-integration-ii/
If I understand, the magic is in here:
http://www.osgi.org/javadoc/r2/org/osgi/service/cm/ConfigurationAdmin.html#createFactoryConfiguration(java.lang.String)
If you create and register a Configuration using the pid of the managed service factory, it will create a new instance of HttpService using a new Pid.
回答4:
Felix http.jetty 3.2.6 provides a ManagedServiceFactory, which makes it easy to create multiple http instances with different configurations.
来源:https://stackoverflow.com/questions/20074211/osgi-http-bundle-bind-to-two-ports