KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > client > unicast > FailoverConnectionDescriptor


1 package com.ubermq.jms.client.unicast;
2 import com.ubermq.jms.client.*;
3 import com.ubermq.jms.client.impl.*;
4 import com.ubermq.kernel.*;
5 import com.ubermq.kernel.chooser.*;
6 import com.ubermq.kernel.overflow.*;
7 import java.net.*;
8 import java.util.*;
9
10 /**
11  * Describes a connection strategy that uses a
12  * set of connections, and produces a different
13  * SocketAddress on each request.<p>
14  *
15  * This is typically used for failover and
16  * load-balancing: if connect attempts fail,
17  * callers can continually query for new destinations
18  * to connect to.<p>
19  *
20  * Load-balancing is also accomplished because
21  * every call to <code>getAddress</code> can
22  * potentially return a different destination
23  * for each connecting client.
24  *
25  */

26 public class FailoverConnectionDescriptor
27     implements InternetConnectionDescriptor,
28     IOverflowHandler
29 {
30     private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(FailoverConnectionDescriptor.class);
31     
32     private Collection connections;
33     private IOverflowHandler delegate;
34     private Chooser selector;
35
36     /**
37      * Constructs a connection descriptor from a set of
38      * InternetConnectionDescriptor objects, and an overflow handler.<P>
39      *
40      * The overflow handler may be used by callers (if they
41      * support the capability) to take some corrective action
42      * if a connection attempt fails. <p>
43      *
44      * The connection collection must be compatible with the
45      * specified <code>Chooser</code> implementation.<p>
46      *
47      * @param connections a collection of <code>InternetConnectionDescriptor</code>
48      * objects.
49      * @param handler an overflow handler used to process failures.
50      * @param selector an implementation of the Chooser interface.
51      */

52     public FailoverConnectionDescriptor(Collection connections,
53                                         IOverflowHandler handler,
54                                         Chooser selector)
55     {
56         if (connections == null ||
57             handler == null ||
58             selector == null)
59             throw new IllegalArgumentException JavaDoc();
60
61         this.connections = connections;
62         this.delegate = handler;
63         this.selector = selector;
64     }
65
66     /**
67      * Parses a text failover specification and creates a
68      * connection descriptor from it.<P>
69      *
70      * The format of the failover spec is:
71      * <pre>
72      * url,url,url
73      * </pre><P>
74      *
75      * Each url will be attempted in sequence, handled
76      * by an exponential backoff handler with
77      * a maximum timeout of DEFAULT_TIMEOUT. The list will
78      * continue to wrap around as needed, until the
79      * connection is reestablished.
80
81      * @param spec a spec as described above
82      * @param defaultPort the default port to use if a url does not
83      * indicate a port explicitly
84      *
85      * @return a failover connection descriptor
86      */

87     public static final InternetConnectionDescriptor parseFailoverSpec(String JavaDoc spec,
88                                                                        int defaultPort)
89     {
90         List connections = new ArrayList();
91
92         StringTokenizer st = new StringTokenizer(spec, ",");
93         while(st.hasMoreTokens())
94         {
95             String JavaDoc url = st.nextToken();
96             connections.add(new SimpleInternetConnectionDescriptor(url, defaultPort));
97         }
98
99         log.debug("parsed failover URL as " + connections);
100
101         // return it. if there is only one URL, return a fail-fast URL.
102
if (connections.size() == 1)
103             return new SimpleInternetConnectionDescriptor(spec, defaultPort);
104         else
105             return new FailoverConnectionDescriptor(connections,
106                                                     new KeepTrying(connections.size() - 1,
107                                                                    0),
108                                                     new RoundRobinChooser());
109     }
110
111     /**
112      * Parses a failover spec based on a URI and default port.
113      *
114      * @see #parseFailoverSpec
115      *
116      * @param uri an URI
117      * @param defaultPort the default port
118      *
119      * @return an InternetConnectionDescriptor
120      */

121     public static final InternetConnectionDescriptor parseFailoverSpec(URI uri,
122                                                                        int defaultPort)
123     {
124         String JavaDoc hosts = uri.getSchemeSpecificPart();
125         int slash = hosts.indexOf('/', 2); // we start at 2 to skip the leading //
126
if (slash >= 0)
127             hosts = hosts.substring(0, slash);
128
129         return parseFailoverSpec(hosts, defaultPort);
130     }
131
132     public SocketAddress getAddress()
133     {
134         // choose a descriptor
135
InternetConnectionDescriptor icd = ((InternetConnectionDescriptor)selector.choose(connections));
136         log.debug("failover choosing " + icd);
137
138         return icd.getAddress();
139     }
140
141     /**
142      * Handles an overflow situation that is not related to outputting
143      * a datagram.
144      */

145     public int overflow()
146     {
147         return delegate.overflow();
148     }
149
150     /**
151      * Produces a new overflow handler to be used if the
152      * recommended retried output attempt fails again.
153      * This can be return null if the overflow()
154      * method never returns true.
155      */

156     public IOverflowHandler getRetryHandler()
157     {
158         return new FailoverConnectionDescriptor(connections,
159                                                 delegate.getRetryHandler(),
160                                                 selector);
161     }
162
163     /**
164      * Not implemented for connection descriptors.
165      * @throws UnsupportedOperationException
166      */

167     public int overflow(IDatagram d)
168     {
169         throw new UnsupportedOperationException JavaDoc();
170     }
171
172     public String JavaDoc toString()
173     {
174         return "FailoverConnectionDescriptor for " + connections.toString();
175     }
176 }
177
Popular Tags