Search in sources :

Example 1 with StartSendingApnsDelegate

use of com.notnoop.apns.StartSendingApnsDelegate in project java-apns by notnoop.

the class ApnsConnectionImpl method sendMessage.

private synchronized void sendMessage(ApnsNotification m, boolean fromBuffer) throws NetworkIOException {
    logger.debug("sendMessage {} fromBuffer: {}", m, fromBuffer);
    if (delegate instanceof StartSendingApnsDelegate) {
        ((StartSendingApnsDelegate) delegate).startSending(m, fromBuffer);
    }
    int attempts = 0;
    while (true) {
        try {
            attempts++;
            Socket socket = getOrCreateSocket(fromBuffer);
            socket.getOutputStream().write(m.marshall());
            socket.getOutputStream().flush();
            cacheNotification(m);
            delegate.messageSent(m, fromBuffer);
            //logger.debug("Message \"{}\" sent", m);
            attempts = 0;
            break;
        } catch (SSLHandshakeException e) {
            // No use retrying this, it's dead Jim
            throw new NetworkIOException(e);
        } catch (IOException e) {
            Utilities.close(socket);
            if (attempts >= RETRIES) {
                logger.error("Couldn't send message after " + RETRIES + " retries." + m, e);
                delegate.messageSendFailed(m, e);
                Utilities.wrapAndThrowAsRuntimeException(e);
            }
            if (attempts != 1) {
                logger.info("Failed to send message " + m + "... trying again after delay", e);
                Utilities.sleep(DELAY_IN_MS);
            }
        }
    }
}
Also used : StartSendingApnsDelegate(com.notnoop.apns.StartSendingApnsDelegate) NetworkIOException(com.notnoop.exceptions.NetworkIOException) IOException(java.io.IOException) Socket(java.net.Socket) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) NetworkIOException(com.notnoop.exceptions.NetworkIOException)

Example 2 with StartSendingApnsDelegate

use of com.notnoop.apns.StartSendingApnsDelegate in project java-apns by notnoop.

the class ApnsConnectionCacheTest method handleReTransmissionError1Good1Bad2Good.

/**
     * Test2 to make sure that after rejected notification
     * in-flight notifications are re-transmitted
     *
     * @throws InterruptedException
     */
@Test(timeout = 5000)
public void handleReTransmissionError1Good1Bad2Good() throws InterruptedException {
    server = new ApnsServerStub(FixedCertificates.serverContext().getServerSocketFactory());
    final CountDownLatch sync = new CountDownLatch(6);
    final AtomicInteger numResent = new AtomicInteger();
    final AtomicInteger numSent = new AtomicInteger();
    final AtomicInteger numStartSend = new AtomicInteger();
    int EXPECTED_RESEND_COUNT = 2;
    int EXPECTED_SEND_COUNT = 3;
    server.getWaitForError().acquire();
    server.start();
    ApnsService service = APNS.newService().withSSLContext(clientContext()).withGatewayDestination(LOCALHOST, server.getEffectiveGatewayPort()).withDelegate(new StartSendingApnsDelegate() {

        public void startSending(final ApnsNotification message, final boolean resent) {
            if (!resent) {
                numStartSend.incrementAndGet();
            }
        }

        public void messageSent(ApnsNotification message, boolean resent) {
            if (!resent) {
                numSent.incrementAndGet();
            }
            sync.countDown();
        }

        public void messageSendFailed(ApnsNotification message, Throwable e) {
            numSent.decrementAndGet();
        }

        public void connectionClosed(DeliveryError e, int messageIdentifier) {
        }

        public void cacheLengthExceeded(int newCacheLength) {
        }

        public void notificationsResent(int resendCount) {
            numResent.set(resendCount);
        }
    }).build();
    server.stopAt(msg1.length() * 3 + eMsg2.length() * 2);
    service.push(msg1);
    service.push(eMsg2);
    service.push(eMsg1);
    service.push(msg2);
    server.sendError(8, eMsg2.getIdentifier());
    server.getWaitForError().release();
    server.getMessages().acquire();
    sync.await();
    Assert.assertEquals(EXPECTED_RESEND_COUNT, numResent.get());
    Assert.assertEquals(EXPECTED_SEND_COUNT, numSent.get());
    Assert.assertEquals(EXPECTED_SEND_COUNT + 1, numStartSend.get());
}
Also used : StartSendingApnsDelegate(com.notnoop.apns.StartSendingApnsDelegate) ApnsServerStub(com.notnoop.apns.utils.ApnsServerStub) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeliveryError(com.notnoop.apns.DeliveryError) ApnsNotification(com.notnoop.apns.ApnsNotification) SimpleApnsNotification(com.notnoop.apns.SimpleApnsNotification) EnhancedApnsNotification(com.notnoop.apns.EnhancedApnsNotification) CountDownLatch(java.util.concurrent.CountDownLatch) ApnsService(com.notnoop.apns.ApnsService)

Example 3 with StartSendingApnsDelegate

use of com.notnoop.apns.StartSendingApnsDelegate in project java-apns by notnoop.

the class ApnsConnectionCacheTest method handleReTransmissionError1Bad.

/**
     * Test to make sure single rejected notifications are returned
     *
     * @throws InterruptedException
     */
@Test(timeout = 5000)
public void handleReTransmissionError1Bad() throws InterruptedException {
    server = new ApnsServerStub(FixedCertificates.serverContext().getServerSocketFactory());
    final CountDownLatch sync = new CountDownLatch(1);
    final AtomicInteger numError = new AtomicInteger();
    final AtomicInteger numStartSend = new AtomicInteger();
    int EXPECTED_ERROR_COUNT = 1;
    server.getWaitForError().acquire();
    server.start();
    ApnsService service = APNS.newService().withSSLContext(clientContext()).withGatewayDestination(LOCALHOST, server.getEffectiveGatewayPort()).withDelegate(new StartSendingApnsDelegate() {

        public void startSending(final ApnsNotification message, final boolean resent) {
            if (!resent) {
                numStartSend.incrementAndGet();
            }
        }

        public void messageSent(ApnsNotification message, boolean resent) {
        }

        public void messageSendFailed(ApnsNotification message, Throwable e) {
            numError.incrementAndGet();
            sync.countDown();
        }

        public void connectionClosed(DeliveryError e, int messageIdentifier) {
        }

        public void cacheLengthExceeded(int newCacheLength) {
        }

        public void notificationsResent(int resendCount) {
        }
    }).build();
    server.stopAt(eMsg1.length());
    service.push(eMsg1);
    server.sendError(8, eMsg1.getIdentifier());
    server.getWaitForError().release();
    server.getMessages().acquire();
    sync.await();
    Assert.assertEquals(EXPECTED_ERROR_COUNT, numError.get());
    Assert.assertEquals(EXPECTED_ERROR_COUNT, numStartSend.get());
}
Also used : StartSendingApnsDelegate(com.notnoop.apns.StartSendingApnsDelegate) ApnsServerStub(com.notnoop.apns.utils.ApnsServerStub) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeliveryError(com.notnoop.apns.DeliveryError) ApnsNotification(com.notnoop.apns.ApnsNotification) SimpleApnsNotification(com.notnoop.apns.SimpleApnsNotification) EnhancedApnsNotification(com.notnoop.apns.EnhancedApnsNotification) CountDownLatch(java.util.concurrent.CountDownLatch) ApnsService(com.notnoop.apns.ApnsService)

Example 4 with StartSendingApnsDelegate

use of com.notnoop.apns.StartSendingApnsDelegate in project java-apns by notnoop.

the class ApnsConnectionCacheTest method handleReTransmissionError5Good1Bad7Good.

/**
     * Test1 to make sure that after rejected notification
     * in-flight notifications are re-transmitted
     *
     * @throws InterruptedException
     */
@Test(timeout = 5000)
public void handleReTransmissionError5Good1Bad7Good() throws InterruptedException {
    server = new ApnsServerStub(FixedCertificates.serverContext().getServerSocketFactory());
    //5 success 1 fail 7 success 7 resent
    final CountDownLatch sync = new CountDownLatch(20);
    final AtomicInteger numResent = new AtomicInteger();
    final AtomicInteger numSent = new AtomicInteger();
    final AtomicInteger numStartSend = new AtomicInteger();
    int EXPECTED_RESEND_COUNT = 7;
    int EXPECTED_SEND_COUNT = 12;
    server.getWaitForError().acquire();
    server.start();
    ApnsService service = APNS.newService().withSSLContext(clientContext()).withGatewayDestination(LOCALHOST, server.getEffectiveGatewayPort()).withDelegate(new StartSendingApnsDelegate() {

        public void startSending(final ApnsNotification message, final boolean resent) {
            if (!resent) {
                numStartSend.incrementAndGet();
            }
        }

        public void messageSent(ApnsNotification message, boolean resent) {
            if (!resent) {
                numSent.incrementAndGet();
            }
            sync.countDown();
        }

        public void messageSendFailed(ApnsNotification message, Throwable e) {
            numSent.decrementAndGet();
        }

        public void connectionClosed(DeliveryError e, int messageIdentifier) {
        }

        public void cacheLengthExceeded(int newCacheLength) {
        }

        public void notificationsResent(int resendCount) {
            numResent.set(resendCount);
        }
    }).build();
    server.stopAt(eMsg1.length() * 5 + eMsg2.length() + eMsg3.length() * 14);
    for (int i = 0; i < 5; ++i) {
        service.push(eMsg1);
    }
    service.push(eMsg2);
    for (int i = 0; i < 7; ++i) {
        service.push(eMsg3);
    }
    server.sendError(8, eMsg2.getIdentifier());
    server.getWaitForError().release();
    server.getMessages().acquire();
    sync.await();
    Assert.assertEquals(EXPECTED_RESEND_COUNT, numResent.get());
    Assert.assertEquals(EXPECTED_SEND_COUNT, numSent.get());
    Assert.assertEquals(EXPECTED_SEND_COUNT + 1, numStartSend.get());
}
Also used : StartSendingApnsDelegate(com.notnoop.apns.StartSendingApnsDelegate) ApnsServerStub(com.notnoop.apns.utils.ApnsServerStub) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeliveryError(com.notnoop.apns.DeliveryError) ApnsNotification(com.notnoop.apns.ApnsNotification) SimpleApnsNotification(com.notnoop.apns.SimpleApnsNotification) EnhancedApnsNotification(com.notnoop.apns.EnhancedApnsNotification) CountDownLatch(java.util.concurrent.CountDownLatch) ApnsService(com.notnoop.apns.ApnsService)

Aggregations

StartSendingApnsDelegate (com.notnoop.apns.StartSendingApnsDelegate)4 ApnsNotification (com.notnoop.apns.ApnsNotification)3 ApnsService (com.notnoop.apns.ApnsService)3 DeliveryError (com.notnoop.apns.DeliveryError)3 EnhancedApnsNotification (com.notnoop.apns.EnhancedApnsNotification)3 SimpleApnsNotification (com.notnoop.apns.SimpleApnsNotification)3 ApnsServerStub (com.notnoop.apns.utils.ApnsServerStub)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 NetworkIOException (com.notnoop.exceptions.NetworkIOException)1 IOException (java.io.IOException)1 Socket (java.net.Socket)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1