001/* 002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bdelbosc 018 */ 019 020package org.nuxeo.importer.stream.producer; 021 022import java.time.Duration; 023 024import org.nuxeo.ecm.core.blob.BlobInfo; 025import org.nuxeo.importer.stream.message.BlobInfoMessage; 026import org.nuxeo.importer.stream.message.DocumentMessage; 027import org.nuxeo.lib.stream.log.LogRecord; 028import org.nuxeo.lib.stream.log.LogTailer; 029 030/** 031 * Returns blob information from a Log, loop on the log. 032 * 033 * @since 9.3 034 */ 035public class RandomLogBlobInfoFetcher implements BlobInfoFetcher { 036 protected static final int READ_DELAY_MS = 100; 037 038 protected final LogTailer<BlobInfoMessage> tailer; 039 040 protected boolean first; 041 042 public RandomLogBlobInfoFetcher(LogTailer<BlobInfoMessage> blobInfoTailer) { 043 this.tailer = blobInfoTailer; 044 this.first = true; 045 } 046 047 @Override 048 public BlobInfo get(DocumentMessage.Builder builder) { 049 LogRecord<BlobInfoMessage> record; 050 try { 051 record = tailer.read(Duration.ofMillis(READ_DELAY_MS)); 052 } catch (InterruptedException e) { 053 Thread.currentThread().interrupt(); 054 throw new RuntimeException(e); 055 } 056 if (record == null) { 057 if (first) { 058 // there is no record in this partition, no need to loop 059 return null; 060 } 061 // start again from beginning 062 tailer.toStart(); 063 return get(builder); 064 } 065 first = false; 066 return record.message(); 067 } 068 069 @Override 070 public void close() { 071 tailer.close(); 072 } 073}