Http 如何在RestEasy中处理AsynchronousResponse对象超时
我正在JBoss AS 7.1.2上使用RestEasy实现jax rs服务,我希望使用异步HTTP处理,如下所述: 对于thr AsynchronousResponse,我定义了10秒的超时。当这段时间到期时,请求得到200 OK和空正文的响应。我想修改此行为,因此需要通知我超时事件 在我的解决方案中,我希望在NotificationManager对象中处理超时事件,该对象暂时保持AsycnhronousResponse。有关详细信息,请参见下面的代码 到目前为止,我还不知道该怎么做。有人对RestEasy异步HTTP处理有更多的经验吗Http 如何在RestEasy中处理AsynchronousResponse对象超时,http,asynchronous,timeout,jboss7.x,resteasy,Http,Asynchronous,Timeout,Jboss7.x,Resteasy,我正在JBoss AS 7.1.2上使用RestEasy实现jax rs服务,我希望使用异步HTTP处理,如下所述: 对于thr AsynchronousResponse,我定义了10秒的超时。当这段时间到期时,请求得到200 OK和空正文的响应。我想修改此行为,因此需要通知我超时事件 在我的解决方案中,我希望在NotificationManager对象中处理超时事件,该对象暂时保持AsycnhronousResponse。有关详细信息,请参见下面的代码 到目前为止,我还不知道该怎么做。有人对R
@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);
}
}
}
@POST
@路径(“/blabla”)
@产生({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
@使用({MediaType.APPLICATION\uxml,MediaType.APPLICATION\ujson})
公共无效订阅轮询(
最终@Suspend(10000)异步响应,
JAXBElement rqParam,
@上下文HttpServletRequest(请求请求请求)引发异常{
//用req做一些事情
线程t=新线程(“ThreadSubscribeTo:+channelID”)
{
@凌驾
公开募捐{
//移交给通知管理器,以便在存在通知的情况下返回通知
试一试{
NotificationManager nm=新建NotificationManager();
nm.setAsyncResponseObject(响应);
logger.info(“在NotificationManager中注册的响应对象”);
}捕获(例外e){
e、 printStackTrace();
}
}
};
t、 start();
logger.info(“释放线程”);
}
公共类通知管理器{
私有异步响应;
私人通知名单;
公共同步void setAsynchronousResponseObject(异步响应){
这个。反应=反应;
如果(nList.getAny().size()>0){
info(“存储通知发送到web客户端:+nList.getAny().get(0.toString())”;
sendNotification(nList.getAny().remove(0));
}
}
公共同步的void sendNotification(对象消息){
if(响应!=null){
info(“找到响应对象。立即发送通知:”+message.toString());
Response responseObject=Response.ok(消息,MediaType.APPLICATION_XML).build();
response.setResponse(responseObject);
响应=空;
}否则{
info(“未找到响应对象通知将被存储”);
添加通知(消息);
}
}
}
提前感谢,
亚历克斯