Example usage for io.netty.util.concurrent Future getNow

List of usage examples for io.netty.util.concurrent Future getNow

Introduction

In this page you can find the example usage for io.netty.util.concurrent Future getNow.

Prototype

V getNow();

Source Link

Document

Return the result without blocking.

Usage

From source file:com.turo.pushy.apns.ApnsChannelPool.java

License:Open Source License

private void acquireWithinEventExecutor(final Promise<Channel> acquirePromise) {
    assert this.executor.inEventLoop();

    if (!this.isClosed) {
        // We always want to open new channels if we have spare capacity. Once the pool is full, we'll start looking
        // for idle, pre-existing channels.
        if (this.allChannels.size() + this.pendingCreateChannelFutures.size() < this.capacity) {
            final Future<Channel> createChannelFuture = this.channelFactory
                    .create(executor.<Channel>newPromise());
            this.pendingCreateChannelFutures.add(createChannelFuture);

            createChannelFuture.addListener(new GenericFutureListener<Future<Channel>>() {

                @Override//ww  w .  j a v  a 2 s.  c  o  m
                public void operationComplete(final Future<Channel> future) {
                    ApnsChannelPool.this.pendingCreateChannelFutures.remove(createChannelFuture);

                    if (future.isSuccess()) {
                        final Channel channel = future.getNow();

                        ApnsChannelPool.this.allChannels.add(channel);
                        ApnsChannelPool.this.metricsListener.handleConnectionAdded();

                        acquirePromise.trySuccess(channel);
                    } else {
                        ApnsChannelPool.this.metricsListener.handleConnectionCreationFailed();

                        acquirePromise.tryFailure(future.cause());

                        // If we failed to open a connection, this is the end of the line for this acquisition
                        // attempt, and callers won't be able to release the channel (since they didn't get one
                        // in the first place). Move on to the next acquisition attempt if one is present.
                        ApnsChannelPool.this.handleNextAcquisition();
                    }
                }
            });
        } else {
            final Channel channelFromIdlePool = ApnsChannelPool.this.idleChannels.poll();

            if (channelFromIdlePool != null) {
                if (channelFromIdlePool.isActive()) {
                    acquirePromise.trySuccess(channelFromIdlePool);
                } else {
                    // The channel from the idle pool isn't usable; discard it and create a new one instead
                    this.discardChannel(channelFromIdlePool);
                    this.acquireWithinEventExecutor(acquirePromise);
                }
            } else {
                // We don't have any connections ready to go, and don't have any more capacity to create new
                // channels. Add this acquisition to the queue waiting for channels to become available.
                pendingAcquisitionPromises.add(acquirePromise);
            }
        }
    } else {
        acquirePromise.tryFailure(POOL_CLOSED_EXCEPTION);
    }
}

From source file:com.turo.pushy.apns.ApnsClient.java

License:Open Source License

/**
 * <p>Sends a push notification to the APNs gateway.</p>
 *
 * <p>This method returns a {@code Future} that indicates whether the notification was accepted or rejected by the
 * gateway. If the notification was accepted, it may be delivered to its destination device at some time in the
 * future, but final delivery is not guaranteed. Rejections should be considered permanent failures, and callers
 * should <em>not</em> attempt to re-send the notification.</p>
 *
 * <p>The returned {@code Future} may fail with an exception if the notification could not be sent. Failures to
 * <em>send</em> a notification to the gatewayi.e. those that fail with exceptionsshould generally be considered
 * non-permanent, and callers should attempt to re-send the notification when the underlying problem has been
 * resolved.</p>/*from w w w  . jav  a 2 s . c o m*/
 *
 * @param notification the notification to send to the APNs gateway
 *
 * @param <T> the type of notification to be sent
 *
 * @return a {@code Future} that will complete when the notification has been either accepted or rejected by the
 * APNs gateway
 *
 * @see com.turo.pushy.apns.util.concurrent.PushNotificationResponseListener
 *
 * @since 0.8
 */
@SuppressWarnings("unchecked")
public <T extends ApnsPushNotification> PushNotificationFuture<T, PushNotificationResponse<T>> sendNotification(
        final T notification) {
    final PushNotificationFuture<T, PushNotificationResponse<T>> responseFuture;

    if (!this.isClosed.get()) {
        final PushNotificationPromise<T, PushNotificationResponse<T>> responsePromise = new PushNotificationPromise<>(
                this.eventLoopGroup.next(), notification);

        final long notificationId = this.nextNotificationId.getAndIncrement();

        this.channelPool.acquire().addListener(new GenericFutureListener<Future<Channel>>() {
            @Override
            public void operationComplete(final Future<Channel> acquireFuture) throws Exception {
                if (acquireFuture.isSuccess()) {
                    final Channel channel = acquireFuture.getNow();

                    channel.writeAndFlush(responsePromise)
                            .addListener(new GenericFutureListener<ChannelFuture>() {

                                @Override
                                public void operationComplete(final ChannelFuture future) throws Exception {
                                    if (future.isSuccess()) {
                                        ApnsClient.this.metricsListener.handleNotificationSent(ApnsClient.this,
                                                notificationId);
                                    }
                                }
                            });

                    ApnsClient.this.channelPool.release(channel);
                } else {
                    responsePromise.tryFailure(acquireFuture.cause());
                }
            }
        });

        responsePromise.addListener(new PushNotificationResponseListener<T>() {
            @Override
            public void operationComplete(final PushNotificationFuture<T, PushNotificationResponse<T>> future)
                    throws Exception {
                if (future.isSuccess()) {
                    final PushNotificationResponse response = future.getNow();

                    if (response.isAccepted()) {
                        ApnsClient.this.metricsListener.handleNotificationAccepted(ApnsClient.this,
                                notificationId);
                    } else {
                        ApnsClient.this.metricsListener.handleNotificationRejected(ApnsClient.this,
                                notificationId);
                    }
                } else {
                    ApnsClient.this.metricsListener.handleWriteFailure(ApnsClient.this, notificationId);
                }
            }
        });

        responseFuture = responsePromise;
    } else {
        final PushNotificationPromise<T, PushNotificationResponse<T>> failedPromise = new PushNotificationPromise<>(
                GlobalEventExecutor.INSTANCE, notification);

        failedPromise.setFailure(CLIENT_CLOSED_EXCEPTION);

        responseFuture = failedPromise;
    }

    return responseFuture;
}

From source file:com.twitter.http2.PipeTest.java

License:Apache License

@Test
public void testSR() {
    Future<Void> sendFuture = pipe.send(MESSAGE);
    assertFalse(sendFuture.isDone());//  w w  w .  java 2  s. c  o m

    Future<Object> recvFuture = pipe.receive();
    assertSame(MESSAGE, recvFuture.getNow());
    assertTrue(sendFuture.isSuccess());
}

From source file:com.twitter.http2.PipeTest.java

License:Apache License

@Test
public void testRS() {
    Future<Object> recvFuture = pipe.receive();
    assertFalse(recvFuture.isDone());/*w  ww  .  j  a v  a  2s.  c o m*/

    Future<Void> sendFuture = pipe.send(MESSAGE);
    assertTrue(sendFuture.isSuccess());
    assertSame(MESSAGE, recvFuture.getNow());
}

From source file:com.twitter.http2.PipeTest.java

License:Apache License

@Test
public void testSRR() {
    Future<Void> sendFuture1 = pipe.send(MESSAGE);
    assertFalse(sendFuture1.isDone());//from w ww.  j a  v a  2 s. c o m

    Future<Object> recvFuture1 = pipe.receive();
    assertSame(MESSAGE, recvFuture1.getNow());
    assertTrue(sendFuture1.isSuccess());

    Future<Object> recvFuture2 = pipe.receive();
    assertFalse(recvFuture2.isDone());
    assertTrue(recvFuture1.isSuccess());
    assertTrue(sendFuture1.isSuccess());
}

From source file:com.twitter.http2.PipeTest.java

License:Apache License

@Test
public void testSSR() {
    Future<Void> sendFuture1 = pipe.send(MESSAGE1);
    assertFalse(sendFuture1.isDone());/*www  .j  ava2s.  c om*/

    Future<Void> sendFuture2 = pipe.send(MESSAGE2);
    assertFalse(sendFuture2.isDone());
    assertFalse(sendFuture1.isDone());

    Future<Object> recvFuture = pipe.receive();
    assertSame(MESSAGE1, recvFuture.getNow());
    assertFalse(sendFuture2.isDone());
    assertTrue(sendFuture1.isSuccess());
}

From source file:com.twitter.http2.PipeTest.java

License:Apache License

@Test
public void testSRS() {
    Future<Void> sendFuture1 = pipe.send(MESSAGE1);
    assertFalse(sendFuture1.isDone());// w  w w.j a v  a 2 s.  c  om

    Future<Object> recvFuture = pipe.receive();
    assertSame(MESSAGE1, recvFuture.getNow());
    assertTrue(sendFuture1.isSuccess());

    Future<Void> sendFuture2 = pipe.send(MESSAGE2);
    assertFalse(sendFuture2.isDone());
    assertTrue(recvFuture.isSuccess());
    assertTrue(sendFuture1.isSuccess());
}

From source file:com.twitter.http2.PipeTest.java

License:Apache License

@Test
public void testRSR() {
    Future<Object> recvFuture1 = pipe.receive();
    assertFalse(recvFuture1.isDone());//from   w w w  . j  a v  a 2  s  .co m

    Future<Void> sendFuture = pipe.send(MESSAGE);
    assertTrue(sendFuture.isSuccess());
    assertSame(MESSAGE, recvFuture1.getNow());

    Future<Object> recvFuture2 = pipe.receive();
    assertFalse(recvFuture2.isDone());
    assertTrue(sendFuture.isSuccess());
    assertTrue(recvFuture1.isSuccess());
}

From source file:com.twitter.http2.PipeTest.java

License:Apache License

@Test
public void testRSS() {
    Future<Object> recvFuture = pipe.receive();
    assertFalse(recvFuture.isDone());/*from ww  w  .  j ava 2  s . com*/

    Future<Void> sendFuture1 = pipe.send(MESSAGE1);
    assertTrue(sendFuture1.isSuccess());
    assertSame(MESSAGE1, recvFuture.getNow());

    Future<Void> sendFuture2 = pipe.send(MESSAGE2);
    assertFalse(sendFuture2.isDone());
    assertTrue(sendFuture1.isSuccess());
    assertTrue(recvFuture.isSuccess());
}

From source file:com.twitter.http2.PipeTest.java

License:Apache License

@Test
public void testRRS() {
    Future<Object> recvFuture1 = pipe.receive();
    assertFalse(recvFuture1.isDone());//from w  w  w  .ja  v a  2  s  .co  m

    Future<Object> recvFuture2 = pipe.receive();
    assertFalse(recvFuture2.isDone());
    assertFalse(recvFuture1.isDone());

    Future<Void> sendFuture = pipe.send(MESSAGE);
    assertTrue(sendFuture.isSuccess());
    assertFalse(recvFuture2.isDone());
    assertSame(MESSAGE, recvFuture1.getNow());
}