java httpclient释放_java – PoolingHttpClientConnectionManager不释放连接

我使用

Spring来实现以下几点:

在服务器上,我通过XML格式的REST接口接收数据.我想将数据转换成JSON并将其POST到另一台服务器.我的代码(我删除了一些敏感的类名/ URL,以避免雇主的愤怒)看起来像这样:@H_502_3@

主/配置类:@H_502_3@package stateservice;

import org.apache.http.HttpHost;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.Bean;

import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;

import org.springframework.web.client.RestTemplate;

@SpringBootApplication

public class App {

Logger log = LoggerFactory.getLogger(App.class);

public static void main(String[] args) {

System.out.println("Start!");

SpringApplication.run(StateServiceApplication.class,args);

System.out.println("End!");

}

@Bean

public RestTemplate restTemplate() {

log.trace("restTemplate()");

HttpHost proxy = new HttpHost("proxy_url",8080);

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

// Increase max total connection to 200

cm.setMaxTotal(200);

cm.setDefaultMaxPerRoute(50);

RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build();

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

httpClientBuilder.setDefaultRequestConfig(requestConfig);

httpClientBuilder.setConnectionManager(cm);

HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(

httpClientBuilder.build());

return new RestTemplate(requestFactory);

}

}

代表RESTful接口的类:@H_502_3@package stateservice;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

import foo.bar.XmlData

@RestController

public class StateController {

private static Logger log = LoggerFactory.getLogger(DataController.class);

@Autowired

ForwarderService forwarder;

@RequestMapping(value = "/data",method = RequestMethod.POST)

public String postState(@RequestBody XmlData data) {

forwarder.forward(data);

return "Done!";

}

}

最后,货代:@H_502_3@package stateservice;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpEntity;

import org.springframework.http.HttpHeaders;

import org.springframework.http.MediaType;

import org.springframework.http.ResponseEntity;

import org.springframework.scheduling.annotation.Async;

import org.springframework.stereotype.Service;

import org.springframework.web.client.RestTemplate;

import foo.bar.Converter;

import foo.bar.XmlData;

@Service

public class ForwarderService {

private static Logger log = LoggerFactory.getLogger(ForwarderService.class);

String uri = "forward_uri";

@Autowired

RestTemplate restTemplate;

@Async

public String forward(XmlData data) {

log.trace("forward(...) - start");

String json = Converter.convert(data);

HttpHeaders headers = new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_JSON);

ResponseEntity response = restTemplate.postForEntity(uri,new HttpEntity(json,headers),String.class);

// responseEntity.getBody();

// log.trace(responseEntity.toString());

log.trace("forward(...) - end");

return response.getBody();

}

}

然而,连接管理器很少似乎释放重新使用的连接,此外,系统会在CLOSE_WAIT状态(可以使用netstat看到)中连接.池中的所有连接都被租用,但未被释放,并且一旦CLOSE_WAIT状态的连接数达到ulimit,我会收到’太多的open file’异常@H_502_3@

由于代码的多线程性质,我怀疑套接字不能被关闭/连接被释放,因为一些其他线程是无法阻止它们的.@H_502_3@

我真的很感激任何帮助或任何提示,你可以帮我解决问题.@H_502_3@

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值