서비스 연동

업무서비스에서 다른 업무서비스를 호출하는 것을 서비스연동이라고 한다. 거래호출시 수행되는 선/후처리 모듈들 때문에 서비스연동시 단순히 서비스객체의 메소드를 호출하면 안되고 ServiceExecutor을 통해서 서비스를 호출해야한다. default extension에서 ServiceExecutor로 DefaultServiceExecutor를 사용하고 있다.

서비스연동
Figure 1. 서비스연동

1. 서비스연동의 구현

서비스연동을 구현할 때는 서비스연동 클래스(TransactionCodeServiceExecutor, ServiceOperationServiceExecutor)중 하나를 구현해야 한다. 거래코드를 사용하는 경우이면 TransactionCodeServiceExecutor를 구현한고 이외의 경우에는 ServiceOperationServiceExecutor를 구현한다. 두개의 인터페이스는 동일한 메소드를 가지고 있으나 서비스를 구별하는 key 로 거래코드 또는 어플리케이션/서비스/오퍼레이션을 사용하는지가 구별된다.

  • 동기/비동기 서비스연동

    • 동기 : Callee 서비스를 Caller서비스를 실행하는 쓰레드에서 수행

    • 비동기 : Callee 서비스를 Caller서비스를 실행하는 쓰레드와 다른 별도의 쓰레드에서 수행

  • 서비스연동의 메소드(TransactionCodeServiceExecutor기준)

구현해야할 메소드가 static메소드여서 상속을 강제하기 위해 메소드자체는 Exception을 던진다.

public static <R, S> S execute( String trxCd, R serviceArg) throws NestedRuntimeException, ApplicationException {
    throw new UnsupportedOperationException("Not supported by this class. Please inherit this class.");
}

public static <R, S> S execute( String trxCd, R serviceArg, boolean newTran) throws NestedRuntimeException, ApplicationException {
    throw new UnsupportedOperationException("Not supported by this class. Please inherit this class.");
}

public static <R> void  executeAsync( final String trxCd, final R serviceArg, long timeout) throws ResourceTimedOutException, NestedRuntimeException, ApplicationException {
    throw new UnsupportedOperationException("Not supported by this class. Please inherit this class.");
}

public static <R> void  executeAsync( final String trxCd, final R serviceArg, long timeout, boolean newTran) throws ResourceTimedOutException, NestedRuntimeException, ApplicationException {
    throw new UnsupportedOperationException("Not supported by this class. Please inherit this class.");
}

public static <R, S> AsyncResponse<S> executeAsyncForWait( final String trxCd, final R serviceArg, long timeout) throws ResourceTimedOutException, NestedRuntimeException, ApplicationException {
    throw new UnsupportedOperationException("Not supported by this class. Please inherit this class.");
}

public static <R, S> AsyncResponse<S> executeAsyncForWait(final String trxCd, final R serviceArg, long timeout, boolean newTran) throws ResourceTimedOutException, NestedRuntimeException, ApplicationException {
    throw new UnsupportedOperationException("Not supported by this class. Please inherit this class.");
}

1.1. 동기식서비스연동(execute)

거래코드로 등록된 서비스를 호출하여 응답객체를 반환한다.

동기식서비스연동(execute) 메소드 파라미터
입력/출력구분 파라미터 설명

입력파라미터

String trxCd

거래코드

R serviceArg

Callee 서비스 입력 객체

boolean newTran

새로운 트랜잭션 시작여부

(지정하지 않으면 기존 트랜잭션을 사용한다.)

true : 새로운 트랜잭션 시작

false: 기존 트랜잭션을 사용

반환값

S

Callee 서비스 응답 객체

예외

NestedRuntimeException

Callee 서비스 실행 중 RuntimeException이 발생한 경우 이를 Wrapping하여 throw한다.

ApplicationException

Callee 서비스 실행중 ApplicationException, CheckedException이 발생한 경우 이를 Wrapping하여 throw한다.

1.2. 비동기식서비스연동(executeAsync)

거래코드로 등록된 서비스를 비동기호출하고 응답을 기다리지 않는다.

비동기식서비스연동(executeAsync) 메소드 파라미터
입력/출력구분 파라미터 설명

입력파라미터

String trxCd

거래코드

R serviceArg

Callee 서비스 입력 객체

long timeout

비동기호출을 위해 사용할 가용 리소스(쓰레드)를 획득할 때 적용할 타임아웃, milliseconds

ex) 1000 : 가용 리소스가 없는 경우 1초 이내로 기다린다.

AsyncTaskExecutor.NOWAIT : 가용 리소스가 없는 경우 바로 RuntimeException이 발생한다.

AsyncTaskExecutor.WAIT_INDEFINITE : 가용 리소스가 확보될 때 까지 기다린다.

boolean newTran

새로운 트랜잭션 시작여부

(지정하지 않으면 기존 트랜잭션을 사용한다.)

true : 새로운 트랜잭션 시작

false: 기존 트랜잭션을 사용

비동기호출시 기존 트랜잭션과 같이 사용할 수 없으므로 항상 true와 같다.

반환값

S

Callee 서비스 응답 객체

예외

NestedRuntimeException

Callee 서비스 실행 중 RuntimeException이 발생한 경우 이를 Wrapping하여 throw한다.

ApplicationException

Callee 서비스 실행중 ApplicationException, CheckedException이 발생한 경우 이를 Wrapping하여 throw한다.

ApplicationException

파라미터로 전달된 timeout이 상수(NOWAIT, WAIT_INDEFINITE)가 아닌 사용자 임의값이고 대기 시간을 초과한 경우 발생한다.

1.3. 비동기식서비스연동(executeAsyncForWait)

거래코드로 등록된 서비스를 비동기호출하고 응답을 처리하기 위한 AsyncResponse객체를 반환한다. Callee 서비스를 비동기로 호출하고 병렬로 다른 작업을 진행하다가 서비스 응답을 받아서 처리하는 경우 사용할 수 있다.

비동기식서비스연동(executeAsyncForWait) 메소드 파라미터
입력/출력구분 파라미터 설명

입력파라미터

String trxCd

거래코드

R serviceArg

Callee 서비스 입력 객체

long timeout

비동기호출을 위해 사용할 가용 리소스(쓰레드)를 획득할 때 적용할 타임아웃, milliseconds

ex) 1000 : 가용 리소스가 없는 경우 1초 이내로 기다린다.

AsyncTaskExecutor.NOWAIT : 가용 리소스가 없는 경우 바로 RuntimeException이 발생한다.

AsyncTaskExecutor.WAIT_INDEFINITE : 가용 리소스가 확보될 때 까지 기다린다.

boolean newTran

새로운 트랜잭션 시작여부

(지정하지 않으면 기존 트랜잭션을 사용한다.)

true : 새로운 트랜잭션 시작

false: 기존 트랜잭션을 사용

비동기호출시 기존 트랜잭션과 같이 사용할 수 없으므로 항상 true와 같다.

반환값

AsyncResponse<S>

비동기호출 응답 객체

Callee 서비스 호출 후 즉시 반환된다. 사용자는 전달받은 AsyncResponse를 이용하여 Callee 서비스의 응답을 처리할 시점에 응답을 반환받을 수 있다.

Callee 서비스가 실행중인 경우 처리를 완료할 때 까지 대기한다. 만약 Callee 서비스에서 예외가 발생한 경우 예외가 throw된다.

예외

NestedRuntimeException

Callee 서비스 실행 중 RuntimeException이 발생한 경우 이를 Wrapping하여 throw한다.

ApplicationException

Callee 서비스 실행중 ApplicationException, CheckedException이 발생한 경우 이를 Wrapping하여 throw한다.

ApplicationException

파라미터로 전달된 timeout이 상수(NOWAIT, WAIT_INDEFINITE)가 아닌 사용자 임의값이고 대기 시간을 초과한 경우 발생한다.

2. 서비스연동의 권장구현

서비스연동을 구현할 때 서비스연동 기능을 구현한 GenericServiceExecutor를 사용하여 기능을 위임하는 것이 바람직하다. 이때 사용자 환경에 맞게 Caller, Callee의 시스템헤더/DataContainer를 설정할 수 있는 방법이 필요하다. 이러한 경우 ServiceExecutorInterceptor의 기능을 이용하여 Caller, Callee의 시스템헤더/DataContainer을 조작할 수 있다.

  • GenericServiceExecutor를 이용한 ServiceExecutor구현 예

public final class DefaultServiceExecutor extends TransactionCodeServiceExecutor
{
    ...

    public static <R, S> S execute( String trxCd, R serviceArg) throws NestedRuntimeException, DefaultApplicationException
    {
        try {
            return GenericServiceExecutor.execute(trxCd, serviceArg, false);
        } catch (ApplicationException e) {
            throw new DefaultApplicationException(DefaultConst.DEFAULT_ERROR_CODE, null, new Object[] {}, e);
        }
    }
    ...
  • 서비스연동시 Prepare, Execute단계를 거치며 연동을 수행하게 된다. Prepare단계의 beforeExecution, afterExecution은 Caller, Callee의 시스템헤더/DataContainer를 수정하기위한 단계이다. 이 두개의 API는 ServiceExecutorInterceptor에 정의되어 있다.

서비스연동 내부 흐름
Figure 2. 서비스연동 내부 흐름

3. 서비스연동-ServiceExecutorInterceptor 인터페이스

  • bxm.container.ServiceExecutorInterceptor의 메소드

void beforeExecution(ContextHeader callerHeader, ContextHeader calleeHeader,
        ServiceLinkParameter linkParam, ControlParameters calleeControlParameter,
        Context calleeTraceContext);

void afterExecution(ContextHeader callerHeader, ContextHeader calleeHeader, ServiceLinkParameter linkParam);

3.1. beforeExecution

Callee서비스를 호출하기전 수행할 작업을 구현한다. Callee서비스를 호출할 수 있는 상태인지 점검하거나 Caller, Callee의 시스템헤더, DataContainer의 값을 설정한다.

beforeExecution 메소드 파라미터
입력/출력구분 파라미터 설명

입력파라미터

ContextHeader callerHeader

Caller 시스템헤더

ContextHeader calleeHeader

Callee 시스템헤더

ServiceLinkParameter linkParam

서비스연동파라미터 객체
(동기/비동기여부,새로운 트랜잭션 시작여부, 거래코드)정보를 담고 있다.

ControlParameters calleeControlParameter

Callee 거래파라미터

Context calleeTraceContext

Callee Context객체

반환값

void

3.2. afterExecution

Callee서비스를 호출한 후 수행할 작업을 구현한다. 연동후 반영할 값을 Caller의 시스템헤더, DataContainer에 설정한다.

afterExecution 메소드 파라미터
입력/출력구분 파라미터 설명

입력파라미터

ContextHeader callerHeader

Caller 시스템헤더

ContextHeader calleeHeader

Callee 시스템헤더

ServiceLinkParameter linkParam

서비스연동파라미터 객체
(동기/비동기여부,새로운 트랜잭션 시작여부, 거래코드)정보를 담고 있다.

반환값

void

Copyright© Bankwareglobal All Rights Reserved.