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     */
017    package org.apache.camel.model.config;
018    
019    import javax.xml.bind.annotation.XmlAccessType;
020    import javax.xml.bind.annotation.XmlAccessorType;
021    import javax.xml.bind.annotation.XmlAttribute;
022    import javax.xml.bind.annotation.XmlRootElement;
023    import javax.xml.bind.annotation.XmlTransient;
024    
025    import org.apache.camel.Exchange;
026    import org.apache.camel.processor.StreamResequencer;
027    import org.apache.camel.processor.resequencer.DefaultExchangeComparator;
028    import org.apache.camel.processor.resequencer.ExpressionResultComparator;
029    
030    /**
031     * Defines the configuration parameters for the {@link StreamResequencer}.
032     * Usage example:
033     * 
034     * <pre>
035     * from(&quot;direct:start&quot;).resequencer(header(&quot;seqnum&quot;)).stream(
036     *         StreamResequencerConfig.getDefault()).to(&quot;mock:result&quot;)
037     * </pre>
038     * 
039     * is equivalent to
040     * 
041     * <pre>
042     * from(&quot;direct:start&quot;).resequencer(header(&quot;seqnum&quot;)).stream().to(&quot;mock:result&quot;)
043     * </pre>
044     * 
045     * Custom values for <code>capacity</code> and <code>timeout</code> can be
046     * set like in this example:
047     * 
048     * <pre>
049     * from(&quot;direct:start&quot;).resequencer(header(&quot;seqnum&quot;)).stream(
050     *         new StreamResequencerConfig(300, 400L)).to(&quot;mock:result&quot;)
051     * </pre>
052     * 
053     * @author Martin Krasser
054     * 
055     * @version $Revision: 1276 $
056     */
057    @XmlRootElement
058    @XmlAccessorType(XmlAccessType.FIELD)
059    public class StreamResequencerConfig {
060    
061        @XmlAttribute
062        private Integer capacity; // optional XML attribute requires wrapper object
063    
064        @XmlAttribute
065        private Long timeout; // optional XML attribute requires wrapper object
066        
067        @XmlTransient
068        private ExpressionResultComparator<Exchange> comparator;
069    
070        /**
071         * Creates a new {@link StreamResequencerConfig} instance using default
072         * values for <code>capacity</code> (1000) and <code>timeout</code>
073         * (1000L). Elements of the sequence are compared using the
074         * {@link DefaultExchangeComparator}.
075         */
076        public StreamResequencerConfig() {
077            this(1000, 1000L);
078        }
079    
080        /**
081         * Creates a new {@link BatchResequencerConfig} instance using the given
082         * values for <code>capacity</code> and <code>timeout</code>. Elements
083         * of the sequence are compared using the {@link DefaultExchangeComparator}.
084         * 
085         * @param capacity   capacity of the resequencer's inbound queue.
086         * @param timeout    minimum time to wait for missing elements (messages).
087         */
088        public StreamResequencerConfig(int capacity, long timeout) {
089            this(capacity, timeout, new DefaultExchangeComparator());
090        }
091    
092        /**
093         * Creates a new {@link BatchResequencerConfig} instance using the given
094         * values for <code>capacity</code> and <code>timeout</code>. Elements
095         * of the sequence are compared with the given
096         * {@link ExpressionResultComparator}.
097         * 
098         * @param capacity   capacity of the resequencer's inbound queue.
099         * @param timeout    minimum time to wait for missing elements (messages).
100         */
101        public StreamResequencerConfig(int capacity, long timeout, ExpressionResultComparator<Exchange> comparator) {
102            this.capacity = capacity;
103            this.timeout = timeout;
104            this.comparator = comparator;
105        }
106    
107        /**
108         * Returns a new {@link StreamResequencerConfig} instance using default
109         * values for <code>capacity</code> (1000) and <code>timeout</code>
110         * (1000L). Elements of the sequence are compared using the
111         * {@link DefaultExchangeComparator}.
112         * 
113         * @return a default {@link StreamResequencerConfig}.
114         */
115        public static StreamResequencerConfig getDefault() {
116            return new StreamResequencerConfig();
117        }
118        
119        public int getCapacity() {
120            return capacity;
121        }
122    
123        public void setCapacity(int capacity) {
124            this.capacity = capacity;
125        }
126    
127        public long getTimeout() {
128            return timeout;
129        }
130    
131        public void setTimeout(long timeout) {
132            this.timeout = timeout;
133        }
134    
135        public ExpressionResultComparator<Exchange> getComparator() {
136            return comparator;
137        }
138    
139        public void setComparator(ExpressionResultComparator<Exchange> comparator) {
140            this.comparator = comparator;
141        }
142        
143    }