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

org.redisson.executor.RedissonExecutorBatchFuture Maven / Gradle / Ivy

/**
 * Copyright 2018 Nikita Koksharov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson.executor;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.redisson.api.RExecutorBatchFuture;
import org.redisson.api.RExecutorFuture;
import org.redisson.misc.RedissonPromise;

import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;

/**
 * 
 * @author Nikita Koksharov
 *
 */
public class RedissonExecutorBatchFuture extends RedissonPromise implements RExecutorBatchFuture {

    private List> futures;

    public RedissonExecutorBatchFuture(List> futures) {
        this.futures = futures;
        
        final AtomicInteger counter = new AtomicInteger(futures.size());
        for (RExecutorFuture future : futures) {
            future.addListener(new FutureListener() {
                @Override
                public void operationComplete(Future future) throws Exception {
                    if (!future.isSuccess()) {
                        RedissonExecutorBatchFuture.this.tryFailure(future.cause());
                        return;
                    }
                    
                    if (counter.decrementAndGet() == 0) {
                        RedissonExecutorBatchFuture.this.trySuccess(null);
                    }
                }
            });
        }
    }
    
    @Override
    public List> getTaskFutures() {
        return futures;
    }
    
}