java 实现http长轮询,Long Polling长轮询实现进阶

Long Polling长轮询实现进阶

简书 涤生。

转载请注明原创出处,谢谢!

如果读完觉得有收获的话,欢迎点赞加关注。

介绍

由于Long Polling长轮询详解 这篇文章中的code实现较为简单,尤其是服务端处理较为粗暴,有一些同学反馈希望服务端处理阻塞这块内容进行更深入讨论等等,所以这里专门补一篇实现进阶,让大家对长轮询有更加深刻的理解。

疑问

对上篇文章,同学反馈有两个疑问。

服务端实现使用的是同步servlet,性能比较差,能支撑的连接数比较少?

同步servlet来hold请求,确实会导致后续请求得不到及时处理,servlet3.0开始支持异步处理,可以更高效的处理请求。

服务端如何去hold住请求,sleep好吗?

同步servlet hold住请求的处理逻辑必须在servlet的doGet方法中,一般先fetch数据,准备好了,就返回,没准备好,就sleep片刻,再来重复。

异步servlet hold住请求比较简单,只要开启异步,执行完doGet方法后,不会自动返回此次请求,需要等到请求的context被complete,这样很巧妙的请求就自动hold住了。

实现

客户端实现

客户端实现基本和上篇差不多,没什么改变。

package com.andy.example.longpolling.client;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.concurrent.atomic.AtomicLong;

/**

* Created by andy on 17/7/8.

*/

public class AbstractBootstrap {

//同步URL

protected static final String URL = "http://localhost:8080/long-polling";

//异步URL

protected static final String ASYNC_URL = "http://localhost:8080/long-polling-async";

private final AtomicLong sequence = new AtomicLong();

protected void poll() {

//循环执行,保证每次longpolling结束,再次发起longpolling

while (!Thread.interrupted()) {

doPoll();

}

}

protected void doPoll() {

System.out.println("第" + (sequence.incrementAndGet()) + "次 longpolling");

long startMillis = System.currentTimeMillis();

HttpURLConnection connection = null;

try {

URL getUrl = new URL(URL);

connection = (HttpURLConnection) getUrl.openConnection();

//50s作为长轮询超时时间

connection.setReadTimeout(50000);

connection.setConnectTimeout(3000);

connection.setRequestMethod(

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,轮询可以通过异步Servlet、WebSocket或者基于线程的实现实现。 具体实现步骤如下: 1. 在Servlet中,需要使用异步Servlet来实现轮询,即在Servlet中使用 `request.startAsync()` 方法来启动异步处理。 2. 在异步处理中,需要使用 `AsyncContext` 对象来设置超时时间、处理完成后的回调函数等。在异步处理中,可以使用 `response.getWriter()` 方法来获取输出流,并向客户端输出数据。 3. 在客户端中,需要使用 JavaScript 来发起轮询请求。可以使用 `XMLHttpRequest` 或者 `fetch` 方法来发起请求,并在请求返回时处理响应数据。 以下是一个简单的基于 Servlet 的轮询实现示例: ```java @WebServlet("/long-polling") public class LongPollingServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AsyncContext asyncContext = request.startAsync(); asyncContext.setTimeout(30000); // 设置超时时间为30秒 asyncContext.addListener(new AsyncListener() { @Override public void onComplete(AsyncEvent event) throws IOException { System.out.println("Long polling completed"); } @Override public void onTimeout(AsyncEvent event) throws IOException { System.out.println("Long polling timeout"); event.getAsyncContext().getResponse().getWriter().write("timeout"); // 返回超时信息 event.getAsyncContext().complete(); // 结束异步处理 } @Override public void onError(AsyncEvent event) throws IOException { System.out.println("Long polling error"); event.getAsyncContext().getResponse().getWriter().write("error"); // 返回错误信息 event.getAsyncContext().complete(); // 结束异步处理 } @Override public void onStartAsync(AsyncEvent event) throws IOException { System.out.println("Long polling started"); } }); // 在异步处理中等待事件的发生 new Thread(() -> { try { // 模拟事件的发生 Thread.sleep(10000); response.getWriter().write("event happened"); // 返回事件信息 asyncContext.complete(); // 结束异步处理 } catch (Exception e) { e.printStackTrace(); } }).start(); } } ``` 客户端可以使用以下 JavaScript 代码来发起轮询请求: ```javascript function longPolling() { fetch('/long-polling') .then(response => response.text()) .then(data => { if (data === 'timeout') { console.log('Long polling timeout'); longPolling(); } else if (data === 'error') { console.log('Long polling error'); longPolling(); } else { console.log('Event happened:', data); longPolling(); } }) .catch(error => { console.error('Long polling error:', error); longPolling(); }); } longPolling(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值