001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.builder;
019    
020    import org.apache.camel.Expression;
021    import org.apache.camel.Processor;
022    import org.apache.camel.Route;
023    import org.apache.camel.Exchange;
024    import org.apache.camel.Service;
025    import org.apache.camel.processor.Resequencer;
026    
027    import java.util.List;
028    
029    /**
030     * @version $Revision: 1.1 $
031     */
032    public class ResequencerBuilder extends FromBuilder {
033        private final List<Expression<Exchange>> expressions;
034        private long batchTimeout = 1000L;
035        private int batchSize = 100;
036    
037        public ResequencerBuilder(FromBuilder builder, List<Expression<Exchange>> expressions) {
038            super(builder);
039            this.expressions = expressions;
040        }
041    
042        @Override
043        public Route createRoute() throws Exception {
044            final Processor processor = super.createProcessor();
045            final Resequencer resequencer = new Resequencer(getFrom(), processor, expressions);
046    
047            return new Route<Exchange>(getFrom()) {
048                protected void addServices(List<Service> list) throws Exception {
049                    list.add(resequencer);
050                }
051    
052                @Override
053                public String toString() {
054                    return "ResequencerRoute[" + getEndpoint() + " -> " + processor + "]";
055                }
056            };
057        }
058    
059        // Builder methods
060        //-------------------------------------------------------------------------
061        public ResequencerBuilder batchSize(int batchSize) {
062            setBatchSize(batchSize);
063            return this;
064        }
065    
066        public ResequencerBuilder batchTimeout(int batchTimeout) {
067            setBatchTimeout(batchTimeout);
068            return this;
069        }
070    
071        // Properties
072        //-------------------------------------------------------------------------
073        public int getBatchSize() {
074            return batchSize;
075        }
076    
077        public void setBatchSize(int batchSize) {
078            this.batchSize = batchSize;
079        }
080    
081        public long getBatchTimeout() {
082            return batchTimeout;
083        }
084    
085        public void setBatchTimeout(long batchTimeout) {
086            this.batchTimeout = batchTimeout;
087        }
088    }