2017-02-08 16 views
2

Google Guava 캐시를 사용하여 서비스 관련 객체로 캐시를 시도하고 있습니다. 캐시 미스에서, 나는 내 REST 클라이언트를 사용하여 객체를 가져온다. Guava CacheLoader가 사용자 정의 예외를 throw하고 catch합니다.

CacheLoader<Key, Graph> loader = new CacheLoader<Key, Graph>() { 
    public Graph load(Key key) throws InternalServerException, ResourceNotFoundException { 
     return client.get(key); 
    } 
    }; 
    LoadingCache<Key, Graph> cache = CacheBuilder.newBuilder().build(loader) 

지금, client.getKey(Key k) 실제로 InternalServerExceptionResourceNotFoundException 발생

: 나는 다음과 같은 방법으로이 작업을 수행 할 수 있다는 것을 알고. 이 캐시 인스턴스를 사용하여 객체를 가져 오려고하면 예외를 ExecutionException으로 잡을 수 있습니다.

try { 
    cache.get(key); 
} catch (ExecutionException e){ 

} 

하지만, 내가 특별히 잡을 내가 정의한 CacheLoader이 (즉. InternalServerExceptionResourceNotFoundException)를 던지고 예외를 처리하고 싶습니다. ExecutionException의 인스턴스가, 하나가 작동 부하의 서명을 야기 내 자신의 예외 중 하나인지 여부를 확인하는 경우

는 잘 모르겠어요() 메소드는 실제로 Exception하지 ExecutionException가 발생합니다. instanceof을 사용할 수 있다고해도 매우 깨끗해 보이지 않습니다. 이 문제를 해결하기위한 좋은 appraoches가 있습니까? javadocs에서

답변

2

:

ExecutionException - 값을로드하는 동안 체크 예외가 발생 된 경우. (계산이 해 InterruptedException에 의해 중단 된 경우에도 ExecutionException가 발생합니다.)

UncheckedExecutionException을 -

당신은 getCause를 호출하여 잡은 ExecutionException의 원인을 확인하는 데 필요한 값을로드하는 동안 체크되지 않은 예외가 발생 된 경우() :

} catch (ExecutionException e){ 
    if(e.getCause() instanceof InternalServerException) { 
     //handle internal server error 
    } else if(e.getCause() instanceof ResourceNotFoundException) { 
     //handle resource not found 
    } 
}