我想了解更多关于异步通知。我有一个URL形式:注册异步通知的URL?
http://www.sample.com/AsyncNotify?sessionId=xxxxxx
现在,如果我用sessionId调用此URL,它等同于注册异步通知。我使用Apache HTTP Commons库来执行Http Post和Get。如果是这种情况,那么我怎样才能从服务器端接收事件?我是否必须忘记这种方法并使用套接字呢?目前,这是我的方法:
HttpClient httpClient = new HttpClient;
String url = "http://www.sample.com/AsyncNotify?sessionId=xxxxxx"
GetMethod get = new GetMethod(url);
try {
httpClient.executeMethod(get);
//read the response
} catch(Exception e) {
}
我想是要建立一个while循环中套接字层连接,并调用每当收到一些数据的处理,但有没有更好的方式来实现这一目标?
编辑:
我用了xSocket去下面的阶段,但30秒后连接关闭:
try {
String _GETRequest = "/sample/notify";
HttpClientConnection con = new HttpClientConnection("10.0.0.23", 5050);
con.setConnectionTimeoutMillis(100000);
GetRequest request = new GetRequest(_GETRequest);
request.setParameter("id", id);
IHttpResponseHandler responseHandler = new AsyncHandler();
con.send(request, responseHandler);
org.xlightweb.client.HttpClient httpClient = new org.xlightweb.client.HttpClient();
request.setParameter("id", id);
con.send(request, responseHandler);
// Don't let the program terminate. In other words,
// wait for a message from the server
while(con.isOpen()) {};
if(!con.isOpen()) {
}
} catch (ConnectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@Romain:感谢您的输入。我正在修改如何编写与Comet服务器一起工作的Java客户端。至于WebSocket,我的服务器不支持它,所以这不是一个选项。如果你在一个与Comet服务器一起工作的非常简单的基于纯Java的客户端上有一些输入,我会非常喜欢。 – Legend 2010-06-26 04:12:44
@Legend - 您正在使用哪个应用服务器?你的客户写了什么?我在回答结尾添加了一些链接,可能有助于 – 2010-06-26 04:18:10
@Romain:感谢您的回复。我认为服务器使用灰熊彗星'https:// grizzly.dev.java.net /'我可以使用XMLHttpRequest使用JavaScript处理,但使用Java,出于某种原因,它不响应。我已经看过你发布的教程。我添加了教程中提到的jar,但像IHttpRequestHandler等一些类仍然不可见。 – Legend 2010-06-26 05:16:57