如何引发Akka演员的异常?

时间:2019-02-06 06:43:15

标签: java spring akka

我有一个Spring Boot应用程序,正在尝试与akka集成。对于大多数端点,我依赖于内置于RuntimeException机制的spring boot,但是akka actor不允许抛出任何异常想法吗?

我有这个帮助其他服务的助手:

public ResponseEntity<R> callService(String address
        , MultiValueMap<String, String> headers
        , B body
        , HttpMethod httpMethod
        , ParameterizedTypeReference<R> parameterizedTypeReference
        , ExceptionHandlerCustom exceptionHandlerCustom) {
    try {
        HttpEntity<B> request = new HttpEntity<>(body, headers);
        return restTemplate.exchange(address
                , httpMethod
                , request
                , parameterizedTypeReference);
    } catch (Exception e) {
        if (e instanceof HttpStatusCodeException) {
            HttpStatusCodeException exception = (HttpStatusCodeException) e;
            Gson gson = new Gson();
            Response<String> errorResponse =
                    gson.fromJson(exception.getResponseBodyAsString(), new TypeToken<Response<String>>(){}.getType());
            if (exception.getStatusCode().equals(HttpStatus.BAD_REQUEST)) {
                throw new BadRequestException(errorResponse.getMessage());
            } else if (exception.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new UnauthorizedException("not authorized");
            } else if (exception.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR)) {
                e.printStackTrace();
                throw new InternalServerErrorException();
            } else if (exceptionHandlerCustom != null) {
                exceptionHandlerCustom.handle(e);
            }else
                e.printStackTrace();
        }
        throw e;
    }
}

我有一个演员调用此方法,我想在异常发生时重新抛出异常(我知道演员的生命周期是不会破坏的)

我的例外都是这样:

@ResponseStatus(HttpStatus.NOT_FOUND)
public class NotFoundException extends RuntimeException {
    public NotFoundException(String message) {
        super(message);
    }
}

在演员中我有这种方法:

@Override
    public Receive createReceive() {
    return receiveBuilder()
            .match(CheckUserIdAndProvidedToken.class, message -> {
                getSender().tell(tokenCheckService.checkUserIdAndProvidedToken(message.getToken()
                        , message.getId()), getSelf());
            })
            .build();
}

在服务中,我这样称呼它:

Future<Object> futureOfCheck = ask(checker, new         
TokenCheckActor.CheckUserIdAndProvidedToken(id, token), timeout);

当发生异常时,我想将其扔给客户端(json格式的spring boot异常):

{
    "timestamp": "2019-02-06T06:42:26.740+0000",
    "status": 404,
    "error": "Not Found",
    "message": "user not found",
    "path": "/xxxx/yyy/ssss"
}

1 个答案:

答案 0 :(得分:2)

可以通过询问模式传递Exceptions。这在akka docs的ask-send-and-receive-future部分中有介绍。

总而言之,在receive方法中捕获异常,用Failure将其包装并发送给发件人。

try {
  String result = operation();
  getSender().tell(result, getSelf());
} catch (Exception e) {
  getSender().tell(new akka.actor.Status.Failure(e), getSelf());
  throw e;
}