public class ApnsConnection<T extends ApnsPushNotification> extends Object
A connection to an APNs gateway. An ApnsConnection
is responsible for sending push notifications to the
APNs gateway, and reports lifecycle events via its ApnsConnectionListener
.
Generally, connections should be managed by a parent PushManager
and not manipulated directly (although
connections are fully functional on their own). Connections are created in a disconnected state, and must be
explicitly connected before they can be used to send push notifications.
PushManager
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_SENT_NOTIFICATION_BUFFER_CAPACITY |
Constructor and Description |
---|
ApnsConnection(ApnsEnvironment environment,
SSLContext sslContext,
NioEventLoopGroup eventLoopGroup,
ApnsConnectionConfiguration configuration,
ApnsConnectionListener<T> listener,
String name)
Constructs a new APNs connection.
|
Modifier and Type | Method and Description |
---|---|
void |
connect()
Asynchronously connects to the APNs gateway in this connection's environment.
|
void |
sendNotification(T notification)
Asynchronously sends a push notification to the connected APNs gateway.
|
void |
shutdownGracefully()
Gracefully and asynchronously shuts down this connection.
|
void |
shutdownImmediately()
Immediately closes this connection (assuming it was ever open).
|
String |
toString() |
void |
waitForPendingWritesToFinish()
Waits for all pending write operations to finish.
|
public static final int DEFAULT_SENT_NOTIFICATION_BUFFER_CAPACITY
public ApnsConnection(ApnsEnvironment environment, SSLContext sslContext, NioEventLoopGroup eventLoopGroup, ApnsConnectionConfiguration configuration, ApnsConnectionListener<T> listener, String name)
environment
- the environment in which this connection will operate; must not be null
sslContext
- an SSL context with the keys/certificates and trust managers this connection should use when
communicating with the APNs gateway; must not be null
eventLoopGroup
- the event loop group this connection should use for asynchronous network operations; must
not be null
configuration
- the set of configuration options to use for this connection. The configuration object is
copied and changes to the original object will not propagate to the connection after creation. Must not be
null
.listener
- the listener to which this connection will report lifecycle events; may be null
name
- a human-readable name for this connection; names must not be null
public void connect()
public void sendNotification(T notification)
notification
- the notification to sendApnsConnectionListener.handleWriteFailure(ApnsConnection, ApnsPushNotification, Throwable)
,
ApnsConnectionListener.handleRejectedNotification(ApnsConnection, ApnsPushNotification, RejectedNotificationReason)
public void waitForPendingWritesToFinish() throws InterruptedException
Waits for all pending write operations to finish. When this method exits normally (i.e. when it does
not throw an InterruptedException
), All pending writes will have either finished successfully or failed
and passed to this connection's listener via the
ApnsConnectionListener.handleWriteFailure(ApnsConnection, ApnsPushNotification, Throwable)
method.
It is not guaranteed that all write operations will have finished by the time a connection has
closed. Applications that need to know when all writes have finished should call this method after a connection
closes, but must not do so in an IO thread (i.e. the thread that called the
ApnsConnectionListener.handleConnectionClosure(ApnsConnection)
method.
InterruptedException
- if interrupted while waiting for pending read/write operations to finishpublic void shutdownGracefully()
Gracefully and asynchronously shuts down this connection. Graceful disconnection is triggered by sending a known-bad notification to the APNs gateway; when the gateway rejects the notification, it is guaranteed that preceding notifications were processed successfully and that all following notifications were not processed at all. The gateway will close the connection after rejecting the notification, and this connection's listener will be notified when the connection is closed.
Note that if/when the known-bad notification is rejected by the APNs gateway, this connection's listener will not be notified of the rejection.
Calling this method before establishing a connection with the APNs gateway or while a graceful shutdown attempt is already in progress has no effect.
public void shutdownImmediately()
Immediately closes this connection (assuming it was ever open). No guarantees are made with regard to the
state of sent notifications, and callers should generally prefer shutdownGracefully()
to
this method. If the connection was previously open, the connection's listener will be notified of the
connection's closure. If a connection attempt was in progress, the listener will be notified of a connection
failure. If the connection was never open, this method has no effect.
Calling this method while not connected has no effect.
Copyright © 2013–2015 RelayRides. All rights reserved.