wrap checked TimeoutException with runtime ElasticSearchTimeoutException

This commit is contained in:
kimchy 2010-03-05 01:48:56 +02:00
parent 7bf0f1ffca
commit 7a38e384c9
4 changed files with 66 additions and 7 deletions

View File

@ -20,7 +20,9 @@
package org.elasticsearch; package org.elasticsearch;
/** /**
* @author kimchy (Shay Banon) (Shay Banon) * The same as {@link InterruptedException} simply a runtime one.
*
* @author kimchy (shay.banon)
*/ */
public class ElasticSearchInterruptedException extends ElasticSearchException { public class ElasticSearchInterruptedException extends ElasticSearchException {

View File

@ -0,0 +1,36 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you 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.elasticsearch;
/**
* The same as {@link java.util.concurrent.TimeoutException} simply a runtime one.
*
* @author kimchy (shay.banon)
*/
public class ElasticSearchTimeoutException extends ElasticSearchException {
public ElasticSearchTimeoutException(String message) {
super(message);
}
public ElasticSearchTimeoutException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -23,16 +23,34 @@ import org.elasticsearch.ElasticSearchException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/** /**
* @author kimchy (Shay Banon) * An extension to {@link Future} allowing for simplified "get" operations.
*
* @author kimchy (shay.banon)
*/ */
public interface ActionFuture<T> extends Future<T> { public interface ActionFuture<T> extends Future<T> {
/**
* Similar to {@link #get()}, just wrapping the {@link InterruptedException} with
* {@link org.elasticsearch.ElasticSearchInterruptedException}, and throwing the actual
* cause of the {@link java.util.concurrent.ExecutionException}.
*/
T actionGet() throws ElasticSearchException; T actionGet() throws ElasticSearchException;
T actionGet(long timeoutMillis) throws ElasticSearchException, TimeoutException; /**
* Similar to {@link #get(long, java.util.concurrent.TimeUnit)}, just wrapping the {@link InterruptedException} with
* {@link org.elasticsearch.ElasticSearchInterruptedException}, and throwing the actual
* cause of the {@link java.util.concurrent.ExecutionException}.
*
* @param timeoutMillis Timeout in millis
*/
T actionGet(long timeoutMillis) throws ElasticSearchException;
T actionGet(long timeout, TimeUnit unit) throws ElasticSearchException, TimeoutException; /**
* Similar to {@link #get(long, java.util.concurrent.TimeUnit)}, just wrapping the {@link InterruptedException} with
* {@link org.elasticsearch.ElasticSearchInterruptedException}, and throwing the actual
* cause of the {@link java.util.concurrent.ExecutionException}.
*/
T actionGet(long timeout, TimeUnit unit) throws ElasticSearchException;
} }

View File

@ -21,6 +21,7 @@ package org.elasticsearch.action.support;
import org.elasticsearch.ElasticSearchException; import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.ElasticSearchInterruptedException; import org.elasticsearch.ElasticSearchInterruptedException;
import org.elasticsearch.ElasticSearchTimeoutException;
import org.elasticsearch.action.ActionFuture; import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.transport.TransportException; import org.elasticsearch.transport.TransportException;
@ -109,13 +110,15 @@ public class PlainActionFuture<T> implements ActionFuture<T>, ActionListener<T>
} }
} }
@Override public T actionGet(long timeoutMillis) throws ElasticSearchException, TimeoutException { @Override public T actionGet(long timeoutMillis) throws ElasticSearchException {
return actionGet(timeoutMillis, TimeUnit.MILLISECONDS); return actionGet(timeoutMillis, TimeUnit.MILLISECONDS);
} }
@Override public T actionGet(long timeout, TimeUnit unit) throws ElasticSearchException, TimeoutException { @Override public T actionGet(long timeout, TimeUnit unit) throws ElasticSearchException {
try { try {
return get(timeout, unit); return get(timeout, unit);
} catch (TimeoutException e) {
throw new ElasticSearchTimeoutException(e.getMessage());
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new ElasticSearchInterruptedException(e.getMessage()); throw new ElasticSearchInterruptedException(e.getMessage());
} catch (ExecutionException e) { } catch (ExecutionException e) {