我们正在使用Jetty 9.3嵌入式和发布多个Web服务端点(使用Endpoint.publish)。但我不确定如何在不同的TCP端口上发布其中一些。
端点发布:
endpoint = Endpoint.publish("http://0.0.0.0:8081/services/Service1"), service1);
endpoint = Endpoint.publish("http://0.0.0.0:8081/services/Service2"), service2);
endpoint = Endpoint.publish("http://0.0.0.0:8082/services/Service3"), service3);
期待:使端口8081上的服务1和2可用,端口8082上只有8081和Service3,而且只有8082。
实际:两个TCP端口都可用的所有三种服务。
设置使用我们的嵌入式码头:
JettyHttpServerProvider.setServer(jettyWebServer);
System.setProperty("com.sun.net.httpserver.HttpServerProvider", JettyHttpServerProvider.class.getName());
为了完整性,jetty web server init:
jettyWebServer = new Server(new DelegatingThreadPool(new QueuedThreadPool()));
ServerConnector connector = new ServerConnector(jettyWebServer);
//connector.setHost();
connector.setPort(8081);
jettyWebServer.addConnector(connector);
不得不为第二个端口添加此端点,端点发布将不适用于Service3:
connector = new ServerConnector(jettyWebServer);
//connector.setHost(...);
connector.setPort(8082);
jettyWebServer.addConnector(connector);
ContextHandlerCollection contexts = new ContextHandlerCollection();
jettyWebServer.setHandler(contexts);
但是这三个端口8081和8082都可以使用。无法设置多个jetty服务器实例来处理这个问题,只有一套方法可用:JettyHttpServerProvider.setServer(...)
,没有添加或类似
任何帮助表示感谢。
答案 0 :(得分:3)
我能够通过创建两个Jetty Server
实例并使用不同的实例设置全局JettyHttpServerProvider.server
静态字段来实现此功能,如下所示:
package com.scotth.jettypublish;
import javax.xml.ws.Endpoint;
import org.eclipse.jetty.http.spi.DelegatingThreadPool;
import org.eclipse.jetty.http.spi.JettyHttpServerProvider;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import com.scotth.jettypublish.ws.HelloWorld;
import com.scotth.jettypublish.ws.impl.HelloWorldImplOne;
import com.scotth.jettypublish.ws.impl.HelloWorldImplThree;
import com.scotth.jettypublish.ws.impl.HelloWorldImplTwo;
public class PublishTesterMain {
public static void main(String[] args) throws Exception {
Server ws1 = new Server(new DelegatingThreadPool(new QueuedThreadPool()));
ServerConnector connector = new ServerConnector(ws1);
connector.setPort(8081);
ws1.addConnector(connector);
ContextHandlerCollection contexts = new ContextHandlerCollection();
ws1.setHandler(contexts);
Server ws2 = new Server(new DelegatingThreadPool(new QueuedThreadPool()));
ServerConnector connector2 = new ServerConnector(ws2);
connector2.setPort(8082);
ws2.addConnector(connector2);
ContextHandlerCollection contexts2 = new ContextHandlerCollection();
ws2.setHandler(contexts2);
System.setProperty("com.sun.net.httpserver.HttpServerProvider", JettyHttpServerProvider.class.getName());
HelloWorld service1 = new HelloWorldImplOne();
HelloWorld service2 = new HelloWorldImplTwo();
JettyHttpServerProvider.setServer(ws1);
Endpoint.publish("http://0.0.0.0:8081/services/Service1", service1);
Endpoint.publish("http://0.0.0.0:8081/services/Service2", service2);
ws1.start();
HelloWorld service3 = new HelloWorldImplThree();
JettyHttpServerProvider.setServer(ws2);
Endpoint.publish("http://0.0.0.0:8082/services/Service3", service3);
ws2.start();
Thread.sleep(Long.MAX_VALUE);
}
}
运行这个主要方法后,我观察到以下印刷标准:
2016-05-31 22:38:54.818:INFO::main: Logging initialized @212ms
2016-05-31 22:38:55.807:INFO:oejs.Server:main: jetty-9.3.z-SNAPSHOT
2016-05-31 22:38:55.870:INFO:oejsh.ContextHandler:main: Started o.e.j.h.s.HttpSpiContextHandler@53045c6c{/services/Service1,null,AVAILABLE}
2016-05-31 22:38:55.870:INFO:oejsh.ContextHandler:main: Started o.e.j.h.s.HttpSpiContextHandler@5149d738{/services/Service2,null,AVAILABLE}
2016-05-31 22:38:55.901:INFO:oejs.AbstractConnector:main: Started ServerConnector@7d9d0818{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
2016-05-31 22:38:55.902:INFO:oejs.Server:main: Started @1299ms
2016-05-31 22:38:55.939:INFO:oejs.Server:main: jetty-9.3.z-SNAPSHOT
2016-05-31 22:38:55.939:INFO:oejsh.ContextHandler:main: Started o.e.j.h.s.HttpSpiContextHandler@5f8e8a9d{/services/Service3,null,AVAILABLE}
2016-05-31 22:38:55.941:INFO:oejs.AbstractConnector:main: Started ServerConnector@17bffc17{HTTP/1.1,[http/1.1]}{0.0.0.0:8082}
2016-05-31 22:38:55.942:INFO:oejs.Server:main: Started @1339ms
请求http://localhost:8081/services/Service1和http://localhost:8081/services/Service2产生了预期的Web服务端点问候页面,而http://localhost:8081/services/Service3产生了HTTP 404
。请求http://localhost:8082/services/Service3产生了预期的第三个服务端点问候页面。
要完成代码示例(如果您已经有可用的服务impl对象,则没有必要),这是我使用的服务端点接口(三个impls的相同接口):
package com.scotth.jettypublish.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
String sayHello(String name);
}
以下是演示的Endpoint.publish()
调用中使用的三种Web服务实现:
一:
package com.scotth.jettypublish.ws.impl;
import javax.jws.WebService;
import com.scotth.jettypublish.ws.HelloWorld;
@WebService(endpointInterface = "com.scotth.jettypublish.ws.HelloWorld")
public class HelloWorldImplOne implements HelloWorld {
@Override
public String sayHello(String name) {
return "ONE Hello " + (name == null ? "World" : name);
}
}
二:
package com.scotth.jettypublish.ws.impl;
import javax.jws.WebService;
import com.scotth.jettypublish.ws.HelloWorld;
@WebService(endpointInterface = "com.scotth.jettypublish.ws.HelloWorld")
public class HelloWorldImplTwo implements HelloWorld {
@Override
public String sayHello(String name) {
return "TWO Hello " + (name == null ? "World" : name);
}
}
三:
package com.scotth.jettypublish.ws.impl;
import javax.jws.WebService;
import com.scotth.jettypublish.ws.HelloWorld;
@WebService(endpointInterface = "com.scotth.jettypublish.ws.HelloWorld")
public class HelloWorldImplThree implements HelloWorld {
@Override
public String sayHello(String name) {
return "THREE Hello " + (name == null ? "World" : name);
}
}