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 = 10; 037 038 protected final LogTailer<BlobInfoMessage> tailer; 039 040 public RandomLogBlobInfoFetcher(LogTailer<BlobInfoMessage> blobInfoTailer) { 041 this.tailer = blobInfoTailer; 042 } 043 044 @Override 045 public BlobInfo get(DocumentMessage.Builder builder) { 046 LogRecord<BlobInfoMessage> record; 047 try { 048 record = tailer.read(Duration.ofMillis(READ_DELAY_MS)); 049 } catch (InterruptedException e) { 050 Thread.currentThread().interrupt(); 051 throw new RuntimeException(e); 052 } 053 if (record == null) { 054 // start again from beginning 055 tailer.toStart(); 056 return get(builder); 057 } 058 return record.message(); 059 } 060 061 @Override 062 public void close() throws Exception { 063 tailer.close(); 064 } 065}