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 */ 019package org.nuxeo.ecm.platform.importer.mqueues.pattern.consumer; 020 021import java.time.Duration; 022 023/** 024 * Describe when a batch must be flushed. 025 * 026 * @since 9.1 027 */ 028public class BatchPolicy { 029 public static final BatchPolicy NO_BATCH = builder().capacity(1).build(); 030 public static final BatchPolicy DEFAULT = builder().build(); 031 032 private final int capacity; 033 private final Duration threshold; 034 035 public BatchPolicy(Builder builder) { 036 capacity = builder.capacity; 037 threshold = builder.threshold; 038 } 039 040 public int getCapacity() { 041 return capacity; 042 } 043 044 public Duration getTimeThreshold() { 045 return threshold; 046 } 047 048 public static Builder builder() { 049 return new Builder(); 050 } 051 052 public static class Builder { 053 private int capacity = 10; 054 private Duration threshold = Duration.ofSeconds(10); 055 056 protected Builder() { 057 058 } 059 060 /** 061 * Set the capacity of the batch. 062 */ 063 public Builder capacity(int capacity) { 064 this.capacity = capacity; 065 return this; 066 } 067 068 /** 069 * Set the time threshold to fill a batch. 070 */ 071 public Builder timeThreshold(Duration threshold) { 072 this.threshold = threshold; 073 return this; 074 } 075 076 public BatchPolicy build() { 077 return new BatchPolicy(this); 078 } 079 080 } 081 082 @Override 083 public String toString() { 084 return "BatchPolicy{" + 085 "capacity=" + capacity + 086 ", threshold=" + threshold + 087 '}'; 088 } 089}