Example usage for io.netty.util Timeout task

List of usage examples for io.netty.util Timeout task

Introduction

In this page you can find the example usage for io.netty.util Timeout task.

Prototype

TimerTask task();

Source Link

Document

Returns the TimerTask which is associated with this handle.

Usage

From source file:com.king.platform.net.http.netty.pool.PoolingChannelPool.java

License:Apache License

public PoolingChannelPool(final Timer cleanupTimer, TimeProvider timeProvider, long timeoutInSeconds,
        final MetricCallback metricCallback) {
    this.timeProvider = timeProvider;
    this.metricCallback = metricCallback;

    maxTtl = TimeUnit.SECONDS.toMillis(timeoutInSeconds);

    cleanupTimer.newTimeout(new TimerTask() {
        @Override//from w  w  w  . ja v a2 s  . c  om
        public void run(Timeout timeout) throws Exception {

            for (Map.Entry<ServerInfo, ServerPool> poolEntry : serverPoolMap.entrySet()) {
                ServerPool serverPool = poolEntry.getValue();
                ServerInfo serverInfo = poolEntry.getKey();

                serverPool.cleanExpiredConnections();
                if (serverPool.shouldRemovePool()) {
                    ServerPool remove = serverPoolMap.remove(serverInfo);
                    if (remove != null) {
                        metricCallback.onRemovedServerPool(serverInfo.getHost());
                    }
                }

            }

            cleanupTimer.newTimeout(timeout.task(), maxTtl, TimeUnit.MILLISECONDS);

        }
    }, maxTtl, TimeUnit.MILLISECONDS);
}

From source file:io.pravega.controller.timeout.TimerWheelTimeoutService.java

License:Open Source License

@Override
public PingTxnStatus pingTxn(final String scope, final String stream, final UUID txnId, long lease) {

    if (!this.isRunning()) {
        return PingTxnStatus.newBuilder().setStatus(PingTxnStatus.Status.DISCONNECTED).build();
    }//from w  w  w  .  j a va 2s . c o m

    final String key = getKey(scope, stream, txnId);
    Preconditions.checkState(map.containsKey(key), "Stream not found in the map");

    final TxnData txnData = map.get(key);

    if (lease > maxLeaseValue || lease > txnData.getScaleGracePeriod()) {
        return PingTxnStatus.newBuilder().setStatus(PingTxnStatus.Status.LEASE_TOO_LARGE).build();
    }

    if (lease + System.currentTimeMillis() > txnData.getMaxExecutionTimeExpiry()) {
        return PingTxnStatus.newBuilder().setStatus(PingTxnStatus.Status.MAX_EXECUTION_TIME_EXCEEDED).build();
    } else {
        Timeout timeout = txnData.getTimeout();
        timeout.cancel();
        Timeout newTimeout = hashedWheelTimer.newTimeout(timeout.task(), lease, TimeUnit.MILLISECONDS);
        txnData.setTimeout(newTimeout);
        return PingTxnStatus.newBuilder().setStatus(PingTxnStatus.Status.OK).build();
    }

}

From source file:org.apache.pulsar.client.impl.TopicsConsumerImplTest.java

License:Apache License

/**
 * Test topic partitions auto subscribed.
 *
 * Steps://  w ww  . jav  a2  s  .  co m
 * 1. Create a consumer with 2 topics, and each topic has 2 partitions: xx-partition-0, xx-partition-1.
 * 2. produce message to xx-partition-2, and verify consumer could not receive message.
 * 3. update topics to have 3 partitions.
 * 4. trigger partitionsAutoUpdate. this should be done automatically, this is to save time to manually trigger.
 * 5. produce message to xx-partition-2 again,  and verify consumer could receive message.
 *
 */
@Test(timeOut = 30000)
public void testTopicAutoUpdatePartitions() throws Exception {
    String key = "TestTopicAutoUpdatePartitions";
    final String subscriptionName = "my-ex-subscription-" + key;
    final String messagePredicate = "my-message-" + key + "-";
    final int totalMessages = 6;

    final String topicName1 = "persistent://prop/use/ns-abc/topic-1-" + key;
    final String topicName2 = "persistent://prop/use/ns-abc/topic-2-" + key;
    List<String> topicNames = Lists.newArrayList(topicName1, topicName2);

    admin.tenants().createTenant("prop", new TenantInfo());
    admin.topics().createPartitionedTopic(topicName1, 2);
    admin.topics().createPartitionedTopic(topicName2, 2);

    // 1. Create a  consumer
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topics(topicNames).subscriptionName(subscriptionName)
            .subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS)
            .receiverQueueSize(4).autoUpdatePartitions(true).subscribe();
    assertTrue(consumer instanceof MultiTopicsConsumerImpl);

    MultiTopicsConsumerImpl topicsConsumer = (MultiTopicsConsumerImpl) consumer;

    // 2. use partition-2 producer,
    Producer<byte[]> producer1 = pulsarClient.newProducer().topic(topicName1 + "-partition-2")
            .enableBatching(false).create();
    Producer<byte[]> producer2 = pulsarClient.newProducer().topic(topicName2 + "-partition-2")
            .enableBatching(false).create();
    for (int i = 0; i < totalMessages; i++) {
        producer1.send((messagePredicate + "topic1-partition-2 index:" + i).getBytes());
        producer2.send((messagePredicate + "topic2-partition-2 index:" + i).getBytes());
        log.info("produce message to partition-2. message index: {}", i);
    }
    // since partition-2 not subscribed,  could not receive any message.
    Message<byte[]> message = consumer.receive(200, TimeUnit.MILLISECONDS);
    assertNull(message);

    // 3. update to 3 partitions
    admin.topics().updatePartitionedTopic(topicName1, 3);
    admin.topics().updatePartitionedTopic(topicName2, 3);

    // 4. trigger partitionsAutoUpdate. this should be done automatically in 1 minutes,
    // this is to save time to manually trigger.
    log.info("trigger partitionsAutoUpdateTimerTask");
    Timeout timeout = topicsConsumer.getPartitionsAutoUpdateTimeout();
    timeout.task().run(timeout);
    Thread.sleep(200);

    // 5. produce message to xx-partition-2 again,  and verify consumer could receive message.
    for (int i = 0; i < totalMessages; i++) {
        producer1.send((messagePredicate + "topic1-partition-2 index:" + i).getBytes());
        producer2.send((messagePredicate + "topic2-partition-2 index:" + i).getBytes());
        log.info("produce message to partition-2 again. messageindex: {}", i);
    }
    int messageSet = 0;
    message = consumer.receive();
    do {
        messageSet++;
        consumer.acknowledge(message);
        log.info("4 Consumer acknowledged : " + new String(message.getData()));
        message = consumer.receive(200, TimeUnit.MILLISECONDS);
    } while (message != null);
    assertEquals(messageSet, 2 * totalMessages);

    consumer.close();
}