List of usage examples for io.netty.util.concurrent Future await
Future<V> await() throws InterruptedException;
From source file:com.bzcentre.dapiPush.ApnsProxy.java
License:Open Source License
public void disconnect() { final Future<Void> closeFuture = apnsClient.close(); try {//from ww w.j a v a2 s . c o m closeFuture.await().addListener((Future<Void> connectFuture) -> { //lambda expression NginxClojureRT.log.info(TAG + "Successfully disconnected from " + apnsHost + "-" + clientID); proxyReady = false; apnsClient = null; clientID = 0; }); } catch (InterruptedException e) { e.printStackTrace(); NginxClojureRT.log.debug(TAG + "Failed to disconnect from " + apnsHost); } }
From source file:com.jim.im.message.service.ApnClientUnit.java
License:Open Source License
/** * @param msg//from w w w . ja va 2 s . c om * @param apnsClient * @throws InterruptedException */ private void waitApnClientConnect(IosMessage msg, ApnsClient<SimpleApnsPushNotification> apnsClient) throws InterruptedException { if (!apnsClient.isConnected()) { Future<Void> future = null; while (true) { future = connectFutures.get(msg.getAppId()); if (future == null) { try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } else { break; } } future.await(); } }
From source file:com.mobius.software.mqtt.performance.controller.net.ClientBootstrap.java
License:Open Source License
public void shutdown() { clearPorts();//from w ww.j a v a 2 s .c o m if (loopGroup != null) { Future<?> future = loopGroup.shutdownGracefully(); try { future.await(); } catch (InterruptedException e) { logger.error("An error occured while performing shutdown: interrupted loopGroup shutdown"); } } }
From source file:com.mpcc.springmvc.configuration.Start.java
void shutdownWorkers(EventLoopGroup boss, EventLoopGroup worker) { Future fb = boss.shutdownGracefully(); Future fw = worker.shutdownGracefully(); try {/*from w ww.j av a 2 s . c om*/ fb.await(); fw.await(); } catch (InterruptedException ignore) { } }
From source file:com.turo.pushy.apns.ApnsClientBenchmark.java
License:Open Source License
@TearDown public void tearDown() throws Exception { this.client.close().await(); this.server.shutdown().await(); final Future<?> clientShutdownFuture = this.clientEventLoopGroup.shutdownGracefully(); final Future<?> serverShutdownFuture = this.serverEventLoopGroup.shutdownGracefully(); clientShutdownFuture.await(); serverShutdownFuture.await();/*from www .j a va 2s .c om*/ }
From source file:com.turo.pushy.apns.ApnsClientTest.java
License:Open Source License
@Test @Parameters({ "true", "false" }) public void testSendManyNotifications(final boolean useTokenAuthentication) throws Exception { final int notificationCount = 1000; final List<SimpleApnsPushNotification> pushNotifications = new ArrayList<>(); for (int i = 0; i < notificationCount; i++) { final String token = ApnsClientTest.generateRandomDeviceToken(); final String payload = ApnsClientTest.generateRandomPayload(); pushNotifications.add(new SimpleApnsPushNotification(token, TOPIC, payload)); }/*ww w .jav a 2s. c o m*/ final List<Future<PushNotificationResponse<SimpleApnsPushNotification>>> futures = new ArrayList<>(); final MockApnsServer server = this.buildServer(new AcceptAllPushNotificationHandlerFactory()); final ApnsClient client = useTokenAuthentication ? this.buildTokenAuthenticationClient() : this.buildTlsAuthenticationClient(); try { server.start(PORT).await(); for (final SimpleApnsPushNotification pushNotification : pushNotifications) { futures.add(client.sendNotification(pushNotification)); } for (final Future<PushNotificationResponse<SimpleApnsPushNotification>> future : futures) { future.await(); assertTrue("Send future should have succeeded, but failed with: " + future.cause(), future.isSuccess()); } } finally { client.close().await(); server.shutdown().await(); } }
From source file:com.turo.pushy.apns.ExampleApp.java
License:Open Source License
public static void main(final String[] args) throws Exception { // The first thing to do is to create an APNs client. Clients need a // certificate and private key OR a signing key to authenticate with // the APNs server. final ApnsClient apnsClient = new ApnsClientBuilder().setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST) .setSigningKey(//from w w w.jav a2 s . c o m ApnsSigningKey.loadFromPkcs8File(new File("/path/to/key.p8"), "TEAMID1234", "KEYID67890")) .build(); // Optional: we can listen for metrics by setting a metrics listener. final ApnsClient apnsClientWithMetricsListener = new ApnsClientBuilder() .setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST) .setSigningKey( ApnsSigningKey.loadFromPkcs8File(new File("/path/to/key.p8"), "TEAMID1234", "KEYID67890")) // .setMetricsListener(new NoopMetricsListener()) .build(); // Optional: we can set a proxy handler factory if we must use a proxy. final ApnsClient apnsClientWithProxyHandler = new ApnsClientBuilder() .setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST) .setSigningKey( ApnsSigningKey.loadFromPkcs8File(new File("/path/to/key.p8"), "TEAMID1234", "KEYID67890")) .setProxyHandlerFactory(new Socks5ProxyHandlerFactory(new InetSocketAddress("my.proxy.com", 1080), "username", "password")) .build(); // Clients create new connections on demand. Once we've created a // client, we can start sending push notifications. final SimpleApnsPushNotification pushNotification; { final ApnsPayloadBuilder payloadBuilder = new ApnsPayloadBuilder(); payloadBuilder.setAlertBody("Example!"); final String payload = payloadBuilder.buildWithDefaultMaximumLength(); final String token = TokenUtil.sanitizeTokenString("<efc7492 bdbd8209>"); pushNotification = new SimpleApnsPushNotification(token, "com.example.myApp", payload); } // Sending notifications is an asynchronous process. We'll get a Future // immediately, but will need to wait for the Future to complete before // we'll know whether the notification was accepted or rejected by the // APNs gateway. final PushNotificationFuture<SimpleApnsPushNotification, PushNotificationResponse<SimpleApnsPushNotification>> sendNotificationFuture = apnsClient .sendNotification(pushNotification); try { final PushNotificationResponse<SimpleApnsPushNotification> pushNotificationResponse = sendNotificationFuture .get(); if (pushNotificationResponse.isAccepted()) { // Everything worked! The notification was successfully sent to // and accepted by the gateway. } else { // Something went wrong; this should be considered a permanent // failure, and we shouldn't try to send the notification again. System.out.println("Notification rejected by the APNs gateway: " + pushNotificationResponse.getRejectionReason()); if (pushNotificationResponse.getTokenInvalidationTimestamp() != null) { // If we have an invalidation timestamp, we should also stop // trying to send notifications to the destination token (unless // it's been renewed somehow since the expiration timestamp). } } } catch (final ExecutionException e) { // Something went wrong when trying to send the notification to the // APNs gateway. The notification never actually reached the gateway, // so we shouldn't consider this a permanent failure. System.err.println("Failed to send push notification."); e.printStackTrace(); } // To send push notifications efficiently, it's best to attach listeners // to "send push notification" futures so we don't have to wait for the // server to reply before we start sending the next notification. sendNotificationFuture.addListener(new PushNotificationResponseListener<SimpleApnsPushNotification>() { @Override public void operationComplete( final PushNotificationFuture<SimpleApnsPushNotification, PushNotificationResponse<SimpleApnsPushNotification>> future) throws Exception { // When using a listener, callers should check for a failure to send a // notification by checking whether the future itself was successful // since an exception will not be thrown. if (future.isSuccess()) { final PushNotificationResponse<SimpleApnsPushNotification> pushNotificationResponse = sendNotificationFuture .getNow(); // Handle the push notification response as before from here. } else { // Something went wrong when trying to send the notification to the // APNs gateway. We can find the exception that caused the failure // by getting future.cause(). future.cause().printStackTrace(); } } }); // Finally, when we're done sending notifications (i.e. when our // application is shutting down), we should close all APNs clients // that may be in play. final Future<Void> closeFuture = apnsClient.close(); closeFuture.await(); }
From source file:com.vangav.backend.push_notifications.apple.AppleNotificationSender.java
License:Open Source License
/** * NOTE: in case values in apple_notification_properties.prop are set and * should be used, then use the singleton method i () instead * Constructor AppleNotificationSender//from w ww . j ava 2 s. co m * @param certificatePath (relative path to the certificate file.p12 * provided by Apple) * @param certificatePassword * @param clientType * @param topic: is the value of UID in the subject your APNs certificate. * It begin's with com.apple.mgmt. * @return new AppleNotificationSender Object * @throws Exception */ public AppleNotificationSender(String certificatePath, String certificatePassword, ClientType clientType, String topic) throws Exception { this.apnsClient = new ApnsClient<>(new File(certificatePath), certificatePassword); if (clientType == ClientType.DEVELOPMENT) { final Future<Void> connectFuture = this.apnsClient.connect(ApnsClient.DEVELOPMENT_APNS_HOST); connectFuture.await(); } else if (clientType == ClientType.PRODUCTION) { final Future<Void> connectFuture = this.apnsClient.connect(ApnsClient.PRODUCTION_APNS_HOST); connectFuture.await(); } else { throw new CodeException(154, 2, "invalid ClientType value [" + clientType.toString() + "], set it to one of the valid enum values", ExceptionClass.TYPE); } this.topic = topic; }
From source file:com.vangav.backend.push_notifications.apple.AppleNotificationSender.java
License:Open Source License
/** * quit/*from w w w . ja va2 s. c o m*/ * call this method after using this instance is done to disconnect the * APNS client * @throws Exception */ public void quit() throws Exception { Future<Void> disconnectFuture = apnsClient.disconnect(); disconnectFuture.await(); }
From source file:io.airlift.drift.transport.netty.server.DriftNettyServerTransport.java
License:Apache License
private static void await(Future<?> future) { try {/*w w w. j ava2s.c o m*/ future.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }