1 package org.jacorb.notification.engine; 2 3 22 23 import org.apache.avalon.framework.logger.Logger; 24 import org.jacorb.notification.interfaces.IProxyPushSupplier; 25 import org.jacorb.notification.util.LogUtil; 26 import org.omg.CORBA.OBJECT_NOT_EXIST ; 27 import org.omg.CosEventComm.Disconnected; 28 29 33 public abstract class AbstractRetryStrategy implements RetryStrategy 34 { 35 protected final Logger logger_ = LogUtil.getLogger(getClass().getName()); 36 protected final PushOperation pushOperation_; 37 38 protected final IProxyPushSupplier pushSupplier_; 39 40 private boolean active_ = true; 41 42 44 public AbstractRetryStrategy(IProxyPushSupplier pushSupplier, PushOperation operation) 45 { 46 pushSupplier_ = pushSupplier; 47 pushOperation_ = operation; 48 } 49 50 52 public void dispose() 53 { 54 pushOperation_.dispose(); 55 } 56 57 protected boolean isRetryAllowed() 58 { 59 return active_ && pushSupplier_.isRetryAllowed(); 60 } 61 62 protected void remoteExceptionOccured(Throwable error) throws RetryException 63 { 64 logger_.debug("Error during retry", error); 65 66 if (isFatalException(error)) 67 { 68 pushSupplier_.destroy(); 69 active_ = false; 70 71 throw new RetryException("fatal exception while retrying push"); 72 } 73 74 pushSupplier_.incErrorCounter(); 75 76 if (!isRetryAllowed()) 77 { 78 pushSupplier_.destroy(); 79 active_ = false; 80 81 throw new RetryException("no more retries. giving up."); 82 } 83 84 waitUntilNextTry(); 85 } 86 87 public static boolean isFatalException(Throwable error) 88 { 89 if (error instanceof OBJECT_NOT_EXIST ) 90 { 91 return true; 92 } 93 else if (error instanceof Disconnected) 94 { 95 return true; 96 } 97 return false; 98 } 99 100 protected abstract long getTimeToWait(); 101 102 public final void retry() throws RetryException 103 { 104 if (isRetryAllowed()) 105 { 106 waitUntilNextTry(); 107 108 retryInternal(); 109 } else 110 { 111 dispose(); 112 } 113 } 114 115 protected abstract void retryInternal() throws RetryException; 116 117 private void waitUntilNextTry() 118 { 119 long timeToWait = getTimeToWait(); 120 121 try 122 { 123 if (timeToWait > 0) 124 { 125 Thread.sleep(timeToWait); 126 } 127 } catch (InterruptedException ignored) 128 { 129 } 131 } 132 } | Popular Tags |