我在Java和Spring上使用akka。我的主要目标是以异步方式通知其他应用程序。但有时此其他应用未收到通知。因此,我实施了主管以重试。但是,当我收到异常(特别是InternalServerErrorException)时,我的主管不会再次尝试。似乎RestTemplate和您对异常的处理导致了此问题。 下面是我的代码:
@Scope(SCOPE_PROTOTYPE)
public class NotificacaoSupervisor extends AbstractActor {
private static final int RETRIES = 5;
private OneForOneStrategy ONE_FOR_ONE_STRATEGY = new OneForOneStrategy(
RETRIES,
Duration.create("5 minutes"),
true,
DeciderBuilder.match(NotificacaoException.class, ex -> SupervisorStrategy.restart())
.build());
@Inject @Qualifier("notifyActor")
private ActorRef notifyActor;
@Override
public Receive createReceive() {
return receiveBuilder()
.matchAny(any -> notifyActor.forward(any, getContext()))
.build();
}
@Override
public SupervisorStrategy supervisorStrategy() {
return ONE_FOR_ONE_STRATEGY;
}
}
发送通知的块
try{
if(content.isPresent()) {
this.logInfo(content.get());
HttpEntity<String> entity = new HttpEntity<>(content.get(), headers);
String urlDeCallback = integracao.getUrlDeCallback();
URI uri = URI.create(urlDeCallback);
restTemplate.postForObject(uri, entity, Void.class);
}
} catch (Exception e) {
this.logError(new ErrorResponse(notificacao).toString(), e);
throw new NotificacaoException(e);
}