如果出错,请重复CXF请求

时间:2014-06-30 06:32:07

标签: java web-services cxf

我有一个OSGi服务,我已经将其作为一个简单的Web服务与CXF一起公开,我为此创建了一个调用其方法的客户端。此服务的方法接受一个签名票证作为其参数之一,该票证标识执行请求的客户端。为了将这个票据注入来自我客户端的所有请求,我创建了一个CXF out拦截器,我已将其绑定到SETUP阶段,并负责在传出消息中注入票证。

如果我的拦截器注入的票证已经过期,该服务将抛出一个我希望能够捕获的例外,获得一张新票并用这张新票重复请求以完全抽象票 - 我的其余代码的管理功能。因此,我创建了一个故障拦截器,我已将其绑定到PRE_LOGICAL阶段,并且我能够确定是否已抛出我感兴趣的特定异常类型。但我不确定如何重复请求并返回第二个请求的结果而不是第一个请求的结果。 CXF是否为我提供了这样做的方法?

1 个答案:

答案 0 :(得分:1)

由于我无法通过故障拦截器找到重复请求的方法,因此我最终使用InvocationHandler来控制请求(有效地将我从CXF中获取的代理包装在另一个代理中)。我最终得到的结果如下:

ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
// Configure factory
MyService serviceClient = (MyService) factory.create(MyService.class);
MyService  proxy = (MyService) Proxy.newProxyInstance(
        ServiceInvocationHandler.class.getClassLoader(), 
        new Class[] { MyService.class },
        new ServiceInvocationHandler(serviceClient));

ServiceInvocationHandler的位置是:

public class ServiceInvocationHandler implements InvocationHandler {
    private final Object proxied;
    private SignedTicket ticket;

    public ServiceInvocationHandler(Object proxied) {
        this.proxied = proxied;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        Object retVal = null;
        try {
            // Generate a ticket if the one held locally by this class is null
            // and inject it in the method arguments
            retVal = method.invoke(proxied, args);
        } catch (Throwable t) {
            if (t.getCause() instanceof InvalidTicketException) {
                // Get a fresh ticket and inject it in the method arguments
                retVal = method.invoke(proxied, args);
            }
        }    
        return retVal;
    }
}