Client.create()耗时太长。

来源:互联网 发布:优酷客户端网络错误 编辑:程序博客网 时间:2024/04/19 20:38

jersey,一个REST风格服务的开发框架。

现在jersey从1.0升级了到了1.0.1,解决了广大的jerseyer抱怨的初始化Client时的耗时太长的问题。

以前使用jersey Client的时候,必须要先通过Client.create()来创建一下Client,我测试了一下,当我循环创建1000个Client时,耗时基本上达到了1分钟左右。

现在我们完全可以通过spring来注入一个Client,这样我们完全省略了Client.create()一步了。

这个是以前的代码

public Class ClientTestOld(){    public String getHelloWorld(){        String url = "http://localhost/helloworld";        Client c = Client.create();        WebResource r = c.resource(url);        return r.get(String.class);    }}

现在的代码

public Class ClientTestNew(){    @Resource    private Client client;    public String getHelloWorld(){        String url = "http://localhost/helloworld";        WebResource r = client.resource(url);        return r.get(String.class);    }}

当然还需要在applicationContext.xml中加入如下配置:

<bean id="jerseyClient" class="com.sun.jersey.api.client.Client" /><bean id="clientTest" class="com.tianji.www.jersey.client.ClientTestNew"    p:client-ref="jerseyClient"/>

pom.xml中jersey依赖也要改成1.0.1,不过jersey-spring现在还只是1.0.1-SNAPSHOT,要注意哦!