1 22 package org.jboss.proxy.ejb; 23 24 import java.io.ObjectOutput ; 25 import java.io.IOException ; 26 import java.io.ObjectInput ; 27 import java.util.Hashtable ; 28 import java.util.Properties ; 29 import javax.naming.InitialContext ; 30 31 import org.jboss.invocation.Invocation; 32 import org.jboss.invocation.InvocationContext; 33 import org.jboss.invocation.InvocationType; 34 import org.jboss.invocation.InvocationKey; 35 import org.jboss.invocation.Invoker; 36 import org.jboss.invocation.ServiceUnavailableException; 37 import org.jboss.logging.Logger; 38 import org.jboss.naming.NamingContextFactory; 39 import org.jboss.proxy.Interceptor; 40 41 63 public class RetryInterceptor extends Interceptor 64 { 65 66 private static final long serialVersionUID = 1; 67 68 private static final int EXTERNAL_VERSION = 1; 69 private static Logger log = Logger.getLogger(RetryInterceptor.class); 70 71 private static Properties retryEnv; 72 73 74 private transient boolean retry; 75 76 private transient boolean trace; 77 78 private transient int maxRetries = -1; 79 80 private transient long sleepTime = 1000; 81 82 86 public static void setRetryEnv(Properties env) 87 { 88 retryEnv = env; 89 } 90 91 94 public RetryInterceptor() 95 {} 96 97 105 protected RetryInterceptor(int maxRetries, long sleepTime) 106 { 107 this.maxRetries = maxRetries; 108 this.sleepTime = sleepTime; 109 } 110 111 113 public void setRetry(boolean flag) 114 { 115 this.retry = flag; 116 } 117 public boolean getRetry() 118 { 119 return this.retry; 120 } 121 122 125 public int getMaxRetries() 126 { 127 return maxRetries; 128 } 129 130 136 public void setMaxRetries(int maxRetries) 137 { 138 this.maxRetries = maxRetries; 139 } 140 141 144 public long getSleepTime() 145 { 146 return sleepTime; 147 } 148 149 152 public void setSleepTime(long sleepTime) 153 { 154 this.sleepTime = sleepTime; 155 } 156 157 162 public Object invoke(Invocation invocation) 163 throws Throwable 164 { 165 Object result = null; 166 InvocationContext ctx = invocation.getInvocationContext(); 167 retry = true; 168 int retryCount = 0; 169 while( retry == true ) 170 { 171 Interceptor next = getNext(); 172 try 173 { 174 if( trace ) 175 log.trace("invoke, method="+invocation.getMethod()); 176 result = next.invoke(invocation); 177 break; 178 } 179 catch(ServiceUnavailableException e) 180 { 181 if( trace ) 182 log.trace("Invocation failed", e); 183 184 InvocationType type = invocation.getType(); 185 if ((maxRetries > -1 && retryCount >= maxRetries) 186 || reestablishInvokerProxy(ctx, type) == false) 187 { 188 throw e; 189 } 190 retryCount++; 191 } 192 } 193 return result; 194 } 195 196 209 private boolean reestablishInvokerProxy(InvocationContext ctx, InvocationType type) 210 { 211 if( trace ) 212 log.trace("Begin reestablishInvokerProxy"); 213 214 boolean isRemote = type == InvocationType.REMOTE; 215 String jndiName = (String ) ctx.getValue(InvocationKey.JNDI_NAME); 216 if( isRemote == true ) 217 jndiName += "-RemoteInvoker"; 218 else 219 jndiName += "-HomeInvoker"; 220 Hashtable retryProps = retryEnv; 221 if (retryProps == null) 222 { 223 retryProps = (Hashtable ) NamingContextFactory.lastInitialContextEnv.get(); 224 if ( trace ) 225 { 226 if (retryProps != null) 227 log.trace("Using retry properties from NamingContextFactory"); 228 else 229 log.trace("No retry properties available"); 230 } 231 } 232 else if ( trace ) 233 { 234 log.trace("Using static retry properties"); 235 } 236 237 int retryCount = 0; 238 Invoker newInvoker = null; 239 while( retry == true ) 240 { 241 try 242 { 243 Thread.sleep(sleepTime); 244 InitialContext namingCtx = new InitialContext (retryProps); 245 if( trace ) 246 log.trace("Looking for invoker: "+jndiName); 247 newInvoker = (Invoker) namingCtx.lookup(jndiName); 248 if( trace ) 249 log.trace("Found invoker: "+newInvoker); 250 ctx.setInvoker(newInvoker); 251 break; 252 } 253 catch(Throwable t) 254 { 255 retryCount++; 256 if( trace ) 257 log.trace("Retry attempt " + retryCount + 258 ": Failed to lookup proxy", t); 259 if (maxRetries > -1 && retryCount >= maxRetries) 260 { 261 if ( trace) 262 log.trace("Maximum retry attempts made"); 263 break; 264 } 265 } 266 } 267 if( trace ) 268 log.trace("End reestablishInvokerProxy"); 269 270 return (newInvoker != null); 271 } 272 273 276 public void writeExternal(final ObjectOutput out) 277 throws IOException 278 { 279 super.writeExternal(out); 280 out.writeInt(EXTERNAL_VERSION); 282 } 284 285 288 public void readExternal(final ObjectInput in) 289 throws IOException , ClassNotFoundException 290 { 291 super.readExternal(in); 292 int version = in.readInt(); 294 if( version == EXTERNAL_VERSION ) 295 { 296 } 298 trace = log.isTraceEnabled(); 300 } 301 } 302 | Popular Tags |