Java “如何使码头动态加载”;“静态”;页
我正在构建JavaWeb应用程序,我讨厌传统的“代码编译部署测试”周期。我想输入一个微小的更改,然后立即看到结果,而无需编译和部署 幸运的是,这是伟大的。它是一个纯java web服务器。它提供了一个非常好的接口,可以让您直接从构建树启动Jetty reading,无需打包war文件或部署。它甚至还有一个scanInterval设置:将其设置为非零值,它将监视java文件和各种配置文件的更改,并在您进行更改后几秒钟自动重新部署 只有一件事让我远离涅盘。我的src/main/webapp目录中有javascript和css文件,Jetty刚刚提供了这些文件。我希望能够编辑这些内容,并在浏览器中刷新页面时显示更改。不幸的是,Jetty将这些文件保持打开状态,因此我无法(在Windows上)在运行时修改它们Java “如何使码头动态加载”;“静态”;页,java,spring,jetty,Java,Spring,Jetty,我正在构建JavaWeb应用程序,我讨厌传统的“代码编译部署测试”周期。我想输入一个微小的更改,然后立即看到结果,而无需编译和部署 幸运的是,这是伟大的。它是一个纯java web服务器。它提供了一个非常好的接口,可以让您直接从构建树启动Jetty reading,无需打包war文件或部署。它甚至还有一个scanInterval设置:将其设置为非零值,它将监视java文件和各种配置文件的更改,并在您进行更改后几秒钟自动重新部署 只有一件事让我远离涅盘。我的src/main/webapp目录中有j
有人知道如何让Jetty放弃这些文件,以便我可以编辑它们,然后为后续请求提供编辑后的文件吗?可能是浏览器保留了这些文件 在工具|互联网选项|临时互联网文件>设置中,单击单选按钮“每次访问页面”。按OK
执行此操作之前,请删除所有临时internet文件。Jetty使用内存映射文件缓冲静态内容,这会导致文件在Windows中锁定。尝试将
DefaultServlet的useFileMappedBuffer
设置为false
有说明。虽然上面的一个答案完全适用于通过xml配置jetty,但如果您想在代码中配置此选项(对于嵌入式服务器),答案是不同的,并且在该页面上找不到
你会在网上找到许多建议,包括
context.getInitParams().put(“useFileMappedBuffer”,“false”)
或者重写WebAppContext,或者为init参数使用完全限定的名称。这些建议都不适用于我(使用Jetty 7.2.2)。部分问题是需要在WebAppContext用于服务静态文件的servlet上设置useFileMappedBuffer选项,而不是在上下文上设置
最后,我在一个简单的ServletContextHandler上做了类似的事情
// Startup stuff
final Server server = new Server(port);
ServletContextHandler handler = new ServletContextHandler();
handler.setResourceBase(path);
SessionManager sm = new HashSessionManager();
SessionHandler sh = new SessionHandler(sm);
handler.setSessionHandler(sh);
DefaultServlet defaultServlet = new DefaultServlet();
ServletHolder holder = new ServletHolder(defaultServlet);
holder.setInitParameter("useFileMappedBuffer", "false");
handler.addServlet(holder, "/");
server.setHandler(handler);
server.start();
server.join();
虽然这是一个老问题,但我发现帖子非常有用,简而言之,只需将配置更改为
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<connectors>
<connector implementation="org.eclipse.jetty.server.bio.SocketConnector">
<port>8080</port>
</connector>
</connectors>
</configuration>
</plugin>
org.mortbay.jetty
jetty maven插件
8080
这将禁用Jetty中的NIO支持(但对于简单情况下的调试puropse,这应该不是问题)。当使用嵌入式Jetty 8.1.10时,“useFileMappedBuffer=false”设置在任何模式下都不起作用。我阅读了DefaultServlet
的代码,它读取了属性,但没有用于任何用途
相反,我查看了缓冲区创建的配置位置,发现我可以子类化SelectChannelConnector
,以获得继续的好处,但不需要在windows上锁定文件。如果您只是使用org.mortbay.jetty.bio.SocketConnector
,那么您将无法获得继续支持
以下是我的例子:
import org.eclipse.jetty.io.Buffers.Type;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
/**
* A Connector that has the advantages NIO, but doesn't lock files in Windows by
* avoiding memory mapped buffers.
* <p>
* It used to be that you could avoid this problem by setting "useFileMappedBuffer" as described in
* http://stackoverflow.com/questions/184312/how-to-make-jetty-dynamically-load-static-pages
* However that approach doesn't seem to work in newer versions of jetty.
*
* @author David Roussel
*
*/
public class SelectChannelConnectorNonLocking extends SelectChannelConnector {
public SelectChannelConnectorNonLocking() {
super();
// Override AbstractNIOConnector and use all indirect buffers
_buffers.setRequestBufferType(Type.INDIRECT);
_buffers.setRequestHeaderType(Type.INDIRECT);
_buffers.setResponseBufferType(Type.INDIRECT);
_buffers.setResponseHeaderType(Type.INDIRECT);
}
}
import org.eclipse.jetty.io.Buffers.Type;
导入org.eclipse.jetty.server.nio.SelectChannelConnector;
/**
*一种连接器,具有NIO的优点,但不会通过锁定Windows中的文件
*避免内存映射缓冲区。
*
*过去,您可以通过设置“useFileMappedBuffer”来避免此问题,如中所述
* http://stackoverflow.com/questions/184312/how-to-make-jetty-dynamically-load-static-pages
*然而,这种方法在较新版本的jetty中似乎不起作用。
*
*@作者大卫·鲁塞尔
*
*/
公共类SelectChannelConnectorNoLocking扩展SelectChannelConnector{
public SelectChannelConnectorNonLocking(){
超级();
//重写AbstractNIOConnector并使用所有间接缓冲区
_setRequestBufferType(Type.INDIRECT);
_setRequestHeaderType(Type.INDIRECT);
_setResponseBufferType(Type.INDIRECT);
_setResponseHeaderType(Type.INDIRECT);
}
}
我已经测试了这个锁定问题,它解决了这个问题。我还没有测试它是否能与Continuations一起工作。在webdefault.xml中将false设置为useFileMappedBuffer对我不起作用(Jetty 8.1.10.v20130312)。
幸运的是,将maxCachedFiles设置为0(也在webdefault.xml中)成功了。类似于@kybernetikos answer,但无需重新创建DefaultServlet:
// Startup stuff
final Server server = new Server(port);
WebAppContext webAppContext = new WebAppContext(path, "/")
webAppContext.setInitParam(
"org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
server.setHandler(webAppContext);
server.start();
server.join();
DefaultServlet将查找自己的useFileMappedBuffer副本,该副本似乎设置在Jetty的深处。但是,通过在属性名称前面加上如上所述的前缀,这个值是首选的。给出了一个Jetty嵌入式示例,使用ResourceHandler
而不是servlet来服务静态文件:
// Create a basic Jetty server object that will listen on port 8080. Note that if you set this to port 0
// then a randomly available port will be assigned that you can either look in the logs for the port,
// or programmatically obtain it for use in test cases.
Server server = new Server(8080);
// Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
// a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
ResourceHandler resource_handler = new ResourceHandler();
// Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
// In this example it is the current directory but it can be configured to anything that the jvm has access to.
resource_handler.setDirectoriesListed(true);
resource_handler.setWelcomeFiles(new String[]{ "index.html" });
resource_handler.setResourceBase(".");
// Add the ResourceHandler to the server.
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
server.setHandler(handlers);
// Start things up! By using the server.join() the server thread will join with the current thread.
// See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
server.start();
server.join();
Jetty使用NIO(内存中文件映射),因此。这是一个已知的问题,可以为servlet找到许多解决方法
但是,由于此示例不依赖servlet,因此基于webapp参数(useFileMappedBuffer、maxCachedFiles)的关联答案不起作用
为了防止内存中的文件映射,您需要添加以下配置行:
resource_handler.setMinMemoryMappedContentLength(-1);
注:如Javadoc中所述(nimrodm注意到):使用内存映射缓冲区提供的文件资源的最小字节大小,或-1表示无内存映射缓冲区。然而,我在valueInteger.MAX\u value
上得到了相同的行为
设置此参数后,Jetty可以在Windows上提供静态文件,您可以编辑这些文件。使用IntelliJ时
<?xml version="1.0" encoding="UTF-8"?>
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Call name="setInitParameter">
<Arg>org.eclipse.jetty.servlet.Default.useFileMappedBuffer</Arg>
<Arg>false</Arg>
</Call>
</Configure>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<httpConnector>
<host>localhost</host>
<port>8801</port>
</httpConnector>
<webApp>
<contextPath>/${project.artifactId}</contextPath>
</webApp>
<contextXml>${project.basedir}/jetty/jetty-config.xml</contextXml>
</configuration>
</plugin>