2012-06-06 2 views
6

La mia applicazione Java richiede una logica di tentativi in ​​caso di errori di chiamate remote. Queste chiamate remote sono:Chiamate di nuovo metodo in modo generico

  • sparsi in tutta l'applicazione
  • appartengono a diverse classi di servizio remoto.

Inoltre, la logica di tentativi potrà avere vari Retry e variando riprovare tentativi.

Ho bisogno di un'implementazione generica retry() che può effettuare chiamate di metodo appropriate a seconda di dove viene chiamato. Di seguito è riportata una semplice illustrazione del codice di cui sto cercando. So che possiamo provare a fare ciò usando java reflection, ma c'è un framework o un open source disponibile da qualche parte che è read-to-use?

try { 
ClassA objA = remoteServiceA.call(paramA1, paramA2, ...); 
} catch (Exception e){ 
ClassA objA = (ClassA)retry(remoteService, listOfParams, ..); // generic method call 
} 
.. 

try { 
ClassB objB = remoteServiceB.call(paramB1, paramB2, ...); 
} catch (Exception e){ 
ClassA objB = (ClassB)retry(remoteService, listOfParams, ..); // generic method call 
} 

risposta

0

da dove si ricevono i servizi? utilizzare una fabbrica per Proxy il servizio che si ottiene dalla fabbrica originale. Il proxy può quindi implementare il tentativo in modo trasparente. Guarda i proxy Proxy/Proxy java in riflessione.

0

Si supponga di avere un metodo, che deve essere ripetuto ogni 500ms e fino a 5 volte. classe corrente:

public class RemoteCaller{ 
    Service serviceCaller; 
    public void remoteCall(String message) { 
       serviceCaller.updateDetails(this.message); 
       return null; 
    } 
} 

approccio Modificato:

public class RetriableHelper<T> implements Callable<T> { 

    private Callable<T> task; 

    private int numberOfRetries; 

    private int numberOfTriesLeft; 

    private long timeToWait; 


    public RetriableHelper(int numberOfRetries, long timeToWait, Callable<T> task) { 
     this.numberOfRetries = numberOfRetries; 
     numberOfTriesLeft = numberOfRetries; 
     this.timeToWait = timeToWait; 
     this.task = task; 
    } 

    public T call() throws Exception { 
     while (true) { 
      try { 
       return task.call(); 
      } catch (InterruptedException e) { 
       throw e; 
      } catch (CancellationException e) { 
       throw e; 
      } catch (Exception e) { 
       numberOfTriesLeft--; 
       if (numberOfTriesLeft == 0) { 
        throw e; 
       } 
       Thread.sleep(timeToWait); 
      } 
     } 
    } 
} 


Backend system/remote call class: 

public class RemoteCaller{ 

    Service serviceCaller; 

    public void remoteCall(String message) { 

     class RemoteCallable implements Callable<Void> { 
      String message; 
      public RemoteCallable(String message) 
      { 
       this.message = message; 
      } 
      public Void call() throws Exception{ 
       serviceCaller.updateDetails(this.message); 
       return null; 
      } 
     } 


     RetriableHelper<Void> retriableHelper = new RetriableHelper<Void>(5, 500, new RemoteCallable(message)); 
     try { 
      retriableHelper.call(); 
     } catch (Exception e) { 
      throw e; 
     } 
    } 
}