KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > connector > ClusterableConnectorRemote


1 /*
2  * Copyright (c) 2005 Your Corporation. All Rights Reserved.
3  */

4
5 package org.jfox.ioc.connector;
6
7 import java.io.Serializable JavaDoc;
8 import java.rmi.RemoteException JavaDoc;
9
10 /**
11  * 把一个普通的remote包装成 clusterable 的remote,它只是一个 Serializable 类,
12  * 并不作真实的远程调用
13  *
14  * Clusterable 的remote需要在调用成功之后将状态复制到集群节点中
15  *
16  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
17  */

18
19 public class ClusterableConnectorRemote extends AbstractConnectorRemote implements ConnectorRemote, Serializable JavaDoc {
20
21     private ConnectorRemote remote;
22     private ConnectorRemote[] clusterRemotes;
23
24     public ClusterableConnectorRemote(ConnectorRemote remote, ConnectorRemote[] clusterNodes) {
25         this.remote = remote;
26         this.clusterRemotes = clusterNodes;
27     }
28
29     /**
30      * 该方法被 ConnectorInvoker 调用,该方法是一个序列化的方法,调用真实的remote完成远程调用的过程
31      * 在该方法中处理Failover和状态复制
32      *
33      * @param invocation
34      * @return
35      * @throws RemoteException
36      * @throws Exception
37      */

38     public Object JavaDoc invoke(Invocation invocation) throws RemoteException JavaDoc, Exception JavaDoc {
39         Exception JavaDoc re = null;
40         try {
41             remote.ping();
42         }
43         catch(Exception JavaDoc e){ // remote 不通
44
re = e;
45             remote = null;
46             if(clusterRemotes != null && clusterRemotes.length >0){
47                 for(int i=0; i<clusterRemotes.length; i++){
48                     ConnectorRemote _remote = clusterRemotes[i];
49                         try {
50                             _remote.ping();
51                         }
52                         catch(Exception JavaDoc e1){
53                             e1.printStackTrace();
54                             continue;
55                         }
56                         remote = _remote;
57                 }
58             }
59         }
60         if(remote == null){
61             throw re;
62         }
63         invocation.setClustable(true);
64         Object JavaDoc result = remote.invoke(invocation);
65         return result;
66     }
67
68     public void ping() throws RemoteException JavaDoc {
69         remote.ping();
70     }
71
72     public String JavaDoc getProtocol() throws RemoteException JavaDoc {
73         return remote.getProtocol();
74     }
75
76     public ConnectorRemote getRemote() {
77         return remote;
78     }
79
80     /**
81      * 不需要做实际的start操作
82      * @throws Exception
83      */

84     protected void doStart() throws Exception JavaDoc {
85     }
86
87     protected void doStop() throws Exception JavaDoc {
88     }
89
90     protected void doInit() throws Exception JavaDoc {
91     }
92
93     protected void doDestroy() throws Exception JavaDoc {
94     }
95
96
97     public static void main(String JavaDoc[] args) {
98
99     }
100 }
101
Popular Tags