汇集JAXWS wsimport服务对象 - 可能吗?

时间:2012-11-07 18:54:37

标签: java web-services jax-ws object-pooling

我们正在使用JAXWS metro客户端与第三方.Net Web服务进行交互。我们需要使用Web服务维护状态。 所以,这是场景。有几个用户应用程序会调用metro客户端,后者又调用.Net Web服务。

我运行了wsimport工具并生成了必要的类。 但是既然,我们必须保持状态,我正在考虑实现服务类的对象池。 这样,每个用户应用程序始终与其正在使用的特定服务对象结合。 所以,流程将是:

COSServiceImpl - > COSServiceFactory实例化/维护COSService(将汇集的wsimport生成的服务类) - > .Net网络服务。

因此,实施如下。有谁有更好的建议?想法?

UserApp.java

COSServiceImpl impl = new COSServiceImpl();
ClaimantAccount claimantAccount = impl.getClaimantAccount(String claimantID)

COSServiceImpl.java

public ClaimantAccount getClaimantAccount(String claimantID) {
ICOSService port = COSServiceFactory.getCOSServicePort();
ClaimantInfo info = port.retrieveClaimantInfo(claimantID);
ClaimantAccount account = new ClaimantAccount();
account.setXXX(info.getXXX);
return account;
}

COSServiceFactory.java

public class COSServiceFactory extends BasePoolableObjectFactory<COSService> {

private static GenericObjectPool<COSService> servicePool = null;

static {
try { 
init();
} catch(Exception e) {
throw new ExceptionInInitializerError(e);
}
}

public static void init() {
servicePool = new GenericObjectPool<COSService>(new COSServiceFactory());
for (int i=0; i < poolSize; i++) {
servicePool.addObject();
}

public COSService makeObject() throws Exception {
URL wsdlURL = null;
service = new COSService(wsdlURL, new QName(nameSpace,localPart) );
return service;
}

private static COSService getCOSService() {
    COSService service = null;
    try {
        service = (COSService) servicePool.borrowObject();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return service;
    }

 public static ICOSService getWebServicePort() {
    ICOSService port = getCOSService().getWSHttpBindingICOSService();
    BindingProvider bindingProvider = (BindingProvider) port;
// Is there any other place to set the request timeout, may be a handler???
    bindingProvider.getRequestContext().put("com.sun.xml.internal.ws.request.timeout", Config.getIntProperty("request.timeout")); 
    return port;
    }

此外,还有其他地方我们可以设置请求超时吗?可以这样做吗?使用上面的代码,我认为我们不会修改端口对象。我还没有测试过这个,但是请求超时属性会起作用吗?

感谢您的评论。

Vijay Ganapathy

1 个答案:

答案 0 :(得分:0)

如果对任何人有帮助,这就是我的理解:

尽管我们可以,但我们不需要池化Service实例。根据我的测试,它似乎工作正常。 我们不需要池服务对象的原因是当我们调用服务类的Service.getPort()方法来创建ICOSService Web服务端口对象时,getPort()方法每次都使用以下命令返回一个新的ICOSService对象。 java.lang.reflect.Proxy的newProxyInstance方法。

相关问题