我可以通过创建两个 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 产生了预期的第三个服务端点问候页面。
为了完成代码示例(如果您已经拥有可用的服务实现对象,则不是必需的),这是我使用的服务端点接口(三个实现的接口相同):
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);
}
}