我正在 JBoss AS 7.1.2 上使用 RestEasy 实现 jax-rs 服务,我想使用异步 HTTP 处理,如下所述:http: //docs.jboss.org/resteasy/docs/1.0.0.GA/userguide /html/Asynchronous_HTTP_Request_Processing.html
对于 thr AsynchronousResponse,我定义了 10 秒的超时。当此期限到期时,请求以 200 OK 和空正文响应。我想修改此行为,因此我需要收到有关超时事件的通知。
在我的解决方案中,我想在 NotificationManager 对象中处理超时事件,该对象暂时保留 AsycnhronousResponse。详情请看下面的代码。
到目前为止,我无法弄清楚如何做到这一点。有人对 RestEasy 异步 HTTP 处理有更多经验吗?
@POST
@Path("/blabla")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void subscribeLongPolling (
final @Suspend(10000) AsynchronousResponse response,
JAXBElement<LongPollingRequestParameters> rqParam,
@Context HttpServletRequest req) throws Exception {
//do some stuff with req
Thread t = new Thread("ThreadSubscribeTo:" + channelID)
{
@Override
public void run() {
//hand over to Notification Manager to return notifications in case some exist
try {
NotificationManager nm = new NotificationManager();
nm.setAsyncResponseObject(response);
logger.info("Response object registered in NotificationManager");
}catch (Exception e){
e.printStackTrace();
}
}
};
t.start();
logger.info("Releasing Thread");
}
public class NotificationManager {
private AsynchronousResponse response;
private NotificationList nList;
public synchronized void setAsyncResponseObject(AsynchronousResponse response) {
this.response = response;
if (nList.getAny().size() > 0) {
logger.info("Stored notification send to web client: " + nList.getAny().get(0).toString());
sendNotification(nList.getAny().remove(0));
}
}
public synchronized void sendNotification(Object message){
if (response != null){
logger.info("Response object found. Send notification immediately: " + message.toString());
Response responseObject = Response.ok(message, MediaType.APPLICATION_XML).build();
response.setResponse(responseObject);
response = null;
}else{
logger.info("Response object not found notification will be stored");
addNotification(message);
}
}
}
提前致谢,
亚历克斯