Java 实现通用WS-retry调用

Java 实现通用WS-retry调用,java,Java,我想对我的WS调用实现通用的重试调用机制。 WS调用可能会有所不同,因此结果具有不同的类型。 假设有两种类型的呼叫: int result = wsPortType.callMethod(arg1, arg2); MyObject result = anotherWsPortType.callSomeOtherMethod(arg); 我可以对所有WS-call方法执行类似的操作: int result; try { result = wsPortType.callMethod(arg1,

我想对我的WS调用实现通用的重试调用机制。 WS调用可能会有所不同,因此结果具有不同的类型。 假设有两种类型的呼叫:

int result = wsPortType.callMethod(arg1, arg2);
MyObject result = anotherWsPortType.callSomeOtherMethod(arg);
我可以对所有WS-call方法执行类似的操作:

int result;
try
{
  result = wsPortType.callMethod(arg1, arg2);
}
catch (Exception ex)
{
  try
  {
    result = wsPortType.callMethod(arg1, arg2);
  }
  catch (Exception ex0)
  {
    // TODO
  } 
}
但我必须为每个WS-call编写,我不想这样做

我在想这样的事情:

public class RetryWsCall
{
  public interface RetryCallInterface
  {
    public <T> T RetryCallMethod();
  };

  public <T> T callWs(RetryCallInterface retryCallImplementation, int retryCount) 
  {
    try
    {
      return retryCallImplementation.RetryCallMethod();
    }
    catch (Exception ex)
    {
      try
      {
        return retryCallImplementation.RetryCallMethod();
      }
      catch (Exception ex0)
      {
        // TODO
      } 
    }
  }
}
public class RetryWsCall
{ 
  public interface RetryCallInterface
  {
    public <T> T RetryCallMethod();
  };

  public <T> T callWs(RetryCallInterface retryCallImplementation, int retryCount) 
  {
    try
    {
      return retryCallImplementation.RetryCallMethod();
    }
    catch (Exception ex)
    {
      if (retryCount > 0)
      {
        while (retryCount > 0)
        {
          try
          {
            return result = retryCallImplementation.RetryCallMethod();
          }
          catch (Exception ex0)
          {
            retryCount--;
          } 
        }
      }
      throw ex;
    }
  }
}
公共类RetryWsCall
{
公共接口RetryCallInterface
{
公共T RetryCallMethod();
};
公共T调用(RetryCallInterface retryCallImplementation,int retryCount)
{
尝试
{
返回retryCallImplementation.RetryCallMethod();
}
捕获(例外情况除外)
{
尝试
{
返回retryCallImplementation.RetryCallMethod();
}
捕获(异常ex0)
{
//待办事项
} 
}
}
}
然后打个电话,比如:

RetryWsCall retryWsCall = new RetryWsCall();
retryWsCall.callWs(new RetryWsCall.RetryCallInterface()
{
  @Override
  public <T> T RetryCallMethod()
  {
    return wsPortType.callMethod(arg1, arg2);
  }
}, 2);
RetryWsCall RetryWsCall=new RetryWsCall();
retryWsCall.callWs(新的retryWsCall.RetryCallInterface()
{
@凌驾
公共T RetryCallMethod()
{
返回wsPortType.callMethod(arg1,arg2);
}
}, 2);

但是我不能正确地使用它,因此泛型方法返回正确的值类型。

好的,我找到了答案。问题是我无法正确使用泛型。 我正在添加max attemts并有意跳过此示例中的日志记录

处理重试的类可能如下所示:

public class RetryWsCall
{
  public interface RetryCallInterface
  {
    public <T> T RetryCallMethod();
  };

  public <T> T callWs(RetryCallInterface retryCallImplementation, int retryCount) 
  {
    try
    {
      return retryCallImplementation.RetryCallMethod();
    }
    catch (Exception ex)
    {
      try
      {
        return retryCallImplementation.RetryCallMethod();
      }
      catch (Exception ex0)
      {
        // TODO
      } 
    }
  }
}
public class RetryWsCall
{ 
  public interface RetryCallInterface
  {
    public <T> T RetryCallMethod();
  };

  public <T> T callWs(RetryCallInterface retryCallImplementation, int retryCount) 
  {
    try
    {
      return retryCallImplementation.RetryCallMethod();
    }
    catch (Exception ex)
    {
      if (retryCount > 0)
      {
        while (retryCount > 0)
        {
          try
          {
            return result = retryCallImplementation.RetryCallMethod();
          }
          catch (Exception ex0)
          {
            retryCount--;
          } 
        }
      }
      throw ex;
    }
  }
}


这是一个相当复杂的问题,你可能需要一些指数退避,正确的日志,也许还需要一些“最大尝试”。如果你在制作应用程序中需要这个,我建议使用已经存在且广泛使用的第三方库。这仍然是一个有趣的课题,而且应该是一个很有教育意义的课题:例如,我面临的问题是,我不知道如何正确使用泛型方法。我意识到我应该简化这个问题,不要提及WS调用,因为它不是主要问题。