我正在尝试向码头注入自定义处理程序。
我已经在包装为战争的应用程序代码中编写了处理程序。
package com.foo.bar
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import org.eclipse.jetty.server.Request
import org.eclipse.jetty.server.handler.AbstractHandler
// scalastyle:off println
class CustomJettyHandler extends AbstractHandler {
override def handle(target: String, baseRequest: Request,
request: HttpServletRequest, response: HttpServletResponse): Unit = {
println("This is a custom jetty handler")
}
}
// scalastyle:on println
然后我将此处理程序注入到jetty.xml
文件中:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
........
........
<Call name="insertHandler">
<Arg>
<New id="CustomJettyHandler" class="com.foo.bar.CustomJettyHandler"/>
</Arg>
</Call>
........
我现在以独立模式运行码头。请注意,我正在将CustomJettyHandler.class
所在的位置传递给jetty-start.jar
。
java -server -Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog -jar lib/jetty-start.jar OPTIONS=All --lib=lib/* --lib=webapps/root/WEB-INF/classes/com/foo/bar/* etc/jetty.xml etc/jetty-jmx.xml --debug
在我的应用程序日志中,我可以看到码头将我的自定义处理程序加载到其类路径,但是由于ClassNotFoundException
而最终失败。有人可以指出这可能出问题吗?
.......
rawlibref = webapps/root/WEB-INF/classes/com/foo/bar/*
expanded = webapps/root/WEB-INF/classes/com/foo/bar/*
getPaths('webapps/root/WEB-INF/classes/com/foo/bar/*')
Using relative path pattern: glob:**/webapps/root/WEB-INF/classes/com/foo/bar/*
Found [webapps/root/WEB-INF/classes/com/foo/bar/CustomJettyHandler.class] /Users/...path_to_application.../webapps/root/WEB-INF/classes/com/foo/bar/CustomJettyHandler.class
Adding classpath component: /Users/...path_to_application.../webapps/root/WEB-INF/classes/com/foo/bar/CustomJettyHandler.class
.......
.......
.......
URLClassLoader.url[33] = file:/Users/...path_to_application.../webapps/root/WEB-INF/classes/com/foo/bar/CustomJettyHandler.class
Loaded 34 URLs into URLClassLoader
class org.eclipse.jetty.xml.XmlConfiguration - 9.4.24.v20191120
Command Line Args: /var/folders/z5/dmt38gq54kxcrrzpgvbl5m_c0000gp/T/start_6046998329479549547.properties /Users/...path_to_application.../etc/jetty.xml /Users/...path_to_application.../etc/jetty-jmx.xml
2020-05-27 14:46:13.676:INFO::main: Logging initialized @477ms to org.eclipse.jetty.util.log.StdErrLog
2020-05-27 14:46:13.934:INFO:oeju.TypeUtil:main: JVM Runtime does not support Modules
2020-05-27 14:46:14.007:WARN:oejx.XmlConfiguration:main: Config error at <Call name="insertHandler"><Arg>| <New id="CustomJettyHandler" class="com.foo.bar.CustomJettyHandler"/>| </Arg></Call> java.lang.ClassNotFoundException: com.foo.bar.CustomJettyHandler in file:///Users/...path_to_application.../etc/jetty.xml
2020-05-27 14:46:14.007:WARN:oejx.XmlConfiguration:main:
java.security.PrivilegedActionException: java.lang.ClassNotFoundException: com.foo.bar.CustomJettyHandler
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1837)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.jetty.start.Main.invokeMain(Main.java:218)
at org.eclipse.jetty.start.Main.start(Main.java:491)
at org.eclipse.jetty.start.Main.main(Main.java:77)
Caused by:
java.lang.ClassNotFoundException: com.foo.bar.CustomJettyHandler
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.eclipse.jetty.util.Loader.loadClass(Loader.java:64)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:1028)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.itemValue(XmlConfiguration.java:1638)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.value(XmlConfiguration.java:1539)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.access$500(XmlConfiguration.java:369)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration$AttrOrElementNode.getList(XmlConfiguration.java:1768)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration$AttrOrElementNode.getList(XmlConfiguration.java:1744)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.call(XmlConfiguration.java:919)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:512)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:454)
at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:354)
at org.eclipse.jetty.xml.XmlConfiguration.lambda$main$0(XmlConfiguration.java:1874)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1837)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.jetty.start.Main.invokeMain(Main.java:218)
at org.eclipse.jetty.start.Main.start(Main.java:491)
at org.eclipse.jetty.start.Main.main(Main.java:77)
您从哪里获得此命令行的?
java -server \
-Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog\
-jar lib/jetty-start.jar\
OPTIONS=All \
--lib=lib/* \
--lib=webapps/root/WEB-INF/classes/com/foo/bar/* \
etc/jetty.xml \
etc/jetty-jmx.xml \
--debug
不适用于Jetty 9.x的start.jar
一些建议
start.jar
和jetty-home
模块系统的责任(顺序是超级重要)。您选择的etc/jetty.xml and etc/jetty-jmx.xml
是xml的不完整列表。 (您缺少所有相关的XML文件)OPTIONS
不被Jetty 9.x支持(这是老式的Codehaus / Jetty 6行为)]--lib=
,它仅支持带有分解类树的jar或目录的完全限定路径(不支持相对路径,不支持glob)。 -Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
是设置日志记录的严格方法。创建一个jetty-logging.properties
文件,并确保它在类路径中。jetty-logging.properties
的示例内容
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
org.eclipse.jetty.LEVEL=INFO
#org.eclipse.jetty.deploy.LEVEL=DEBUG
为新处理程序创建基于注入的XML。
my-handler.xml
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call name="insertHandler">
<Arg>
<New id="CustomJettyHandler" class="com.foo.bar.CustomJettyHandler" />
</Arg>
</Call>
</Configure>
下一步,正确创建您的码头基目录(对于码头的start.jar
)
# Create your "jetty-base" directory
$ mkdir /path/to/myjettybase
$ cd /path/to/myjettybase
# Establish the basic files / directories / modules that you want to use
# You can find the configuration in start.ini or start.d/*.ini
$ java -jar /path/to/jetty-home/start.jar --add-to-start=http,jmx,deploy,ext,resources
# Copy your custom handler JAR into place
$ cp /path/to/my-handlers.jar /path/to/myjettybase/lib/ext/
# Copy your custom handler XML into place
$ cp /path/to/my-handler.xml /path/to/myjettybase/etc/
# Ensure that the custom handler XML is loaded into the jetty instance at the right point in the XML load order by declaring it to be used in a custom INI
$ mkdir start.d
$ echo "etc/my-handler.xml" >> start.d/my-handlers.ini
# Copy your jetty-logging.properties into place
$ cp /path/to/my-jetty-logging.properties /path/to/myjettybase/resources/jetty-logging.properties
# verify that your configuration looks sane (including the server classpath)
$ cd /path/to/myjettybase
$ java -jar /path/to/jetty-home/start.jar --list-config
# run your instance
$ cd /path/to/myjettybase
$ java -jar /path/to/jetty-home/start.jar
但这还不是全部,因为您似乎想从Maven风格的项目(或项目布局)中使用jetty-home
,所以您也可以这样做!
显示此内容的示例项目可在...上找到
https://github.com/jetty-project/servlet-error-page-handling
该maven项目也是一个有效的jetty-base
目录,适合由您计算机上其他位置的jetty-home
存档执行。