Reputation: 11909
We are using Jetty 9.3 embedded and publishing multiple webservice Endpoints (using Endpoint.publish). I'm however unsure how to publish some of them on a different TCP port.
Endpoint publishing:
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);
Expecting: Make Service 1 and 2 available on port 8081 and only 8081 and Service3 on port 8082 and only 8082.
Actual: All three services available on both TCP ports.
Setting up to use our embedded jetty using:
JettyHttpServerProvider.setServer(jettyWebServer);
System.setProperty("com.sun.net.httpserver.HttpServerProvider", JettyHttpServerProvider.class.getName());
For completeness, jetty web server init:
jettyWebServer = new Server(new DelegatingThreadPool(new QueuedThreadPool()));
ServerConnector connector = new ServerConnector(jettyWebServer);
//connector.setHost();
connector.setPort(8081);
jettyWebServer.addConnector(connector);
Had to add this for second port else Endpoint publishing won't work for Service3:
connector = new ServerConnector(jettyWebServer);
//connector.setHost(...);
connector.setPort(8082);
jettyWebServer.addConnector(connector);
ContextHandlerCollection contexts = new ContextHandlerCollection();
jettyWebServer.setHandler(contexts);
But then all three are available on both port 8081 and 8082. Can't set up more than one jetty server instance to handle this it seems, only a single set method available: JettyHttpServerProvider.setServer(...)
, no add or similar.
Any help appreciated.
Upvotes: 3
Views: 1715
Reputation: 3424
I was able to get this working by creating two Jetty Server
instances and setting the global JettyHttpServerProvider.server
static field with the different instances as follows:
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);
}
}
Upon running this main method I observed the following printed to standard out:
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
Requesting http://localhost:8081/services/Service1 and http://localhost:8081/services/Service2 yielded the expected web service endpoint greeting page, while http://localhost:8081/services/Service3 yielded an HTTP 404
. Requesting http://localhost:8082/services/Service3 yielded the expected third service endpoint greeting page.
To complete the code sample (though not necessary if you already have your service impl objects available), here is the service endpoint interface I used (same interface for three impls):
package com.scotth.jettypublish.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
String sayHello(String name);
}
Here are the three web service implementations used in the Endpoint.publish()
calls demonstrated:
One:
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);
}
}
Two:
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);
}
}
Three:
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);
}
}
Upvotes: 3