1 22 package org.jboss.aspects.remoting; 23 24 import org.jboss.aop.DispatcherConnectException; 25 import org.jboss.aop.util.PayloadKey; 26 import org.jboss.ha.framework.interfaces.GenericClusteringException; 27 import org.jboss.ha.framework.interfaces.LoadBalancePolicy; 28 import org.jboss.remoting.CannotConnectException; 29 import org.jboss.remoting.InvokerLocator; 30 31 import java.util.ArrayList ; 32 33 39 public class ClusterChooserInterceptor implements org.jboss.aop.advice.Interceptor, ClusterConstants, java.io.Serializable 40 { 41 private static final long serialVersionUID = -8666382019058421135L; 42 43 public static final ClusterChooserInterceptor singleton = new ClusterChooserInterceptor(); 44 45 public String getName() 46 { 47 return "ClusterChooserInterceptor"; 48 } 49 50 public Object invoke(org.jboss.aop.joinpoint.Invocation invocation) 51 throws Throwable 52 { 53 LoadBalancePolicy lb = (LoadBalancePolicy) invocation.getMetaData(CLUSTERED_REMOTING, LOADBALANCE_POLICY); 54 FamilyWrapper family = (FamilyWrapper) invocation.getMetaData(CLUSTERED_REMOTING, CLUSTER_FAMILY_WRAPPER); 55 56 int failoverCounter = 0; 60 String familyName = family.get().getFamilyName(); 61 invocation.getMetaData().addMetaData(CLUSTERED_REMOTING, CLUSTER_FAMILY, familyName, PayloadKey.AS_IS); 62 InvokerLocator target = (InvokerLocator) lb.chooseTarget(family.get()); 63 Throwable lastException = null; 64 while (target != null) 65 { 66 invocation.getMetaData().addMetaData(CLUSTERED_REMOTING, FAILOVER_COUNTER, new Integer (failoverCounter), PayloadKey.AS_IS); 67 invocation.getMetaData().addMetaData(InvokeRemoteInterceptor.REMOTING, InvokeRemoteInterceptor.INVOKER_LOCATOR, target, PayloadKey.AS_IS); 68 invocation.getMetaData().addMetaData(CLUSTERED_REMOTING, CLUSTER_VIEW_ID, new Long (family.get().getCurrentViewId()), PayloadKey.AS_IS); 69 70 boolean definitivlyRemoveNodeOnFailure = true; 71 lastException = null; 72 try 73 { 74 75 76 77 Object rsp = invocation.invokeNext(); 78 ArrayList newReplicants = (ArrayList ) invocation.getResponseAttachment("replicants"); 79 if (newReplicants != null) 80 { 81 long newViewId = ((Long ) invocation.getResponseAttachment("viewId")).longValue(); 82 family.get().updateClusterInfo(newReplicants, newViewId); 83 } 84 return rsp; 85 } 86 catch(DispatcherConnectException dce) 87 { 88 lastException = dce; 92 } 93 catch (CannotConnectException ex) 94 { 95 lastException = ex; 96 } 97 catch (GenericClusteringException gce) 98 { 99 lastException = gce; 100 if (gce.getCompletionStatus() == GenericClusteringException.COMPLETED_NO) 105 { 106 if (family.get().getTargets().size() >= failoverCounter) 110 { 111 if (!gce.isDefinitive()) 112 definitivlyRemoveNodeOnFailure = false; 113 } 114 } 115 else 116 { 117 throw new RuntimeException ("Clustering exception thrown", gce); 118 } 119 } 120 catch (Throwable t) 121 { 122 if(t.getCause() instanceof GenericClusteringException) 126 { 127 GenericClusteringException gce = (GenericClusteringException)t.getCause(); 128 lastException = gce; 129 if (gce.getCompletionStatus() == GenericClusteringException.COMPLETED_NO) 134 { 135 if (family.get().getTargets().size() >= failoverCounter) 139 { 140 if (!gce.isDefinitive()) 141 definitivlyRemoveNodeOnFailure = false; 142 } 143 } 144 else 145 { 146 throw new RuntimeException ("Clustering exception thrown", gce); 147 } 148 } else 149 { 150 throw t; 151 } 152 } 153 154 family.get().removeDeadTarget(target); 156 if (!definitivlyRemoveNodeOnFailure) 157 { 158 family.get().resetView(); 159 } 160 161 target = (InvokerLocator) lb.chooseTarget(family.get()); 162 if (target == null) 163 { 164 if (lastException != null) 165 { 166 throw new RuntimeException ("cluster invocation failed, last exception was: ", lastException); 167 } 168 else 169 { 170 throw new RuntimeException ("cluster invocation failed"); 171 } 172 } 173 failoverCounter++; 174 } 175 throw new RuntimeException ("Unreachable?: Service unavailable."); 177 } 178 } 179 | Popular Tags |