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