发送 5000 个推送通知后 GCM 超时和连接被拒绝

GCM Timeout & Connection Refused after 5000 push notifications sent

我需要通过 gcm 服务每秒发送 500 个推送通知。不幸的是,与 chrome49 不同,我必须为每个 chrome50 客户加密消息,请求中指示了加密密钥 header。发送超过 5000 条消息后,我收到来自 https://android.googleapis.com/gcm/send 的超时和拒绝连接,它停止工作一段时间,然后定期正常工作

Post 请求在线程中运行

        try {
        String path = "https://android.googleapis.com/gcm/send";
        URI uri = new URIBuilder()
                .setPath(path)
                .build();
        HttpPost httpPost = new HttpPost(uri);
        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("Content-Encoding", "aesgcm");
        httpPost.addHeader("Cache-Control", "no-cache");
        httpPost.addHeader("Connection", "Keep-Alive");
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.addHeader("Authorization", "key=" + appKey);
        httpPost.addHeader("Encryption", encryptionHeader);
        httpPost.addHeader("Crypto-Key", cryptoKeyHeader);
        httpPost.setEntity(new StringEntity(jsonFromData));
        HttpResponse response = client.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity).replace("%", "@");
        LOGGER.info(responseString);
    } catch (Exception e) {
        e.printStackTrace();
    }

异常:连接被拒绝

org.apache.http.conn.HttpHostConnectException: Connection to https://android.googleapis.com refused at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:190) at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294) at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:643) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784) at com.mpsdevelopment.push.threads.SenderThread.sent(SenderThread.java:100) at com.mpsdevelopment.push.threads.SenderThread.run(SenderThread.java:76) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

连接超时

Caused by: java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668) at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:414) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180) ... 13 more

当您遇到 ERR_CONNECTION_REFUSED 时,请检查您的互联网连接,检查所有电缆并重新启动您可能正在使用的所有路由器、调制解调器或其他网络设备。该错误主要是由于互联网连接问题以及您用于连接互联网的设备存在问题而导致的。按照steps in this link解决这个问题。

基于此 thread,企业网络中可能会出现许多问题,因为防火墙可能会阻止对相当不寻常的端口的访问,或者代理可能会干扰任何传出流量。该异常表明您存在网络问题 - 它只是超时。我想 Google 和您的提供商都不是这里的问题。检查您的设置以及您与外部互联网之间的所有防火墙(公司或个人)。