问题描述
在没有指定上下文且没有上下文处理程序的情况下启动码头实例是否正确,然后在服务器启动后继续向其添加上下文.尽管我可以使用可变的HandlerCollection来做到这一点,并且日志显示Server和Contexts已启动并且可用,但是我无法使用URL访问它.还是应该在启动服务器时向服务器添加至少一个根上下文和上下文处理程序?
Is it right to start a jetty instance with no context specified and no context handler, then keep adding context to it once the server has started. Although I was able to do this using mutable HandlerCollection and the logs says the Server and the Contexts are started and available, I am not able to access it with the URL. Or should we add at least one root context and contexthandler to the server while starting it?
我做了一些类似于下面链接中建议的示例的操作. Jetty 9(嵌入):在运行时添加处理程序
I did something similar to the example suggested in below link.Jetty 9 (embedded): Adding handlers during runtime
我的码头版本是9.3.7.v20160115
My jetty version is 9.3.7.v20160115
推荐答案
将处理程序添加到正在运行的服务器是一种常见的模式,但是文档并不十分清楚(嵌入式码头"教程中的所有示例均以服务器 之后.)AFAIK人员正在遵循以下方法:
the addition of handlers to a running server is a common pattern but the documentation is not clear at all (all the examples in the "embedding jetty" tutorial start the server after the configuration.) AFAIK people is following these approaches:
1)使用HandlerCollection(boolean mutableWhenRunning)ctor添加/删除处理程序
1) using the HandlerCollection(boolean mutableWhenRunning) ctor to add/remove handlers
2)显式添加并启动处理程序
2) add and start the handlers explicitly
我观察到在Jetty 9.1.4中并不需要#2,但在Jetty 9.2.14及更高版本中需要它(顺便说一下,这些版本号被Maven选为Jersey依赖项,与该问题完全无关.)例如:
I observed that #2 was not needed in Jetty 9.1.4, but it is on Jetty 9.2.14 and afterward (BTW these version numbers were picked by Maven as Jersey dependencies which is totally unrelated to this issue.) For example:
// after server creation ...
ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();
jettyServer.setHandler(contextHandlerCollection);
jettyServer.start();
// ...
ServletContextHandler newSCH= new ServletContextHandler(ServletContextHandler.SESSIONS);
newSCH.setResourceBase(System.getProperty("java.io.tmpdir"));
newSCH.setContextPath("/servlets");
ServletHolder newHolder = new SwServletHolder(servlet);
newSCH.addServlet(newHolder, "/*");
contextHandlerCollection.addHandler(newSCH);
try {
newSCH.start(); // needed from about 9.2
} catch (Exception e) {
logger.info("Exception starting ServletContextHandler for Jetty", e);
}
为了添加SOAP上下文,这是在9.1.4上可用"的代码(在9.2.14上它报告404):
In order to add a SOAP context this is a code that "used to work" on 9.1.4 (on 9.2.14 it reports 404):
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import org.eclipse.jetty.http.spi.JettyHttpServerProvider;
import org.eclipse.jetty.http.spi.HttpSpiContextHandler;
import org.eclipse.jetty.http.spi.JettyHttpContext;
import org.eclipse.jetty.http.spi.JettyHttpServer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import com.sun.net.httpserver.HttpContext;
public class JettyJaxWs {
public static void main(String[] args) throws Exception {
Server server = new Server(7777);
ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();
server.setHandler(contextHandlerCollection);
server.start();
HttpContext context = buildOrig(server, "/ws");
MyWebService ws = new MyWebService();
Endpoint endpoint = Endpoint.create(ws);
endpoint.publish(context);
// access wsdl on http://localhost:7777/ws/MyWebService?wsdl
}
@WebService
public static class MyWebService {
public String hello(String s) {
return "hi " + s;
}
}
public static HttpContext buildOrig(Server server, String contextString) throws Exception {
JettyHttpServerProvider.setServer(server);
return new JettyHttpServerProvider().createHttpServer(new InetSocketAddress(7777), 5).createContext(contextString);
}
后来,我必须在最后一种方法中执行此操作(不确定是否有更好的方法):
Later, I had to do this for the last method (not sure if there is a better way):
public static HttpContext buildNew(Server server, String contextString) {
JettyHttpServer jettyHttpServer = new JettyHttpServer(server, true);
JettyHttpContext ctx = (JettyHttpContext) jettyHttpServer.createContext(contextString);
try {
Method method = JettyHttpContext.class.getDeclaredMethod("getJettyContextHandler");
method.setAccessible(true);
HttpSpiContextHandler contextHandler = (HttpSpiContextHandler) method.invoke(ctx);
contextHandler.start();
} catch (Exception e) {
e.printStackTrace();
}
return ctx;
}
这篇关于嵌入式Jetty-启动Jetty服务器后添加上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!