Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DistributeBridgeMDO |
|
| 2.0;2 |
1 | /* | |
2 | * Copyright (C) The Apache Software Foundation. All rights reserved. | |
3 | * | |
4 | * This software is published under the terms of the Apache Software License | |
5 | * version 1.1, a copy of which has been included with this distribution in | |
6 | * the LICENSE file. | |
7 | * | |
8 | * $Id: DistributeBridgeMDO.java 155459 2005-02-26 13:24:44Z dirkv $ | |
9 | */ | |
10 | package org.apache.commons.messagelet; | |
11 | ||
12 | import java.util.ArrayList; | |
13 | import java.util.List; | |
14 | ||
15 | import javax.jms.Destination; | |
16 | import javax.jms.JMSException; | |
17 | import javax.servlet.ServletException; | |
18 | ||
19 | import org.apache.commons.logging.Log; | |
20 | import org.apache.commons.logging.LogFactory; | |
21 | ||
22 | ||
23 | /** | |
24 | * <p><code>DistributeBridgeMDO</code> is an MDO which | |
25 | * consumes JMS from one destination and randomly distributes | |
26 | * them across a number of other destinations. | |
27 | * This MDO can be used to provide a simple load balancing mechanism | |
28 | * consuming from one destination and sending messages to a number of different | |
29 | * physical destinations. | |
30 | * </p> | |
31 | * | |
32 | * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> | |
33 | * @version $Revision: 155459 $ | |
34 | */ | |
35 | public class DistributeBridgeMDO extends BridgeMDO { | |
36 | ||
37 | /** Logger */ | |
38 | 0 | private static final Log log = LogFactory.getLog(DistributeBridgeMDO.class); |
39 | ||
40 | /** a List of output Destination objects */ | |
41 | 0 | private List outputDestinations = new ArrayList(); |
42 | ||
43 | 0 | public DistributeBridgeMDO() { |
44 | 0 | } |
45 | ||
46 | // Properties | |
47 | //------------------------------------------------------------------------- | |
48 | ||
49 | /** | |
50 | * @return a List of output Destinations | |
51 | */ | |
52 | public List getOutputDestinations() { | |
53 | 0 | return outputDestinations; |
54 | } | |
55 | ||
56 | /** | |
57 | * Adds a new output subject | |
58 | */ | |
59 | public void addOutputSubject(String subject) throws JMSException { | |
60 | 0 | Destination destination = getOutputMessenger().getDestination( subject ); |
61 | 0 | outputDestinations.add( destination ); |
62 | 0 | } |
63 | ||
64 | ||
65 | ||
66 | /** | |
67 | * Randomly chooses a destination from the list of destinations | |
68 | */ | |
69 | public Destination getOutputDestination() throws JMSException { | |
70 | 0 | int size = outputDestinations.size(); |
71 | 0 | if ( size < 1 ) { |
72 | 0 | throw new JMSException( "No output destinations are available" ); |
73 | } | |
74 | ||
75 | 0 | int index = (int) Math.round( Math.random() * size ); |
76 | 0 | if ( index == size ) { |
77 | 0 | index = size -1; |
78 | } | |
79 | 0 | return (Destination) outputDestinations.get(index); |
80 | } | |
81 | ||
82 | ||
83 | // Implementation methods | |
84 | //------------------------------------------------------------------------- | |
85 | ||
86 | protected void validateOutputDestination() throws JMSException, ServletException { | |
87 | 0 | int size = outputDestinations.size(); |
88 | 0 | if ( size < 1 ) { |
89 | 0 | throw new JMSException( "No output destinations are available" ); |
90 | } | |
91 | 0 | } |
92 | } | |
93 | ||
94 |