gunterzhou0502 / javapns

Automatically exported from code.google.com/p/javapns
0 stars 0 forks source link

Closing connection after getting response from apple #183

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.push notitfication to list fo devices.(include an invalid token)

What is the expected output? 
readResponses(Socket socket) method in ResponsePacketReader closes the 
connection.
Before it reach the method the socket is not closed .
After reading the response connection is closed.

What do you see instead?
socket should be reusable.

What version of the product are you using? 
2.2

On what operating system?
Windows

Please provide any additional information below.
Is it possible to resuse the connection instead of closing the connection.
It may be closing the connection because 

/* Set socket timeout to avoid getting stuck on read() */
try {
    previousTimeout = socket.getSoTimeout();
    socket.setSoTimeout(TIMEOUT);
    } catch (Exception e) {
Why do we need to set 'TIMEOUT' which is 5 seconds.

I'm trying to send 5000 notitifcation on a connection. One reason or the other 
end up in  java.net.SocketException: Socket Closed

Original issue reported on code.google.com by mohamfbh...@gmail.com on 8 May 2013 at 3:18

GoogleCodeExporter commented 9 years ago
The way the sockets are handled right now is a result of how APNS works.

Sending 5000 notifications shouldn't be a problem, and sockets opening/closing 
should be handled automatically by the library.  If you are experiencing 
exceptions, please provide the code you are using, as well as a copy of the log 
output for an attempt.

Original comment by sype...@gmail.com on 8 May 2013 at 8:58

GoogleCodeExporter commented 9 years ago

Original comment by sype...@gmail.com on 8 May 2013 at 8:59

GoogleCodeExporter commented 9 years ago
Thank you for prompt reply.
In fact we need to push arround couple of millions notifications 
we start 400 threads(that sends 5000 messages) simaltaniously. (code given 
below) . One reson or the other when start the threads , ends up in timeout 
Exception.(sometimes)
it's throwing when tring to excute 

if (socketTimeout > 0)this.socket.setSoTimeout(socketTimeout); in 
PushNotificationManager.java ( protected void sendNotification() method)

It works , If I catch the exception and reinitialize connection.

However , my biggest worry is ;

Each thread will close the connection after sending 5000 messages .
If this exceptions thrown we cannot read the response from Apple.

public class PushBulkWorker implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(PushBulkWorker.class);

    List<String> tokens;
    String message;
    AppleNotificationServer server;
    int no;

    public PushBulkWorker(List<String> tokens, String message, AppleNotificationServer server,int no) {
    super();
    this.tokens = tokens;
    this.message = message;
    this.server = server;
    this.no=no;
}

    private void send() throws Exception {
        PushNotificationPayload payload = PushNotificationPayload.alert(message);
        PushedNotifications notifications;
        PushNotificationManager pushManager;
        notifications = new PushedNotifications();
        pushManager = new PushNotificationManager();
        pushManager.initializeConnection(server);
        pushManager.setEnhancedNotificationFormatEnabled(false);
        List deviceList = Devices.asDevices(tokens);
        notifications.setMaxRetained(deviceList.size());
        for(Iterator i$ = deviceList.iterator(); i$.hasNext();)
        {
            Device device = (Device)i$.next();
            try
            {
                BasicDevice.validateTokenFormat(device.getToken());
                PushedNotification notification = pushManager.sendNotification(device, payload, true);
                notifications.add(notification);
            }
            catch(InvalidDeviceTokenFormatException e)
            {
                notifications.add(new PushedNotification(device, payload, e));
            }
        }
        try
        {
            pushManager.stopConnection();
            System.out.print(" fin : "+no);
            //System.out.println("thred ---------------     "+no+" finished");
            //logger.info("thred ---------------     "+no+" finished");
        }catch(Exception e) 
        { 
            e.printStackTrace(); 
        }finally
        {
        }
    }

    public void run() {
        try {
            send();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Original comment by mohamfbh...@gmail.com on 9 May 2013 at 8:49