001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.model.config;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023import javax.xml.bind.annotation.XmlTransient;
024
025import org.apache.camel.processor.resequencer.DefaultExchangeComparator;
026import org.apache.camel.processor.resequencer.ExpressionResultComparator;
027import org.apache.camel.spi.Metadata;
028
029/**
030 * Configures stream-processing resequence eip.
031 */
032@Metadata(label = "eip,routing,resequence")
033@XmlRootElement(name = "stream-config")
034@XmlAccessorType(XmlAccessType.FIELD)
035public class StreamResequencerConfig extends ResequencerConfig {
036    @XmlAttribute @Metadata(defaultValue = "100")
037    private Integer capacity;
038    @XmlAttribute @Metadata(defaultValue = "1000")
039    private Long timeout;
040    @XmlAttribute @Metadata(defaultValue = "1000")
041    private Long deliveryAttemptInterval;
042    @XmlAttribute
043    private Boolean ignoreInvalidExchanges;
044    @XmlTransient
045    private ExpressionResultComparator comparator;
046    @XmlAttribute
047    private String comparatorRef;
048    @XmlAttribute
049    private Boolean rejectOld;
050
051    /**
052     * Creates a new {@link StreamResequencerConfig} instance using default
053     * values for <code>capacity</code> (1000) and <code>timeout</code>
054     * (1000L). Elements of the sequence are compared using the
055     * {@link DefaultExchangeComparator}.
056     */
057    public StreamResequencerConfig() {
058        this(1000, 1000L);
059    }
060
061    /**
062     * Creates a new {@link StreamResequencerConfig} instance using the given
063     * values for <code>capacity</code> and <code>timeout</code>. Elements
064     * of the sequence are compared using the {@link DefaultExchangeComparator}.
065     * 
066     * @param capacity   capacity of the resequencer's inbound queue.
067     * @param timeout    minimum time to wait for missing elements (messages).
068     */
069    public StreamResequencerConfig(int capacity, long timeout) {
070        this(capacity, timeout, new DefaultExchangeComparator());
071    }
072
073    /**
074     * Creates a new {@link StreamResequencerConfig} instance using the given
075     * values for <code>capacity</code> and <code>timeout</code>. Elements
076     * of the sequence are compared with the given
077     * {@link ExpressionResultComparator}.
078     * 
079     * @param capacity   capacity of the resequencer's inbound queue.
080     * @param timeout    minimum time to wait for missing elements (messages).
081     * @param comparator comparator for sequence comparision
082     */
083    public StreamResequencerConfig(int capacity, long timeout, ExpressionResultComparator comparator) {
084        this.capacity = capacity;
085        this.timeout = timeout;
086        this.comparator = comparator;
087    }
088
089    /**
090     * Creates a new {@link StreamResequencerConfig} instance using the given
091     * values for <code>capacity</code> and <code>timeout</code>. Elements
092     * of the sequence are compared using the {@link DefaultExchangeComparator}.
093     *
094     * @param capacity   capacity of the resequencer's inbound queue.
095     * @param timeout    minimum time to wait for missing elements (messages).
096     * @param rejectOld  if true, throws an exception when messages older than the last delivered message are processed
097     */
098    public StreamResequencerConfig(int capacity, long timeout, Boolean rejectOld) {
099        this(capacity, timeout, rejectOld, new DefaultExchangeComparator());
100    }
101
102    /**
103     * Creates a new {@link StreamResequencerConfig} instance using the given
104     * values for <code>capacity</code> and <code>timeout</code>. Elements
105     * of the sequence are compared with the given {@link ExpressionResultComparator}.
106     *
107     * @param capacity   capacity of the resequencer's inbound queue.
108     * @param timeout    minimum time to wait for missing elements (messages).
109     * @param rejectOld  if true, throws an exception when messages older than the last delivered message are processed
110     * @param comparator comparator for sequence comparision
111     */
112    public StreamResequencerConfig(int capacity, long timeout, Boolean rejectOld, ExpressionResultComparator comparator) {
113        this.capacity = capacity;
114        this.timeout = timeout;
115        this.rejectOld = rejectOld;
116        this.comparator = comparator;
117    }
118
119    /**
120     * Returns a new {@link StreamResequencerConfig} instance using default
121     * values for <code>capacity</code> (1000) and <code>timeout</code>
122     * (1000L). Elements of the sequence are compared using the
123     * {@link DefaultExchangeComparator}.
124     * 
125     * @return a default {@link StreamResequencerConfig}.
126     */
127    public static StreamResequencerConfig getDefault() {
128        return new StreamResequencerConfig();
129    }
130    
131    public int getCapacity() {
132        return capacity;
133    }
134
135    /**
136     * Sets the capacity of the resequencer's inbound queue.
137     */
138    public void setCapacity(int capacity) {
139        this.capacity = capacity;
140    }
141
142    public long getTimeout() {
143        return timeout;
144    }
145
146    /**
147     * Sets minimum time to wait for missing elements (messages).
148     */
149    public void setTimeout(long timeout) {
150        this.timeout = timeout;
151    }
152
153    public Long getDeliveryAttemptInterval() {
154        return deliveryAttemptInterval;
155    }
156
157    /**
158     * Sets the interval in milli seconds the stream resequencer will at most wait
159     * while waiting for condition of being able to deliver.
160     */
161    public void setDeliveryAttemptInterval(Long deliveryAttemptInterval) {
162        this.deliveryAttemptInterval = deliveryAttemptInterval;
163    }
164
165    public Boolean getIgnoreInvalidExchanges() {
166        return ignoreInvalidExchanges;
167    }
168
169    /**
170     * Whether to ignore invalid exchanges
171     */
172    public void setIgnoreInvalidExchanges(Boolean ignoreInvalidExchanges) {
173        this.ignoreInvalidExchanges = ignoreInvalidExchanges;
174    }
175
176    public ExpressionResultComparator getComparator() {
177        return comparator;
178    }
179
180    /**
181     * To use a custom comparator
182     */
183    public void setComparator(ExpressionResultComparator comparator) {
184        this.comparator = comparator;
185    }
186
187    public String getComparatorRef() {
188        return comparatorRef;
189    }
190
191    /**
192     * To use a custom comparator
193     */
194    public void setComparatorRef(String comparatorRef) {
195        this.comparatorRef = comparatorRef;
196    }
197
198    /**
199     * If true, throws an exception when messages older than the last delivered message are processed
200     */
201    public void setRejectOld(boolean value) {
202        this.rejectOld = value;
203    }
204
205    public Boolean getRejectOld() {
206        return rejectOld;
207    }
208
209}