@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> list = client.getOutInterceptors();
        Interceptor 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 tlClient = new ThreadLocal();

    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> list = client.getOutInterceptors();
        Interceptor 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()方法的作用,请大佬们指教!

你可能感兴趣的:(@Scheduled + WebService定时任务运行一段时间后,无故停止)