From 1d2aafe0cd6607a48d880c007881768079e49060 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Mon, 9 Dec 2019 10:25:02 +0100 Subject: Refactor & Fixes - Handle InterruptedExceptions as discussed in https://stackoverflow.com/questions/3976344/handling-interruptedexception-in-java - Remove unused imports. --- .../java/at/gv/egiz/moazs/service/MsgService.java | 27 +++++++++++++--------- .../java/at/gv/egiz/moazs/service/MzsService.java | 10 ++++++-- 2 files changed, 24 insertions(+), 13 deletions(-) (limited to 'src/main/java/at/gv/egiz/moazs/service') diff --git a/src/main/java/at/gv/egiz/moazs/service/MsgService.java b/src/main/java/at/gv/egiz/moazs/service/MsgService.java index 6fce6ae..3de7b38 100644 --- a/src/main/java/at/gv/egiz/moazs/service/MsgService.java +++ b/src/main/java/at/gv/egiz/moazs/service/MsgService.java @@ -43,6 +43,7 @@ import static at.gv.zustellung.msg.xsd.DeliveryNotificationACKType.deliveryNotif import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusACKType.deliveryRequestStatusACKTypeBuilder; import static at.gv.zustellung.msg.xsd.GetVersionResponse.getVersionResponseBuilder; import static java.lang.String.format; +import static java.lang.Thread.currentThread; import static java.util.concurrent.CompletableFuture.runAsync; /** @@ -81,10 +82,11 @@ public class MsgService implements Zuse2AppPort { try { sendToBackend(response).get(); return statusAck(response.getAppDeliveryID(), response.getZSDeliveryID()); - } catch (InterruptedException | ExecutionException e) { - var message = format(BACKEND_ERROR_MSG, "request status"); - log.error(message, e); - throw toFault(e, message); + } catch (ExecutionException e) { + throw logAndCreateFault("delivery request status", e); + } catch (InterruptedException e) { + currentThread().interrupt(); + throw logAndCreateFault("delivery request status", e); } } @@ -97,21 +99,24 @@ public class MsgService implements Zuse2AppPort { } @Override - public DeliveryNotificationACKType notification(DeliveryNotificationType notification) { + public DeliveryNotificationACKType notification(DeliveryNotificationType notification){ var response = new NotificationResponse(notification); try { sendToBackend(response).get(); return notificationAck(response.getAppDeliveryID(), response.getZSDeliveryID()); - } catch (InterruptedException | ExecutionException e) { - var message = format(BACKEND_ERROR_MSG, "delivery notification"); - log.error(message, e); - throw toFault(e, message); + } catch (ExecutionException e) { + throw logAndCreateFault("delivery notification", e); + } catch (InterruptedException e) { + currentThread().interrupt(); + throw logAndCreateFault("delivery notification", e); } } - private RuntimeException toFault(Exception e, String fallbackMessage) { + private RuntimeException logAndCreateFault(String responseType, Exception e) { + var message = format(BACKEND_ERROR_MSG, responseType); + log.error(message, e); Throwable cause = (e.getCause() != null) ? e.getCause() : e; - return new SoapFault(fallbackMessage, cause, new QName("faultcode")); + return new SoapFault(message, cause, new QName("faultcode")); } private CompletableFuture sendToBackend(MsgResponse response) { diff --git a/src/main/java/at/gv/egiz/moazs/service/MzsService.java b/src/main/java/at/gv/egiz/moazs/service/MzsService.java index caaa251..565e374 100644 --- a/src/main/java/at/gv/egiz/moazs/service/MzsService.java +++ b/src/main/java/at/gv/egiz/moazs/service/MzsService.java @@ -39,6 +39,7 @@ import org.springframework.stereotype.Service; import javax.jws.WebParam; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.function.Consumer; @@ -46,6 +47,7 @@ import java.util.function.Consumer; import static at.gv.egiz.moazs.MoaZSException.moaZSException; import static at.gv.zustellung.app2mzs.xsd.PartialSuccessType.partialSuccessTypeBuilder; import static java.lang.String.format; +import static java.lang.Thread.currentThread; import static java.util.concurrent.CompletableFuture.supplyAsync; /** @@ -116,11 +118,15 @@ public class MzsService implements App2MzsPortType { } catch (TimeoutException e) { return applySinkHubToAsyncResponse(appDeliveryID, completeRequest, requestProcessed); - } catch (Exception e) { + } catch (InterruptedException e) { + currentThread().interrupt(); var message = format(MZS_SERVICE_ERROR_MSG, appDeliveryID); throw moaZSException(message, e); - } + } catch (ExecutionException e) { + var message = format(MZS_SERVICE_ERROR_MSG, appDeliveryID); + throw moaZSException(message, e); + } } private RequestStatusResponse process(DeliveryRequestType deliveryRequest) { -- cgit v1.2.3