eclipse Filter web.xml 问题解决 异步请求@WebServlet

<filter>
<filter-name>AsynServlete</filter-name>
<filter-class>com.kad.app.action.AsynServletT</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>AsynServlete</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>

http://localhost:8080/AppService/asyncServlet/testAsyn?

用上边地址请求, 爆错,

严重: Servlet.service() for servlet [applicationContext] in context with path [/AppService] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.FilterChain]: Specified class is an interface] with root cause
org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.FilterChain]: Specified class is an interface
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:)

奇怪了这个异步以前用着很正常的, 就因为我注释了一下再 打开, 出了问题......这个问题磨蹭到下午..............

起先是部署到resin 里边,程序爆错. 中间担心resin 的支持servlet 3.0 跟tomcat 有区别. 我想换成另一种不在web.xml  filter 的方式,  
@WebServlet(asyncSupported=true,urlPatterns="/asyncComplete")   这种多方便 不用一个个写过滤的, 结果这种方式也也需要继承一个接口HttpServlet.

还是换成webserlet的方式吧...

后来发现web filter 过滤的方式,eclipse Filter  web.xml 问题解决 异步请求@WebServlet过滤url-pattern 后边的/需要加上*号   .还是建议 用webservlet的方式

这种异步有两种实现方式,一个是另起一个异步线程,另一种是线程池,请求过来的链接放到线程池里边处理,这种一般应用于并发比较大的情况.

为了保留过滤的方式 在此记录上过滤的方式  先配置web.xml 设置一条过滤规则

com.kad.app.action.AsynServlet  这个是项目内接受异步请求的类
<filter>
<filter-name>AsynServlete</filter-name>
<filter-class>com.kad.app.action.AsynServlet</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>AsynServlete</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> 还得把servlet 下加上

<servlet>
<servlet-name>applicationContext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-common.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>

下边

/*package com.kad.app.action;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.AsyncContext;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet; import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.google.common.eventbus.Subscribe;
import com.google.gson.Gson;
import com.kad.app.action.eventregservice.MessageEventReg;
import com.kad.app.redis.impl.RedisImpl;
import com.kad.dev.manage.DevNoOne;
import com.kad.guavaevents.messge.RTUnLockMessage;
import com.kad.netty.service.kdNettyServer; @Controller
@RequestMapping("/asyncServlet")
public class AsynServlet implements Filter
{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub }
@RequestMapping(value="/testAsyn")
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
// System.out.println("start");
PrintWriter out = response.getWriter();
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(5000);
asyncContext.addListener(new MyAsyncListener());
Work work=new Work(asyncContext,request);
new Thread(work).start();
response = asyncContext.getResponse();
// PrintWriter out1 = response.getWriter(); out.print("异步执行中");
}
@Override
public void destroy() {
} class Work implements Runnable
{ private AsyncContext asyncContext;
private ServletRequest request;
public Work(AsyncContext asyncContext,ServletRequest request)
{
this.asyncContext = asyncContext;
this.request = request;
// EventBusCenter.register(this);
} @Override
public void run()
{ ServletResponse response = asyncContext.getResponse();
PrintWriter out;
try {
out = response.getWriter();
out.println("后台线程执行完成");
out.close();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} String devid =request.getParameter("devid"); // PrintWriter out = response.getWriter();
boolean sign=false;
DevNoOne dev= DevNoOne.devone;
RedisImpl redis =new RedisImpl();
while(true){
try {
// out.println("后台线程执行完成");
Thread.sleep(100);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String mess=null;
mess= redis.get(devid);
if(mess!=null) {
if(request.getAsyncContext() != null && asyncContext.getTimeout()>0){
// try {
ServletResponse response = asyncContext.getResponse();
PrintWriter out = response.getWriter();
out.println("后台线程执行完成");
out.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
asyncContext.complete();
break;
}
}
}
}
}
} */

这种似乎是一种土方子....  中间还必须加

一下是第二种方式  简单明了,继承 extends HttpServlet   ,重写doGet 或doPost 也就是你用get方法请求就重写doget ,用post 就重写dopost  
下边程序异步work类里边逻辑没写好,不影响测试.最后的报错跟异步无关.先写上来.调试过程中间有个报错 用post方式请求. 半道出家基础不牢靠啊...
 Status  - HTTP method POST is not supported by this URL

这是指重写了doget 没重写dopost导致......

package com.kad.app.action;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; @WebServlet(asyncSupported=true,urlPatterns="/asyncweb")
public class AsynWebServlet extends HttpServlet{ /**
*
*/
private static final long serialVersionUID = 1L; /* @Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
// request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
System.out.println("start");
PrintWriter out = response.getWriter(); AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(500000);
asyncContext.addListener(new MyAsyncListener());
Work work=new Work(asyncContext,request);
new Thread(work).start();
out.print("异步执行中"); }*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("start");
PrintWriter out = response.getWriter(); AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout();
asyncContext.addListener(new MyAsyncListener());
Work work=new Work(asyncContext,request);
new Thread(work).start();
out.print("异步执行中");
} class Work implements Runnable
{ private AsyncContext asyncContext;
private ServletRequest request; public Work(AsyncContext asyncContext,ServletRequest request)
{
this.asyncContext = asyncContext;
this.request = request;
} @Override
public void run() { if(request.getAsyncContext() != null && asyncContext.getTimeout()>){
try {
ServletResponse response = asyncContext.getResponse();
PrintWriter out = response.getWriter();
out.println("后台线程执行完成");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
asyncContext.complete();
} }
} }

周日传到服务器上

出现了500 错误,

HTTP Status  – Internal Server Error

Type Exception Report

Message Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:)
javax.servlet.http.HttpServlet.service(HttpServlet.java:)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:)
javax.servlet.http.HttpServlet.service(HttpServlet.java:)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal

服务器内部错误, 最终是因为 本地测试用的数据库的表名与线上的表名名字大小写存在不同...linux 对大小写很敏感....

上一篇:解决React Native unable to load script from assets index.android.bundle on windows


下一篇:c语言定义函数指针和typedef简写