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 net.jodah.failsafe.RetryPolicy; 022 023import java.time.Duration; 024 025import static org.nuxeo.ecm.platform.importer.mqueues.pattern.consumer.ConsumerPolicy.NO_RETRY; 026 027/** 028 * @since 9.2 029 */ 030public class ConsumerPolicyBuilder { 031 protected BatchPolicy batchPolicy = BatchPolicy.DEFAULT; 032 protected RetryPolicy retryPolicy = NO_RETRY; 033 protected boolean skipFailure = false; 034 protected Duration waitMessageTimeout = Duration.ofSeconds(2); 035 protected ConsumerPolicy.StartOffset startOffset = ConsumerPolicy.StartOffset.LAST_COMMITTED; 036 protected boolean salted = false; 037 protected String name; 038 protected short maxThreads = 0; 039 040 protected ConsumerPolicyBuilder() { 041 042 } 043 044 public ConsumerPolicyBuilder batchPolicy(BatchPolicy policy) { 045 batchPolicy = policy; 046 return this; 047 } 048 049 public ConsumerPolicyBuilder retryPolicy(RetryPolicy policy) { 050 retryPolicy = policy; 051 return this; 052 } 053 054 /** 055 * Continue on next message even if the retry policy has failed. 056 */ 057 public ConsumerPolicyBuilder continueOnFailure(boolean value) { 058 skipFailure = value; 059 return this; 060 } 061 062 /** 063 * Maximum consumer threads to use. The number of threads is limited by the size of the MQueue. 064 */ 065 public ConsumerPolicyBuilder maxThreads(short maxThreads) { 066 this.maxThreads = maxThreads; 067 return this; 068 } 069 070 /** 071 * Consumer will stop if there is no more message after this timeout. 072 */ 073 public ConsumerPolicyBuilder waitMessageTimeout(Duration duration) { 074 waitMessageTimeout = duration; 075 return this; 076 } 077 078 /** 079 * Consumer will wait for ever message. 080 */ 081 public ConsumerPolicyBuilder waitMessageForEver() { 082 waitMessageTimeout = Duration.ofSeconds(Integer.MAX_VALUE); 083 return this; 084 } 085 086 /** 087 * Where to read the first message. 088 */ 089 public ConsumerPolicyBuilder startOffset(ConsumerPolicy.StartOffset startOffset) { 090 this.startOffset = startOffset; 091 return this; 092 } 093 094 /** 095 * Consumer will wait some random time before start, to prevent wave of concurrency in batch processing. 096 */ 097 public ConsumerPolicyBuilder salted() { 098 salted = true; 099 return this; 100 } 101 102 /** 103 * Consumer group name. 104 */ 105 public ConsumerPolicyBuilder name(String name) { 106 this.name = name; 107 return this; 108 } 109 110 public ConsumerPolicy build() { 111 return new ConsumerPolicy(this); 112 } 113 114}