KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > server > journal > impl > FailoverArbiter


1 package com.ubermq.jms.server.journal.impl;
2
3 import com.ubermq.jms.server.journal.*;
4 import com.ubermq.jms.common.routing.impl.*;
5 import com.ubermq.kernel.*;
6 import java.io.*;
7 import java.util.*;
8
9 /**
10  * This arbiter implementation supports failover,
11  * also known as hot/standby or active/backup
12  * deployment topologies. <P>
13  *
14  * The arbiter works such that the first connection
15  * receives all messages until that connection
16  * disconnects or fail, at which point the next most-
17  * recently connected output node will begin
18  * to receive messages.
19  */

20 public class FailoverArbiter
21     implements DurableConnectionArbiter
22 {
23     private transient DatagramSink active;
24     private transient LinkedList cxns;
25
26     public static final long serialVersionUID = 1L;
27
28     /**
29      * Constructs a failover arbiter.
30      */

31     public FailoverArbiter()
32     {
33         cxns = new LinkedList();
34     }
35
36     private void readObject(ObjectInputStream ois)
37         throws IOException, ClassNotFoundException JavaDoc
38     {
39         ois.defaultReadObject();
40         cxns = new LinkedList();
41     }
42
43     /**
44      * Switches the active connection to the next one
45      * from the list of connections, if any,
46      * and recovers the durable subscriber to re-output
47      * unacknowledged messages.
48      */

49     private void nextConnection()
50     {
51         if (cxns.size() > 0) {
52             active = (DatagramSink)cxns.removeFirst();
53         } else {
54             active = null;
55         }
56     }
57
58     /**
59      * Outputs a datagram. If the datagram causes an overflow,
60      * the given overflow handler is called to determine whether the
61      * operation should be retried.
62      * @param d a datagram
63      * @param h the overflow handler to use
64      */

65     public void output(IDatagram d,
66                        IOverflowHandler h)
67         throws IOException
68     {
69         if (isOpen()) {
70             try {
71                 active.output(d, h);
72             } catch(IOException iox) {
73                 // move to next connection if we
74
// fail here.
75
nextConnection();
76                 throw iox;
77             }
78         }
79         else
80             throw new IOException("not open");
81     }
82
83     /**
84      * Determines whether the destination node is available for output.
85      * @return true if the output dest node is available
86      */

87     public boolean isOpen()
88     {
89         return (active != null);
90     }
91
92     /**
93      * Disconnects an output node from the arbiter.
94      */

95     public void disconnect(DatagramSink cdn)
96     {
97         if (cdn.equals(active)) {
98             nextConnection();
99         } else {
100             cxns.remove(cdn);
101         }
102     }
103
104     /**
105      * Disconnects all output nodes from the arbiter.
106      */

107     public void disconnectAll()
108     {
109         active = null;
110         cxns.clear();
111     }
112
113     /**
114      * Connects a new output destination node to the
115      * arbiter.
116      */

117     public void connect(DatagramSink cdn)
118     {
119         if (active == null)
120             active = cdn;
121         else {
122             cxns.addLast(cdn);
123         }
124     }
125
126     public String JavaDoc toString()
127     {
128         return "Active Proxy: " + active + " Backups: " + cxns;
129     }
130
131     public String JavaDoc toHtml()
132     {
133         if (active != null)
134         {
135             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
136             sb.append(AbstractMultipleArbiter.getDisplayName(active));
137             sb.append(" <I>(Active)</I><br>");
138
139             Iterator iter = cxns.iterator();
140             while (iter.hasNext())
141             {
142                 sb.append(AbstractMultipleArbiter.getDisplayName((DatagramSink)iter.next()));
143                 sb.append("<br>");
144             }
145
146             return sb.toString();
147         }
148         else
149         {
150             return "<b>Disconnected</b>";
151         }
152     }
153 }
154
Popular Tags