Java 为Tomcat设置Servlet上下文初始化参数
我有一个Grails应用程序,它有一个简单的Websocket端点,如下所示:Java 为Tomcat设置Servlet上下文初始化参数,java,spring,tomcat,servlets,grails,Java,Spring,Tomcat,Servlets,Grails,我有一个Grails应用程序,它有一个简单的Websocket端点,如下所示: @WebListener @ServerEndpoint("/chatEndpoint/{chatId}") public class WebsocketChatroomEndpoint implements ServletContextListener { static ConfigObject config @Override public void contextInitialized(Serv
@WebListener
@ServerEndpoint("/chatEndpoint/{chatId}")
public class WebsocketChatroomEndpoint implements ServletContextListener {
static ConfigObject config
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.servletContext
ServerContainer serverContainer = servletContext.getAttribute("javax.websocket.server.ServerContainer")
try {
ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute(GA.APPLICATION_CONTEXT)
config = ctx.grailsApplication.config
serverContainer.defaultMaxSessionIdleTimeout = 0
} catch (IOException e) {
log.error(e.message, e)
}
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
@OnOpen
public void onOpen(Session userSession, @PathParam("chatId") String chatId) {
// Do some stuff
}
@OnMessage
public String onMessage(String message, Session userSession) throws IOException {
// Handle message received
}
@OnClose
public void onClose(Session userSession, CloseReason reason) {
// Handle close
}
@OnError
public void onError(Throwable t) {
// Handle error
}
}
端点工作正常,但我遇到的问题是,在部署到Tomcat 8时,空闲连接在60秒后断开
声明可以通过设置以下servlet上下文初始化参数来更改此值:org.apache.tomcat.websocket.executorKeepAliveTimeSeconds
我在web.xml文件中设置了以下参数:
<context-param>
<param-name>org.apache.tomcat.websocket.executorKeepAliveTimeSeconds</param-name>
<param-value>1000</param-value>
</context-param>
问题是,无论我将该值设置为什么,端点在60秒后仍然超时。我已经尝试在Tomcat的context.xml文件中设置该值,并在侦听器中以编程方式进行设置。有人知道如何覆盖Tomcat的默认值60秒吗?我已经解决了这个问题,在解决这个问题时,我显然是疯了 我有到Tomcat的nginx反向代理连接。Tomcat正确设置了该值,但nginx中的代理读取超时默认为60秒。在我的nginx站点配置中增加此值修复了此问题
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.servletContext
// Logs "1000"
log.debug(servletContext.getInitParameter("org.apache.tomcat.websocket.executorKeepAliveTimeSeconds"))
}