使用 URL 注册异步通知?

发布于 2024-09-07 03:07:03 字数 1869 浏览 0 评论 0原文

我正在尝试了解有关异步通知的更多信息。我有一个以下形式的 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();
        }

I am trying to understand more about async notifications. I have a URL in the form of:

http://www.sample.com/AsyncNotify?sessionId=xxxxxx

Now if I call this URL with the sessionId, it is equivalent to registering for Asynchronous notifications. I am using Apache HTTP Commons library to do Http Post and Get. If that's the case, then how can I receive events from the server side? Do I have to forget this approach and use sockets instead? Currently, this is my approach:

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) {

}

What I was thinking was to establish a socket level connection inside a while loop and call a handler whenever it receives some data, but is there a better way to achieve this?

EDIT:

I've used xSocket to get to the following stage but the connection closes after 30 seconds:

         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();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗恋未遂 2024-09-14 03:07:03

Tomcat 推出了一项名为 Tomcat Comet 的技术 (http://tomcat.apache. org/tomcat-6.0-doc/aio.html )。它还被用于新的 Servlet 3.0 规范。该技术将允许您建立持久的 HTTP 连接,通过该连接您可以将通知推送到任何客户端。

还有一种称为 WebSockets 的技术,它是 HTML 5 的一部分
( http://dev.w3.org/html5/websockets/ ) 当然可以目前仅适用于有限的浏览器。也许应该等待这一点。

当然,当前实现技术向后兼容的方法(即使很糟糕)是定期轮询服务器并以这种方式获取结果。

当然,如果每个人(客户端和服务器)都在本地网络上,那么 RMI 甚至 EJB 或 JMS Pub/Sub 之类的东西可能是最好的。

这是 Comet 教程 http://www.ibm.com /developerworks/web/library/wa-cometjava/index.html 和另一个 http://www.javaworld.com/javaworld/jw-03-2008/jw-03-asynchhttp-test.html

Tomcat came out with a technology called Tomcat Comet ( http://tomcat.apache.org/tomcat-6.0-doc/aio.html ). It has also been used for the new Servlet 3.0 spec. This technology will allow you to do persistent HTTP connections through which you can push notifications to any clients.

There is also a technolgy called WebSockets that is part of HTML 5
( http://dev.w3.org/html5/websockets/ ) Of course it only works in a limied set of browsers for now. Probably should wait on this.

Of course the current way to do it to be technolgy backwards compatible (even if it sucks) is to poll the server periodically and get results that way.

Of course if everybody (clients and servers) are on a local network then probably something like RMI or even EJBs or JMS Pub/Sub would be best.

Here is a Comet tutorial http://www.ibm.com/developerworks/web/library/wa-cometjava/index.html and another one http://www.javaworld.com/javaworld/jw-03-2008/jw-03-asynchhttp-test.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文