Я пытаюсь добавить Hystrix в мое приложение, которое прослушивает (@SqsListener) очередь (Amazon SQS) и выполняет операцию с сообщением.
Код работает для RestController, но я не могуполучить то же поведение для моего слушателя.
@EnableCircuitBreaker
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@PreDestroy
public void hystrixCleanup() {
Hystrix.reset();
}
}
@Slf4j
@Component
@RequiredArgsConstructor
public class QueueListener {
private final SampleService sampleService;
@SqsListener(value = "sample-queue", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
public void receiveMsg(String payload, Acknowledgment acknowledgment) {
SampleResponse sampleResponse = sampleService.getSampleResponse(payload);
log.info(sampleResponse.getMessage());
acknowledgment.acknowledge();
}
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class SampleResponse {
private String message;
}
@Service
@DefaultProperties(raiseHystrixExceptions = {RUNTIME_EXCEPTION})
public class SampleService {
@HystrixCommand(commandKey = "getSampleResponse")
public SampleResponse getSampleResponse(String value) {
if ("123".equals(value)) {
throw new SampleException("123");
}
return new SampleResponse(value);
}
}
@Slf4j
@ControllerAdvice
public class SampleExceptionHandler {
@ExceptionHandler({ HystrixRuntimeException.class })
// public ResponseEntity<SampleResponse> handleHystrixRuntimeException(HystrixRuntimeException exception) {
public void handleHystrixRuntimeException(HystrixRuntimeException exception) {
String newMsg;
if (exception.getFailureType() == TIMEOUT) {
newMsg = "Timeout Error: ".concat(exception.getMessage());
} else {
if (exception.getCause() instanceof SampleException) {
newMsg = "Sample Exception: ".concat(exception.getCause().getMessage());
} else {
newMsg = "Server Error: ".concat(exception.getMessage());
}
}
log.error("Hystrix Exception: {}", newMsg);
// return new ResponseEntity<>(new SampleResponse(newMsg), HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler({ SampleException.class })
public void handleSampleException(SampleException exception) {
log.error("Sample Exception: {}", exception.getMessage());
}
}
Пример кода можно найти здесь: https://github.com/Niranjan-K/spring-samples/tree/sqs-hystrix
Когда выполняется вызов Get для контроллера (http://localhost:8080/hello/123), itпопадает в ExceptionHandler.class, как и ожидалось, но этого не происходит, когда в очередь публикуется сообщение «123». Класс Controller и Listener вызывает один и тот же класс обслуживания (и один и тот же метод).
Ниже приведеножурнал при вызове покоя:
2019-06-11 14: 46: 57.189 ОШИБКА 55399 --- [nio-8080-exec-1] onsshhSampleExceptionHandler: Исключение Hystrix: Исключение образца: 123
В случае SqsListener:
2019-06-11 14: 47: 35.117 ОШИБКА 55399 --- [enerContainer-2] oscamlistener.QueueMessageHandler: необработанное исключение изметод обработки сообщений
com.netflix.hystrix.exception.HystrixRuntimeException: getSampleResponse завершился ошибкой и произошел сбой.на com.netflix.hystrix.AbstractCommand $ 22.call (AbstractCommand.java:832) ~ [hystrix-core-1.5.18.jar! /: 1.5.18] на com.netflix.hystrix.AbstractCommand $ 22.call (AbstractCommand.Java: 807) ~ [hystrix-core-1.5.18.jar! /: 1.5.18] в rx.internal.operators.OperatorOnErrorResumeNextViaFunction $ 4.onError (OperatorOnErrorResumeNextViaFunction.java:140) ~ [rxjava-1.3.8./:1.3.8] в rx.internal.operators.OnSubscribeDoOnEach $ DoOnEachSubscriber.onError (OnSubscribeDoOnEach.java:87) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OnSubscribeOnEDoOnEachSubscriber.onError (OnSubscribeDoOnEach.java:87) ~ [rxjava-1.3.8.jar! /: 1.3.8] в com.netflix.hystrix.AbstractCommand $ DeprecatedOnFallbackHookApplication $ 1.onError (AbstractCommand.java:14:14:14)core-1.5.18.jar! /: 1.5.18] на com.netflix.hystrix.AbstractCommand $ FallbackHookApplication $ 1.onError (AbstractCommand.java:1397) ~ [hystrix-core-1.5.18.jar! /: 1.5.18] в rx.internal.operators.OnSubscribeDoOnEach $ DoOnEachSubscriber.onError (OnSubscribeDoOnEach.java: 87) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.observers.Subscribeers $ 5.onError (Subscribeers.java:230) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OnSubscribeThrow.call (OnSubscribeThrow.java:44) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OnSubscribeThrow.call (OnSubscribeThrow.java:28) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.Observable.unsafeSubscribe (Observable.java:10327) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OnSubscribeDefer.call (OnSubscribeDefer.java:51) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OnSubscribeDefer.call (OnSubscribeDefer.java:35) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.Observable.unsafeSubscribe (Observable.java:10327) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OnSubscribeDoOnEach.call (OnSubscribeDoOnEach.java:41) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OnSubscribeDoOnEach.call (OnSubscribeDoOnEach.java:30) ~ [rxjava-1.3.8.jar! /: 1.3.8] at rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:48) ~ [rxjava-1.3.8.jar!/:1.3.8] в rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:30) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:48) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:30) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.Observable.unsafeSubscribe (Observable.java:10327) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OnSubscribeDoOnEach.call (OnSubscribeDoOnEach.java:41) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OnSubscribeDoOnEach.call (OnSubscribeDoOnEach.java:30) ~ [rxjava-1.3.8.jar! /: 1.3.8]at rx.Observable.unsafeSubscribe (Observable.java:10327) ~ [rxjava-1.3.8.jar! /: 1.3.8]в rx.internal.operators.OnSubscribeDoOnEach.call (OnSubscribeDoOnEach.java:41) ~ [rxjava-1.3.8.jar! /: 1.3.8]
at rx.internal.operators.OnSubscribeDoOnEach.call (OnSubscribeDoOnEach.java:30) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:48) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:30) ~ [rxjava-1.3.8.jar! /: 1.3.8]
at rx.Observable.unsafeSubscribe (Observable.java:10327) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeDoOnEach.call (OnSubscribeDoOnEach.java:41) ~ [rxjava-1.3.8.jar! /: 1.3.8]
at rx.internal.operators.OnSubscribeDoOnEach.call (OnSubscribeDoOnEach.java:30) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:48) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:30) ~ [rxjava-1.3.8.jar! /: 1.3.8]
at rx.Observable.unsafeSubscribe (Observable.java:10327) ~ [rxjava-1.3.8.jar! /: 1.3.8]
at rx.internal.operators.OperatorOnErrorResumeNextViaFunction $ 4.onError (OperatorOnErrorResumeNextViaFunction.java:142) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeDoOnEach $ DoOnEachSubscriber.onError (OnSubscribeDoOnEach.java:87) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeDoOnEach $ DoOnEachSubscriber.onError (OnSubscribeDoOnEach.java:87) ~ [rxjava-1.3.8.jar! /: 1.3.8]
на com.netflix.hystrix.AbstractCommand $ HystrixObservableTimeoutOperator $ 3.onError (AbstractCommand.java:1194) ~ [hystrix-core-1.5.18.jar! /: 1.5.18]
at rx.internal.operators.OperatorSubscribeOn $ SubscribeOnSubscriber.onError (OperatorSubscribeOn.java:80) ~ [rxjava-1.3.8.jar! /: 1.3.8]
на rx.observers.Subscribeers $ 5.onError (Subscribeers.java:230) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeDoOnEach $ DoOnEachSubscriber.onError (OnSubscribeDoOnEach.java:87) ~ [rxjava-1.3.8.jar! /: 1.3.8]
на rx.observers.Subscribeers $ 5.onError (Subscribeers.java:230) ~ [rxjava-1.3.8.jar! /: 1.3.8]
at com.netflix.hystrix.AbstractCommand $ DeprecatedOnRunHookApplication $ 1.onError (AbstractCommand.java:1431) ~ [hystrix-core-1.5.18.jar! /: 1.5.18]
на com.netflix.hystrix.AbstractCommand $ ExecutionHookApplication $ 1.onError (AbstractCommand.java:1362) ~ [hystrix-core-1.5.18.jar! /: 1.5.18]
на rx.observers.Subscribeers $ 5.onError (Subscribeers.java:230) ~ [rxjava-1.3.8.jar! /: 1.3.8]
на rx.observers.Subscribeers $ 5.onError (Subscribeers.java:230) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeThrow.call (OnSubscribeThrow.java:44) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeThrow.call (OnSubscribeThrow.java:28) ~ [rxjava-1.3.8.jar! /: 1.3.8]
at rx.Observable.unsafeSubscribe (Observable.java:10327) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeDefer.call (OnSubscribeDefer.java:51) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeDefer.call (OnSubscribeDefer.java:35) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:48) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:30) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:48) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:30) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:48) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:30) ~ [rxjava-1.3.8.jar! /: 1.3.8]
at rx.Observable.unsafeSubscribe (Observable.java:10327) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeDefer.call (OnSubscribeDefer.java:51) ~ [rxjava-1.3.8.jar! /: 1.3.8]
в rx.internal.operators.OnSubscribeDefer.call (OnSubscribeDefer.java:35) ~ [rxjava-1.3.8.jar! /: 1.3.8]в rx.Observable.unsafeSubscribe (Observable.java:10327) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OnSubscribeDoOnEach.call (OnSubscribeDoOnEach.java:41) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OnSubscribeDoOnEach.call (OnSubscribeDoOnEach.java:30) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operator.OnSubscribeLift.call (OnSubscribeLift.java:48) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OnSubscribeLift.call (OnSubscribeLift.java:30) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.Observable.unsafeSubscribe (Observable.java:10327) ~ [rxjava-1.3.8.jar! /: 1.3.8] в rx.internal.operators.OperatorSubscribeOn $ SubscribeOnSubscriber.call (OperatorSubscribeOn.java:100) ~ [rxjava-1.3.8.jar! /: 1.3.8] в com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction $ 1.call (HystrixContexSchedulerAction.java:56) ~ [hystrix-core-1.5.18.jar! /: 1.5.18] на com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction $ 1.call (HystrixContexSchedulerAction.java:47) ~ [hystrix-core-1.5.18.jar! /: 1.5.18] at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction.call (HystrixContexSchedulerAction.java:69) ~ [hystrix-core-1.5.18.jar! /: 1.5.18]в rx.internal.schedulers.ScheduledAction.run (ScheduledAction.java:55) ~ [rxjava-1.3.8.jar! /: 1.3.8] в java.util.concurrent.Executors $ RunnableAdapter.call (Executors.java:511) ~ [na: 1.8.0_181] в java.util.concurrent.FutureTask.run (FutureTask.java:266) ~ [na: 1.8.0_181] в java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149) [na: 1.8.0_181] в java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:624) [na: 1.8.0_181] в java.lang.Thread.run (Thread.java:748)[na: 1.8.0_181] Причина: org.nira.sample.sqs.hystrix.exception.SampleException: 123 at org.nira.sample.sqs.hystrix.service.SampleService.getSampleResponse (SampleService.java:18) ~ [классы! /: 1.0-SNAPSHOT] в sun.reflect.NativeMethodAccessorImpl.invoke0 (собственный метод) ~ [na: 1.8.0_181] в sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) ~ [na: 1.8.0_181] в sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) ~ [na: 1.8.0_181] в java.lang.reflect.Method.java:498) ~ [na: 1.8.0_181] на com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.execute (MethodExecutionAction.java:116) ~ [hystrix-javanica-1.5.18.jar! /: 1.5.18] на com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.executeWithArgs (MethodExecutionAction.java:93) ~ [hystrix-javanica-1.5.18.jar! /: 1.5.18] на com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.execute (MethodExecutionAction.java:78) ~ [hystrix-javanica-1.5.18.jar! /: 1.5.18] на com.netflix.hystrix.contrib.javanica.command.GenericCommand $ 1.execute (GenericCommand.java:48) ~ [hystrix-javanica-1.5.18.jar! /: 1.5.18] на com.netflix.hystrix.contrib.javanica.command.AbstractHystrixCommand.process (AbstractHystrixCommand.java:145) ~ [hystrix-javanica-1.5.18.jar! /: 1.5.18] на com.netflix.hystrix.contrib.javanica.command.GenericCommand.run (GenericCommand.java:45) ~ [hystrix-javanica-1.5.18.jar! /: 1.5.18] на com.netflix.hystrix.HystrixCommand $ 2.call (HystrixCommand.java:302) ~ [hystrix-core-1.5.18.jar! /: 1.5.18] на com.netflix.hystrix.HystrixCommand $ 2.call (HystrixCommand.java:298) ~ [hystrix-core-1.5.18.jar!/:1.5.18] at rx.internal.operators.OnSubscribeDefer.call (OnSubscribeDefer.java:46) ~ [rxjava-1.3.8.jar! /: 1.3.8] ... пропущено 26 общих кадров