io.github.yawenok.apns.http2.impl.NettyApnsHttp2Client Maven / Gradle / Ivy
package io.github.yawenok.apns.http2.impl;
import io.github.yawenok.apns.http2.ApnsConfig;
import io.github.yawenok.apns.http2.ApnsHttp2Client;
import io.github.yawenok.apns.http2.ApnsHttp2Connection;
import io.github.yawenok.apns.http2.ApnsHttp2ConnectionPool;
import io.github.yawenok.apns.http2.concurrent.FutureCallback;
import io.github.yawenok.apns.http2.exceptions.AuthenticationException;
import io.github.yawenok.apns.http2.exceptions.ConnectionException;
import io.github.yawenok.apns.http2.Notification;
import io.github.yawenok.apns.http2.NotificationResponse;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class NettyApnsHttp2Client implements ApnsHttp2Client {
private ApnsConfig apnsConfig;
private ApnsHttp2ConnectionPool apnsHttp2ConnectionPool;
public NettyApnsHttp2Client() {
}
@Override
public void init(final ApnsConfig config) throws AuthenticationException, ConnectionException {
if (config == null) {
throw new IllegalArgumentException("ApnsConfig is null!");
}
apnsConfig = config;
apnsHttp2ConnectionPool = new NettyApnsHttp2ConnectionPool(apnsConfig);
apnsHttp2ConnectionPool.init();
}
@Override
public void destroy() {
if (apnsHttp2ConnectionPool != null) {
apnsHttp2ConnectionPool.destroy();
apnsHttp2ConnectionPool = null;
}
}
@Override
public NotificationResponse sendNotificationSync(final Notification notification) throws InterruptedException, ExecutionException, ConnectionException, AuthenticationException {
Future responseFuture = this.sendNotificationAsync(notification);
return responseFuture.get();
}
@Override
public Future sendNotificationAsync(final Notification notification) throws ConnectionException, AuthenticationException {
ApnsHttp2Connection connection = null;
try {
connection = apnsHttp2ConnectionPool.acquireConnection();
return connection.sendNotification(notification);
} catch (AuthenticationException e) {
throw e;
} catch (Exception e) {
throw new ConnectionException("Acquire connection error!", e);
} finally {
if (connection != null) {
apnsHttp2ConnectionPool.returnConnection(connection);
}
}
}
@Override
public void sendNotificationAsync(final Notification notification, final FutureCallback callback) throws ConnectionException, AuthenticationException {
ApnsHttp2Connection connection = null;
try {
connection = apnsHttp2ConnectionPool.acquireConnection();
connection.sendNotification(notification, callback);
} catch (AuthenticationException e) {
throw e;
} catch (Exception e) {
throw new ConnectionException("Acquire connection error!", e);
} finally {
if (connection != null) {
apnsHttp2ConnectionPool.returnConnection(connection);
}
}
}
}