001package ca.uhn.fhir.jpa.subscription.async; 002 003/*- 004 * #%L 005 * HAPI FHIR Subscription Server 006 * %% 007 * Copyright (C) 2014 - 2024 Smile CDR, Inc. 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import ca.uhn.fhir.jpa.model.entity.IPersistedResourceModifiedMessage; 024import ca.uhn.fhir.subscription.api.IResourceModifiedConsumerWithRetries; 025import ca.uhn.fhir.subscription.api.IResourceModifiedMessagePersistenceSvc; 026import com.google.common.annotations.VisibleForTesting; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029import org.springframework.data.domain.Page; 030import org.springframework.data.domain.PageRequest; 031 032/** 033 * The purpose of this service is to submit messages to the processing pipeline for which previous attempts at 034 * submission has failed. See also {@link AsyncResourceModifiedProcessingSchedulerSvc} and {@link IResourceModifiedMessagePersistenceSvc}. 035 * 036 */ 037public class AsyncResourceModifiedSubmitterSvc { 038 private static final Logger ourLog = LoggerFactory.getLogger(AsyncResourceModifiedSubmitterSvc.class); 039 040 public static final int MAX_LIMIT = 1000; 041 042 private final IResourceModifiedMessagePersistenceSvc myResourceModifiedMessagePersistenceSvc; 043 private final IResourceModifiedConsumerWithRetries myResourceModifiedConsumer; 044 045 public AsyncResourceModifiedSubmitterSvc( 046 IResourceModifiedMessagePersistenceSvc theResourceModifiedMessagePersistenceSvc, 047 IResourceModifiedConsumerWithRetries theResourceModifiedConsumer) { 048 myResourceModifiedMessagePersistenceSvc = theResourceModifiedMessagePersistenceSvc; 049 myResourceModifiedConsumer = theResourceModifiedConsumer; 050 } 051 052 public void runDeliveryPass() { 053 boolean hasMoreToFetch = false; 054 int limit = getLimit(); 055 do { 056 // we always take the 0th page, because we're deleting the elements as we process them 057 Page<IPersistedResourceModifiedMessage> persistedResourceModifiedMsgsPage = 058 myResourceModifiedMessagePersistenceSvc.findAllOrderedByCreatedTime(PageRequest.of(0, limit)); 059 ourLog.debug( 060 "Attempting to submit {} resources to consumer channel.", 061 persistedResourceModifiedMsgsPage.getTotalElements()); 062 063 hasMoreToFetch = persistedResourceModifiedMsgsPage.hasNext(); 064 065 for (IPersistedResourceModifiedMessage persistedResourceModifiedMessage : 066 persistedResourceModifiedMsgsPage) { 067 boolean wasProcessed = myResourceModifiedConsumer.submitPersisedResourceModifiedMessage( 068 persistedResourceModifiedMessage); 069 070 if (!wasProcessed) { 071 // we're not fetching anymore no matter what 072 hasMoreToFetch = false; 073 break; 074 } 075 } 076 } while (hasMoreToFetch); 077 } 078 079 @VisibleForTesting 080 public static int getLimit() { 081 return MAX_LIMIT; 082 } 083}