잡동사니

반응형

질문

인터페이스가 있습니다.

public interface ThirdPartySystemCaller {
    void sendRequest(String request) throws ThirdPartySystemException;
}

그리고 구현 코드 :

@Slf4j
@Service
public class ThirdPartySystemCallerImpl implements ThirdPartySystemCaller {

    @Override
    public void sendRequest(String request) throws ThirdPartySystemException {

        if (request == null) throw new ThirdPartySystemException();

        log.info("send: {}", request);
    }
}

그리고 CryptoService 마녀가 요청에 서명 할 수 있습니다.

public interface CryptoService {
    String signRequest(String request) throws CryptoException;
}

그리고 그것은 구현 :

@Slf4j
@Service
public class CryptoServiceImpl implements CryptoService {

    @Override
    public String signRequest(String request) throws CryptoException {
        if (request.length() > 100) throw new CryptoException(); //just for example
        return "signed " + request;
    }
}

이제 다음 서비스를 사용할 수 있습니다.

String signedRequest = cryptoService.signRequest("Hello"); 
thirdPartySystemCaller.sendRequest(signedRequest); 

하지만 매번 두 서비스 모두에 전화해야합니다. 프록시 를 만들고 싶습니다.

@Slf4j
@Service
public class ThirdPartySystemCallerSignedProxy implements ThirdPartySystemCaller {

    private final ThirdPartySystemCaller thirdPartySystemCaller;
    private final CryptoService cryptoService;

    public ThirdPartySystemCallerSignedProxy(ThirdPartySystemCaller thirdPartySystemCaller, CryptoService cryptoService) {
        this.thirdPartySystemCaller = thirdPartySystemCaller;
        this.cryptoService = cryptoService;
    }

    @Override
    public void sendRequest(String request) throws ThirdPartySystemException {
        String signedRequest = cryptoService.signRequest(request);
        thirdPartySystemCaller.sendRequest(signedRequest);
    }
}

하지만 제 ThirdPartySystemCallerSignedProxyThirdPartySystemCaller인터페이스를 구현하고 sendRequest메서드는 ThirdPartySystemException만 throw합니다. 하지만 cryptoServiceCryptoException을 던지면 나도 던질 필요가 있습니다.

제가 어떻게 해야 할까요?

확인되지 않은 예외를 만들려고 했지만 확인해야 합니다.

 

답변1

기본 예외 생성

ThirdPartySystemExceptionCryptoException에 대한 기본 예외가 될 수있는 추상 예외 BusinessException 을 만들 수 있습니다. 이제 sendRequest메소드가 BusinessException을 발생시키고 실제 예외는 주어진 문제에 따라 달라진다는 것을 정의할 수 있습니다.

정면

ThirdPartySystemCallerSignedProxy Proxy 구현 한 패턴이 아닙니다. 이 수업은 더 간단한 API 로 통합 인터페이스를 만들고 싶기 때문에 Facade 패턴을 상기시킵니다. 두 개의 다른 인터페이스에 대해. 이 경우 ThirdPartySystemException에 throw 될 경우 CryptoException을 래핑하거나 기본 예외를 생성하고 메서드에서 선언 할 수 있습니다. 어떤 종류의 예외가 발생할지 모르지만 확실히 BusinessException이 될 것이기 때문에 더 좋습니다.

Chain of Responsibility

많은 라이브러리에서 Chain of Responsibility을 사용하여 요청 - 응답커뮤니케이션. 모든 체인 셀은 필요한 경우 선언에서 기본 예외와 함께 동일한 인터페이스를 구현해야합니다. 빈 정의에서 체인을 구축 할 수 있습니다. 모든 셀이 독립적이고 Facade에서처럼 서로에 대해 알 필요가 없기 때문에 유지 관리가 조금 더 쉽습니다. 아래와 같이 @Bean메소드 선언에서 체인을 빌드 할 수 있습니다.

@Bean
public ServiceChainCell thirdPartyCaller() {
    CryptoService crypto = cryptoService();
    ThirdPartySystemCaller caller = thirdPartySystemCaller();

    // Create chain
    crypto.setNext(caller);

    return crypto;
}

setNext메서드는 sendRequest (String request)메서드도 있어야하는 ServiceChainCell인터페이스에서 가져옵니다.

이러한 패턴에 대해 자세히 읽어 보면 가장 적합한 솔루션을 찾을 수 있습니다.



 

 

 

 

출처 : https://stackoverflow.com/questions/55418558/i-can-not-to-forward-exceptions-from-my-proxy-class

반응형

이 글을 공유합시다

facebook twitter googleplus kakaoTalk kakaostory naver band