我正在使用泽西2.41 / Java 7 / tomcat 7部署Server-Sent-Event资源。
我收到错误
2013年12月16日上午4:04:40 org.apache.catalina.core.StandardWrapperValve 调用SEVERE:servlet中的Servlet.service()[ServletAdaptor] 路径[/ trackapp]的上下文引发了异常 [java.lang.UnsupportedOperationException:不是异步处理 支持Servlet 2.x容器。]有根本原因 java.lang.UnsupportedOperationException:不是异步处理 Servlet 2.x容器支持。在 org.glassfish.jersey.servlet.WebComponent $ 3.suspend(WebComponent.java:123) 在 org.glassfish.jersey.servlet.internal.ResponseWriter.suspend(ResponseWriter.java:109) 在 org.glassfish.jersey.server.ServerRuntime $ Responder.writeResponse(ServerRuntime.java:619) 在 org.glassfish.jersey.server.ServerRuntime $ Responder.processResponse(ServerRuntime.java:381) 在 org.glassfish.jersey.server.ServerRuntime $ Responder.process(ServerRuntime.java:371) 在 org.glassfish.jersey.server.ServerRuntime $ 1.run(ServerRuntime.java:262) 在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:271)at at org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:267)at org.glassfish.jersey.internal.Errors.process(Errors.java:315)at org.glassfish.jersey.internal.Errors.process(Errors.java:297)at org.glassfish.jersey.internal.Errors.process(Errors.java:267)at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318) 在 org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236) 在 org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:983) 在 org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:361) 在 org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372) 在 org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335) 在 org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001) 在 org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:585) 在 org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:312) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615) 在java.lang.Thread.run(Thread.java:744)
我的资源
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String broadcastMessage(String message) {
OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
OutboundEvent event = eventBuilder.name("message")
.mediaType(MediaType.TEXT_PLAIN_TYPE)
.data(String.class, message)
.build();
broadcaster.broadcast(event);
System.out.println("broadcasting listen [" +message+ "]");
return "Message was '" + message + "' broadcast.";
}
@GET
@Produces(SseFeature.SERVER_SENT_EVENTS)
@Path("/listen")
public EventOutput listenToBroadcast() {
System.out.println("will listen");
final EventOutput eventOutput = new EventOutput();
this.broadcaster.add(eventOutput);
return eventOutput;
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false">
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>net.jigarshah.mse.tracker</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
客户端JS
<script>
var url="http://localhost:8080/trackapp/webapi/broadcast/listen";
var source=new EventSource(url);
source.onerror=function(event)
{
console.log("error");
};
source.onmessage=function(event)
{
console.log(event.data);
document.getElementById("result").innerHTML+=event.data + "<br>";
};
</script>
</head>
<body>
<div id="result">
test
</div>
答案 0 :(得分:2)
一种可能性是检查此处列出的maven依赖关系 https://jersey.java.net/documentation/latest/modules-and-dependencies.html
您还可以通过jersey邮件列表https://java.net/projects/jersey/lists/users/archive/2013-11/message/63
中的此邮件进行检查希望这有帮助。
答案 1 :(得分:0)
我遇到了同样的麻烦,我的问题是我使用了错误的jersey servlet实现。添加以替换
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
by(注意缺少的-core
)
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>