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

IceInternal.QueueExecutorService Maven / Gradle / Ivy

Go to download

Ice is a comprehensive RPC framework that helps you build distributed applications with minimal effort using familiar object-oriented idioms

The newest version!
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

package IceInternal;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.RejectedExecutionException;

public final class QueueExecutorService
{
    QueueExecutorService(ExecutorService executor)
    {
        _executor = executor;
        _thread = executeNoThrow(new Callable()
        {
            @Override
            public Thread call()
            {
                return Thread.currentThread();
            }
        });
    }

    public  T executeNoThrow(Callable callable)
    {
        try
        {
            return execute(callable);
        }
        catch(RetryException ex)
        {
            assert(false);
            return null;
        }
    }

    public  T execute(Callable callable)
        throws RetryException
    {
        if(_thread == Thread.currentThread())
        {
            try
            {
                return callable.call();
            }
            catch(RuntimeException ex)
            {
                throw ex;
            }
            catch(Exception ex)
            {
                // RetryException is the only checked exception that
                // can be raised by Ice internals.
                assert(ex instanceof RetryException);
                throw (RetryException)ex;
            }
        }

        boolean interrupted = false;
        try
        {
            Future future = _executor.submit(callable);
            while(true)
            {
                try
                {
                    T value = future.get();
                    return value;
                }
                catch(InterruptedException ex)
                {
                    interrupted = true;
                }
            }
        }
        catch(RejectedExecutionException e)
        {
            throw new Ice.CommunicatorDestroyedException();
        }
        catch(ExecutionException e)
        {
            try
            {
                throw e.getCause();
            }
            catch(RuntimeException ex)
            {
                throw ex;
            }
            catch(Throwable ex)
            {
                // RetryException is the only checked exception that
                // can be raised by Ice internals.
                assert(ex instanceof RetryException);
                throw (RetryException)ex;
            }
        }
        finally
        {
            if(interrupted)
            {
                Thread.currentThread().interrupt();
            }
        }
    }

    final ExecutorService _executor;
    final Thread _thread;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy