From a2d4c9392221e511540e0e90a67808ebc89f29d6 Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Wed, 6 Mar 2019 16:12:04 -0500 Subject: [PATCH] Subscription matcher transactions (#1229) * fix bug in ResourceIndexSearchParamQuantity Move transaction boundary around SubscriptionMatcherInterceptor so it can be overridden in subclass Lots of CRLF changes from a mvn -P DIST * FIXME --- .../ca/uhn/fhir/jpa/dao/SearchBuilder.java | 4 +- .../SubscriptionMatcherInterceptor.java | 50 +++++++++---------- ...=> InMemorySubscriptionMatcherR4Test.java} | 5 +- .../ResourceIndexedSearchParamQuantity.java | 5 +- .../executor/InterceptorService.java | 2 +- ...=> InMemorySubscriptionMatcherR3Test.java} | 2 +- 6 files changed, 33 insertions(+), 35 deletions(-) rename hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/module/matcher/{InMemorySubscriptionMatcherTestR4.java => InMemorySubscriptionMatcherR4Test.java} (99%) rename hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/matcher/{InMemorySubscriptionMatcherTestR3.java => InMemorySubscriptionMatcherR3Test.java} (99%) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java index 45e19e8bf2c..f7ec48aa22b 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java @@ -9,9 +9,9 @@ package ca.uhn.fhir.jpa.dao; * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/subscription/SubscriptionMatcherInterceptor.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/subscription/SubscriptionMatcherInterceptor.java index 16cad396812..894e20aaf2d 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/subscription/SubscriptionMatcherInterceptor.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/subscription/SubscriptionMatcherInterceptor.java @@ -105,31 +105,15 @@ public class SubscriptionMatcherInterceptor implements IResourceModifiedConsumer private void submitResourceModified(IBaseResource theNewResource, ResourceModifiedMessage.OperationTypeEnum theOperationType) { ResourceModifiedMessage msg = new ResourceModifiedMessage(myFhirContext, theNewResource, theOperationType); - - /* - * We only want to submit the message to the processing queue once the - * transaction is committed. We do this in order to make sure that the - * data is actually in the DB, in case it's the database matcher. - */ - if (TransactionSynchronizationManager.isSynchronizationActive()) { - TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { - @Override - public int getOrder() { - return 0; - } - - @Override - public void afterCommit() { - submitResourceModified(msg); - } - }); - } else { - submitResourceModified(msg); + // Interceptor call: SUBSCRIPTION_RESOURCE_MODIFIED + if (!myInterceptorBroadcaster.callHooks(Pointcut.SUBSCRIPTION_RESOURCE_MODIFIED, msg)) { + return; } + submitResourceModified(msg); } - private void sendToProcessingChannel(final ResourceModifiedMessage theMessage) { + protected void sendToProcessingChannel(final ResourceModifiedMessage theMessage) { ourLog.trace("Sending resource modified message to processing channel"); Validate.notNull(myProcessingChannel, "A SubscriptionMatcherInterceptor has been registered without calling start() on it."); myProcessingChannel.send(new ResourceModifiedJsonMessage(theMessage)); @@ -144,12 +128,26 @@ public class SubscriptionMatcherInterceptor implements IResourceModifiedConsumer */ @Override public void submitResourceModified(final ResourceModifiedMessage theMsg) { - // Interceptor call: SUBSCRIPTION_RESOURCE_MODIFIED - if (!myInterceptorBroadcaster.callHooks(Pointcut.SUBSCRIPTION_RESOURCE_MODIFIED, theMsg)) { - return; - } + /* + * We only want to submit the message to the processing queue once the + * transaction is committed. We do this in order to make sure that the + * data is actually in the DB, in case it's the database matcher. + */ + if (TransactionSynchronizationManager.isSynchronizationActive()) { + TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { + @Override + public int getOrder() { + return 0; + } - sendToProcessingChannel(theMsg); + @Override + public void afterCommit() { + sendToProcessingChannel(theMsg); + } + }); + } else { + sendToProcessingChannel(theMsg); + } } @VisibleForTesting diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/module/matcher/InMemorySubscriptionMatcherTestR4.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/module/matcher/InMemorySubscriptionMatcherR4Test.java similarity index 99% rename from hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/module/matcher/InMemorySubscriptionMatcherTestR4.java rename to hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/module/matcher/InMemorySubscriptionMatcherR4Test.java index 290f2300a9b..5cf57f91adb 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/module/matcher/InMemorySubscriptionMatcherTestR4.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/module/matcher/InMemorySubscriptionMatcherR4Test.java @@ -8,7 +8,6 @@ import ca.uhn.fhir.jpa.subscription.module.CanonicalSubscription; import ca.uhn.fhir.jpa.subscription.module.ResourceModifiedMessage; import ca.uhn.fhir.model.api.TemporalPrecisionEnum; import ca.uhn.fhir.rest.param.*; -import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import org.apache.commons.lang3.StringUtils; import org.hl7.fhir.instance.model.api.IAnyResource; import org.hl7.fhir.instance.model.api.IBaseResource; @@ -29,8 +28,8 @@ import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {TestR4Config.class}) -public class InMemorySubscriptionMatcherTestR4 { - private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(InMemorySubscriptionMatcherTestR4.class); +public class InMemorySubscriptionMatcherR4Test { + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(InMemorySubscriptionMatcherR4Test.class); @Autowired InMemorySubscriptionMatcher myInMemorySubscriptionMatcher; diff --git a/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamQuantity.java b/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamQuantity.java index a21febc17ad..ca340e710e9 100644 --- a/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamQuantity.java +++ b/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamQuantity.java @@ -36,6 +36,7 @@ import java.math.BigDecimal; import java.util.Objects; import static org.apache.commons.lang3.StringUtils.defaultString; +import static org.apache.commons.lang3.StringUtils.isBlank; //@formatter:off @Embeddable @@ -246,7 +247,7 @@ public class ResourceIndexedSearchParamQuantity extends BaseResourceIndexedSearc // Only match on system if it wasn't specified String quantityUnitsString = defaultString(quantity.getUnits()); - if (quantity.getSystem() == null && quantityUnitsString == null) { + if (quantity.getSystem() == null && isBlank(quantityUnitsString)) { if (Objects.equals(getValue(),quantity.getValue())) { retval = true; } @@ -257,7 +258,7 @@ public class ResourceIndexedSearchParamQuantity extends BaseResourceIndexedSearc Objects.equals(getValue(),quantity.getValue())) { retval = true; } - } else if (quantityUnitsString == null) { + } else if (isBlank(quantityUnitsString)) { if (getSystem().equalsIgnoreCase(quantity.getSystem()) && Objects.equals(getValue(),quantity.getValue())) { retval = true; diff --git a/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/interceptor/executor/InterceptorService.java b/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/interceptor/executor/InterceptorService.java index d62f0a25417..0df3ef841de 100644 --- a/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/interceptor/executor/InterceptorService.java +++ b/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/interceptor/executor/InterceptorService.java @@ -131,7 +131,7 @@ public class InterceptorService implements IInterceptorRegistry, IInterceptorBro */ private List scanInterceptorForHookMethods(Object theInterceptor, int theTypeOrder) { ArrayList retVal = new ArrayList<>(); - for (Method nextMethod : theInterceptor.getClass().getDeclaredMethods()) { + for (Method nextMethod : theInterceptor.getClass().getMethods()) { Hook hook = AnnotationUtils.findAnnotation(nextMethod, Hook.class); if (hook != null) { diff --git a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/matcher/InMemorySubscriptionMatcherTestR3.java b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/matcher/InMemorySubscriptionMatcherR3Test.java similarity index 99% rename from hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/matcher/InMemorySubscriptionMatcherTestR3.java rename to hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/matcher/InMemorySubscriptionMatcherR3Test.java index 81ab63e9c89..16f9637b51e 100644 --- a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/matcher/InMemorySubscriptionMatcherTestR3.java +++ b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/matcher/InMemorySubscriptionMatcherR3Test.java @@ -14,7 +14,7 @@ import java.util.Collections; import static org.junit.Assert.*; -public class InMemorySubscriptionMatcherTestR3 extends BaseSubscriptionDstu3Test { +public class InMemorySubscriptionMatcherR3Test extends BaseSubscriptionDstu3Test { @Autowired SubscriptionStrategyEvaluator mySubscriptionStrategyEvaluator; @Autowired