All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.yawenok.apns.http2.impl.NettyApnsHttp2ConnectionPool Maven / Gradle / Ivy

package io.github.yawenok.apns.http2.impl;

import io.github.yawenok.apns.http2.ApnsConfig;
import io.github.yawenok.apns.http2.ApnsConstant;
import io.github.yawenok.apns.http2.ApnsHttp2Connection;
import io.github.yawenok.apns.http2.ApnsHttp2ConnectionPool;
import io.github.yawenok.apns.http2.exceptions.AuthenticationException;
import io.github.yawenok.apns.http2.exceptions.ConnectionException;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPool;

@SuppressWarnings("unchecked")
public class NettyApnsHttp2ConnectionPool extends BasePooledObjectFactory implements ApnsHttp2ConnectionPool {
    private final ApnsConfig apnsConfig;

    private final GenericObjectPool internalPool;

    public NettyApnsHttp2ConnectionPool(final ApnsConfig apnsConfig) {
        this.apnsConfig = apnsConfig;
        this.internalPool = new GenericObjectPool(this, apnsConfig);
    }

    @Override
    public void init() {
    }

    @Override
    public void destroy() {
        internalPool.close();
    }

    @Override
    public ApnsHttp2Connection acquireConnection() throws Exception {
        return internalPool.borrowObject();
    }

    @Override
    public void returnConnection(final ApnsHttp2Connection connection) {
        internalPool.returnObject(connection);
    }

    @Override
    public int getNumActive() {
        return internalPool.getNumActive();
    }

    @Override
    public int getNumIdle() {
        return internalPool.getNumIdle();
    }

    @Override
    public ApnsHttp2Connection create() throws ConnectionException, AuthenticationException {
        String host = apnsConfig.isSandbox() ? ApnsConstant.APNS_HOST_DEVELOPMENT : ApnsConstant.APNS_HOST_PRODUCTION;
        int port = ApnsConstant.APNS_DEFAULT_PORT;

        ApnsHttp2Connection connection = new NettyApnsHttp2Connection();
        connection.init(host, port, apnsConfig.getAuthConfig(), apnsConfig.getProxyConfig());
        return connection;
    }

    @Override
    public PooledObject wrap(ApnsHttp2Connection connection) {
        return new DefaultPooledObject(connection);
    }

    @Override
    public void destroyObject(PooledObject pooledObject) throws Exception {
        ApnsHttp2Connection connection = pooledObject.getObject();
        connection.destroy();
    }

    @Override
    public boolean validateObject(PooledObject pooledObject) {
        ApnsHttp2Connection connection = pooledObject.getObject();
        return connection.isActive();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy