use of com.notnoop.exceptions.NetworkIOException in project java-apns by notnoop.
the class ApnsConnectionImpl method getOrCreateSocket.
private synchronized Socket getOrCreateSocket(boolean resend) throws NetworkIOException {
if (reconnectPolicy.shouldReconnect()) {
logger.debug("Reconnecting due to reconnectPolicy dictating it");
Utilities.close(socket);
socket = null;
}
if (socket == null || socket.isClosed()) {
try {
if (proxy == null) {
socket = factory.createSocket(host, port);
logger.debug("Connected new socket {}", socket);
} else if (proxy.type() == Proxy.Type.HTTP) {
TlsTunnelBuilder tunnelBuilder = new TlsTunnelBuilder();
socket = tunnelBuilder.build((SSLSocketFactory) factory, proxy, proxyUsername, proxyPassword, host, port);
logger.debug("Connected new socket through http tunnel {}", socket);
} else {
boolean success = false;
Socket proxySocket = null;
try {
proxySocket = new Socket(proxy);
proxySocket.connect(new InetSocketAddress(host, port), connectTimeout);
socket = ((SSLSocketFactory) factory).createSocket(proxySocket, host, port, false);
success = true;
} finally {
if (!success) {
Utilities.close(proxySocket);
}
}
logger.debug("Connected new socket through socks tunnel {}", socket);
}
socket.setSoTimeout(readTimeout);
socket.setKeepAlive(true);
if (errorDetection) {
monitorSocket(socket);
}
reconnectPolicy.reconnected();
logger.debug("Made a new connection to APNS");
} catch (IOException e) {
logger.error("Couldn't connect to APNS server", e);
// indicate to clients whether this is a resend or initial send
throw new NetworkIOException(e, resend);
}
}
return socket;
}
use of com.notnoop.exceptions.NetworkIOException 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);
}
}
}
}
use of com.notnoop.exceptions.NetworkIOException in project java-apns by notnoop.
the class ApnsConnectionResendTest method assertNetworkIoExForRedelivery.
private void assertNetworkIoExForRedelivery(ApnsNotification notification, MessageSentFailedRecord failed) {
failed.assertRecord(notification, new NetworkIOException());
final NetworkIOException found = failed.getException();
assertTrue(found.isResend());
}
use of com.notnoop.exceptions.NetworkIOException in project java-apns by notnoop.
the class QueuedApnsService method start.
public void start() {
if (started.getAndSet(true)) {
// So it is returning immediately here
return;
}
service.start();
shouldContinue = true;
thread = threadFactory.newThread(new Runnable() {
public void run() {
while (shouldContinue) {
try {
ApnsNotification msg = queue.take();
service.push(msg);
} catch (InterruptedException e) {
// ignore
} catch (NetworkIOException e) {
// ignore: failed connect...
} catch (Exception e) {
// weird if we reached here - something wrong is happening, but we shouldn't stop the service anyway!
logger.warn("Unexpected message caught... Shouldn't be here", e);
}
}
}
});
thread.start();
}
use of com.notnoop.exceptions.NetworkIOException in project java-apns by notnoop.
the class ApnsConnectionTest method sendOneSimpleClientCertFail.
@Test(timeout = 2000)
public void sendOneSimpleClientCertFail() throws InterruptedException {
ApnsService service = APNS.newService().withSSLContext(clientMultiKeyContext("notused")).withGatewayDestination(LOCALHOST, gatewayPort).build();
server.stopAt(msg1.length());
try {
service.push(msg1);
fail();
} catch (NetworkIOException e) {
assertTrue("Expected bad_certifcate exception", e.getMessage().contains("bad_certificate"));
}
}
Aggregations