@Scheduled + WebService定时任务运行一段时间后,无故停止

前段时间做了一个空气质量定时更新的功能,发现运行一天后定时任务停止,后台不报错
贴出webservice代码

public class DustServiceClient {

    JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
    
    public Object fetchRealTimeData(String id){
        Client client = clientFactory.createClient(wsdl);
        Client client = tlClient.get();
        HTTPConduit http = (HTTPConduit) client.getConduit();
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        httpClientPolicy.setConnectionTimeout(5000);
        httpClientPolicy.setReceiveTimeout(5000);
        httpClientPolicy.setAllowChunking(false);
        http.setClient(httpClientPolicy);

        String username = "username";
        String password = "password";
        List<Interceptor<? extends Message>> list = client.getOutInterceptors();
        Interceptor<? extends Message> in = new AddUserInterceptor(username, password);
        list.add(in);
        list.add(new LoggingOutInterceptor());
        try {
            Object[] result = client.invoke("fetchRealTimeData", id);
            return result[0];
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.PARAM_ERR;
        }
    }
}

后台设置一分钟执行一次更新
@Scheduled(fixedRate=5*60000)
每次大概有一百多次的webservice调用,执行一天后基本就停止了,后台也没有错误,查了好多原因后才发现原来Client在用完后还需要销毁

public class DustServiceClient {

    JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();

    private static ThreadLocal<Client> tlClient = new ThreadLocal<Client>();

    public void initClient() {
        tlClient.set(clientFactory.createClient(wsdl);
        Client client = tlClient.get();
        HTTPConduit http = (HTTPConduit) client.getConduit();
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        httpClientPolicy.setConnectionTimeout(5000);
        httpClientPolicy.setReceiveTimeout(5000);
        httpClientPolicy.setAllowChunking(false);
        http.setClient(httpClientPolicy);

        String username = "username";
        String password = "password";
        List<Interceptor<? extends Message>> list = client.getOutInterceptors();
        Interceptor<? extends Message> in = new AddUserInterceptor(username, password);
        list.add(in);
        list.add(new LoggingOutInterceptor());
    }
    
    public void destory() {
        tlClient.get().destroy();
    }
    
    
    public Object fetchRealTimeData(String id){
        
        try {
            Object[] result = tlClient.get().invoke("fetchRealTimeData", id);
            return result[0];
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.PARAM_ERR;
        }
        
    }
}

没有深入了解下destory()方法的作用,请大佬们指教!

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,200评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,695评论 25 709
  • 我觉得上小学简直太好了,因为中午可以回家,可以认识很多新的小朋友,还可以学习到很多的知识,今天我和小朋友玩的...
    刘乾日记阅读 1,688评论 0 2
  • 打开一本书,就走进了一个故事,跟着他们欢欢喜喜、苦乐悲痛,这本书里,围绕着艾丽丝岛唯一的一家书店,我们走进了书店老...
    四方fancy阅读 2,444评论 1 3
  • 独行于长街的片段 秋风里易碎的感官 爱是短暂美满 还是一生欺瞒 誓海盟山 不过都是狂言斗胆 何处是阑珊
    段童阅读 3,034评论 0 2

友情链接更多精彩内容