如何在 Java 服务器中可靠有效地读取来自 Apple Push Notifications Server 的所有错误响应? [英] How to reliably and efficiently read all error responses from Apple Push Notifications Server in a Java Server?

查看:17
本文介绍了如何在 Java 服务器中可靠有效地读取来自 Apple Push Notifications Server 的所有错误响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Java 中 Apple 推送通知的服务器端开发.我正在使用增强格式,以便检测失败的消息并重新发送跟随坏消息的消息.我知道 Apple 不保证推送通知的交付,但我希望能够知道 Apple 是否收到了我的消息以及其中是否包含任何错误.

I am researching server side development of Apple Push Notifications in Java. I am using the enhanced format, in order to detect failed messages and resend messages that follow bad messages. I know that Apple to not guarantee the delivery of Push Notifications, but I wish to be able to know whether Apple received my messages and whether the contained any errors.

我注意到,当我尝试向 Apple 发送无效消息(无效的设备令牌、过大的有效负载等)时,我可以在套接字关闭之前再发送几条消息.当套接字关闭时,从 Apple 读取错误代码为时已晚,所以我不知道哪条消息是坏消息(或者即使有坏消息,因为 Apple 说即使没有错误).

I noticed that when I try to send an invalid message (invalid device token, too large payload, etc...) to Apple, I can send several more messages before the socket is closed. When the socket is closed it's too late to read the error codes from Apple, so I have no idea which message was the bad one (or even if there was a bad message, since Apple say the connection may close occasionally even when there is no error).

我在 JavaPNs 的源代码中看到的处理此问题的方法是在发送每条消息(或消息组)后立即读取来自 Apple 的响应.如果频繁执行,则从套接字读取,并等待超时以防无任何读取.如果您不经常发送推送通知,这是可以接受的.

The approach I've seen in the source of JavaPNs to handle this is to read responses from Apple immediately after each message (or group of messages) are sent. The reading from the socket if performed frequently, and waits for a timeout in case there is nothing to read. This is acceptable if you send push notifications infrequently.

如果您尝试以高频率发送大量通知(比如说每秒数百个),您无法在每条消息后停下来等待 Apple 的回复(即使您只等待了 20 毫秒的回复),这会将您限制为每秒 50 条消息,并且您无法知道 Apple 需要多长时间才能编写错误响应,因此短暂的等待可能还不够).如果您在每条消息之后都没有停下来阅读可能的错误,则在向 Apple 发送消息期间,您的连接可能会被关闭,在这种情况下,您将无法获得导致连接关闭的消息的 ID.

If you try to send a large number of notifications in a high frequency (lets say hundreds per second), you can't stop after each message to wait for responses from Apple (even if you wait for a response only for 20ms, that would limit you to 50 messages per second, and you have no way of knowing how long it would take Apple to write the error response, so a short wait may not be enough). If you don't stop to read possible errors after each message, you risk your connection being closed during the sending of messages to Apple, in which case you won't be able to get the Id of the message that caused the connection to close.

我正在考虑的另一种方法是在单独的线程中执行读取和写入.这样我就有更好的机会从 Apple 获取错误消息(在连接关闭之前),而不会影响我向 Apple 发送消息的速度.这种方法在编程上更加复杂,而且由于我的服务器需要将 APN 发送到多个 iOS 应用程序,每个应用程序都需要自己的套接字和读取器/写入器线程,这将使我需要的线程数量增加两倍.

Another approach I'm considering is to perform the reads and the writes in separate threads. This way I have a much better chance of getting the error messages from Apple (before the connection is closed) without compromising the speed in which I send messages to Apple. This approach is more complicated programmatically, and since my server is expected to send APNs to multiple iOS applications, each requiring its own socket and reader/writer threads, this will increase the number of threads I need by a factor of two.

上述 APN 服务器的所有行为都是在尝试向 Apple 的沙盒服务器发送推送通知时学习的.我不确定 Apple 的生产服务器的行为是否更好,而且我不确定在生产服务器上执行我的测试是否是一个好主意.

All of the above described behavior of the APN Server was learned while attempting to send push notifications to Apple's sandbox server. I'm not sure if the behavior of Apple's production server is better, and I'm not sure it's a good idea to perform my tests on the production server.

我的问题 - 有没有办法在 Alpha 关闭连接之前可靠地读取它们的错误响应,而不会牺牲性能?是否可以在服务器关闭套接字后以某种方式从套接字读取输入?

My question - Is there a way to reliably read the error responses from Alpha before they close the connection, without sacrificing performance? Is it possible somehow to read input from the socket after it was closed by the server?

推荐答案

我遇到了和你一样的问题.我尝试了两种方法,但没有一种方法可以在关闭连接之前可靠地读取来自 Apple 的错误响应:

I have encountered the same problem as you. And I have tried two approaches, but none of them can reliably read the error responses from Apple before they close the connection :

  1. 对于每个套接字,我启动了两个线程,一个用于连续写入,另一个用于阻塞读取.大多数情况下,当写入遇到Socket Broken"异常时,读取线程可以读取错误响应,但并不完全可靠.
  2. 对于每个socket,我只启动一个线程连续写入,遇到异常时,在try缓存块中,尝试读取错误响应.在大多数情况下,套接字是关闭的,所以我只读取了一个异常.

这篇关于如何在 Java 服务器中可靠有效地读取来自 Apple Push Notifications Server 的所有错误响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆