001    /*
002     * Copyright (C) The Apache Software Foundation. All rights reserved.
003     *
004     * This software is published under the terms of the Apache Software License
005     * version 1.1, a copy of which has been included with this distribution in
006     * the LICENSE file.
007     * 
008     * $Id: DistributeBridgeMDO.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    package org.apache.commons.messagelet;
011    
012    import java.util.ArrayList;
013    import java.util.List;
014    
015    import javax.jms.Destination;
016    import javax.jms.JMSException;
017    import javax.servlet.ServletException;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    
022    
023    /** 
024     * <p><code>DistributeBridgeMDO</code> is an MDO which 
025     * consumes JMS from one destination and randomly distributes
026     * them across a number of other destinations.
027     * This MDO can be used to provide a simple load balancing mechanism
028     * consuming from one destination and sending messages to a number of different
029     * physical destinations.
030     * </p>
031     *
032     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
033     * @version $Revision: 155459 $
034     */
035    public class DistributeBridgeMDO extends BridgeMDO {
036    
037        /** Logger */
038        private static final Log log = LogFactory.getLog(DistributeBridgeMDO.class);
039    
040        /** a List of output Destination objects */
041        private List outputDestinations = new ArrayList();
042          
043        public DistributeBridgeMDO() {
044        }
045        
046        // Properties
047        //-------------------------------------------------------------------------
048    
049        /**
050         * @return a List of output Destinations
051         */    
052        public List getOutputDestinations() {
053            return outputDestinations;
054        }
055        
056        /**
057         * Adds a new output subject
058         */
059        public void addOutputSubject(String subject) throws JMSException {
060            Destination destination = getOutputMessenger().getDestination( subject );
061            outputDestinations.add( destination );
062        }
063        
064        
065        
066        /**
067         * Randomly chooses a destination from the list of destinations
068         */
069        public Destination getOutputDestination() throws JMSException {
070            int size = outputDestinations.size();
071            if ( size < 1 ) {
072                throw new JMSException( "No output destinations are available" );
073            }
074            
075            int index = (int) Math.round( Math.random() * size );
076            if ( index == size ) {
077                index = size -1;
078            }
079            return (Destination) outputDestinations.get(index);        
080        }
081        
082        
083        // Implementation methods
084        //-------------------------------------------------------------------------
085    
086        protected void validateOutputDestination() throws JMSException, ServletException {
087            int size = outputDestinations.size();
088            if ( size < 1 ) {
089                throw new JMSException( "No output destinations are available" );
090            }
091        }
092    }
093    
094