IceInternal.QueueExecutorService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ice Show documentation
Show all versions of ice Show documentation
Ice is a comprehensive RPC framework that helps you build distributed applications with minimal effort using familiar object-oriented idioms
// **********************************************************************
//
// Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
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 - 2025 Weber Informatics LLC | Privacy Policy