日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

          歡迎訪問 生活随笔!

          生活随笔

          當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

          编程问答

          java httpclient单例_java-8 – CloseableHttpClient.execute每隔几周就会冻结一次,尽管超时...

          發(fā)布時間:2023/12/31 编程问答 38 豆豆
          生活随笔 收集整理的這篇文章主要介紹了 java httpclient单例_java-8 – CloseableHttpClient.execute每隔几周就会冻结一次,尽管超时... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

          我們有一個groovy單例,使用PoolingHttpClientConnectionManager(httpclient:4.3.6),池大小為200,以處理與搜索服務非常高的并發(fā)連接并處理xml響應.

          盡管已經指定了超時,但它每個月凍結約一次,但在其余時間內運行良好.

          下面的groovy單身.方法retrieveInputFromURL似乎阻止在client.execute(get);

          @Singleton(strict=false)

          class StreamManagerUtil {

          // Instantiate once and cache for lifetime of Signleton class

          private static PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();

          private static CloseableHttpClient client;

          private static final IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(connManager);

          private int warningLimit;

          private int readTimeout;

          private int connectionTimeout;

          private int connectionFetchTimeout;

          private int poolSize;

          private int routeSize;

          PropertyManager propertyManager = PropertyManagerFactory.getInstance().getPropertyManager("sebe.properties")

          StreamManagerUtil() {

          // Initialize all instance variables in singleton from properties file

          readTimeout = 6

          connectionTimeout = 6

          connectionFetchTimeout =6

          // Pooling

          poolSize = 200

          routeSize = 50

          // Connection pool size and number of routes to cache

          connManager.setMaxTotal(poolSize);

          connManager.setDefaultMaxPerRoute(routeSize);

          // ConnectTimeout : time to establish connection with GSA

          // ConnectionRequestTimeout : time to get connection from pool

          // SocketTimeout : waiting for packets form GSA

          RequestConfig config = RequestConfig.custom()

          .setConnectTimeout(connectionTimeout * 1000)

          .setConnectionRequestTimeout(connectionFetchTimeout * 1000)

          .setSocketTimeout(readTimeout * 1000).build();

          // Keep alive for 5 seconds if server does not have keep alive header

          ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {

          @Override

          public long getKeepAliveDuration(HttpResponse response, HttpContext context) {

          HeaderElementIterator it = new BasicHeaderElementIterator

          (response.headerIterator(HTTP.CONN_KEEP_ALIVE));

          while (it.hasNext()) {

          HeaderElement he = it.nextElement();

          String param = he.getName();

          String value = he.getValue();

          if (value != null && param.equalsIgnoreCase

          ("timeout")) {

          return Long.parseLong(value) * 1000;

          }

          }

          return 5 * 1000;

          }

          };

          // Close all connection older than 5 seconds. Run as separate thread.

          staleMonitor.start();

          staleMonitor.join(1000);

          client = HttpClients.custom().setDefaultRequestConfig(config).setKeepAliveStrategy(myStrategy).setConnectionManager(connManager).build();

          }

          private retrieveInputFromURL (String categoryUrl, String xForwFor, boolean isXml) throws Exception {

          URL url = new URL( categoryUrl );

          GPathResult searchResponse = null

          InputStream inputStream = null

          HttpResponse response;

          HttpGet get;

          try {

          long startTime = System.nanoTime();

          get = new HttpGet(categoryUrl);

          response = client.execute(get);

          int resCode = response.getStatusLine().getStatusCode();

          if (xForwFor != null) {

          get.setHeader("X-Forwarded-For", xForwFor)

          }

          if (resCode == HttpStatus.SC_OK) {

          if (isXml) {

          extractXmlString(response)

          } else {

          StringBuffer buffer = buildStringFromResponse(response)

          return buffer.toString();

          }

          }

          }

          catch (Exception e)

          {

          throw e;

          }

          finally {

          // Release connection back to pool

          if (response != null) {

          EntityUtils.consume(response.getEntity());

          }

          }

          }

          private extractXmlString(HttpResponse response) {

          InputStream inputStream = response.getEntity().getContent()

          XmlSlurper slurper = new XmlSlurper()

          slurper.setFeature("http://xml.org/sax/features/validation", false)

          slurper.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false)

          slurper.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false)

          slurper.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)

          return slurper.parse(inputStream)

          }

          private StringBuffer buildStringFromResponse(HttpResponse response) {

          StringBuffer buffer= new StringBuffer();

          BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

          String line = "";

          while ((line = rd.readLine()) != null) {

          buffer.append(line);

          System.out.println(line);

          }

          return buffer

          }

          public class IdleConnectionMonitorThread extends Thread {

          private final HttpClientConnectionManager connMgr;

          private volatile boolean shutdown;

          public IdleConnectionMonitorThread

          (PoolingHttpClientConnectionManager connMgr) {

          super();

          this.connMgr = connMgr;

          }

          @Override

          public void run() {

          try {

          while (!shutdown) {

          synchronized (this) {

          wait(5000);

          connMgr.closeExpiredConnections();

          connMgr.closeIdleConnections(10, TimeUnit.SECONDS);

          }

          }

          } catch (InterruptedException ex) {

          // Ignore

          }

          }

          public void shutdown() {

          shutdown = true;

          synchronized (this) {

          notifyAll();

          }

          }

          }

          我還發(fā)現(xiàn)在日志中發(fā)現(xiàn)這一點,導致我相信它在等待響應數(shù)據時發(fā)生

          java.net.SocketTimeoutException:通過java.net.SocketInputStream.read(SocketInputStream.java:150)在java.net.SocketInputStream.read(SocketInputStream.java:121)上的java.net.SocketInputStream.socketRead0(Native Method)讀取超時)在sun.security.ssl.InputRecord.readFully(InputRecord.java:465)

          到目前為止的調查結果:

          問題

          >這可能是同步問題嗎?從我的理解,即使單線程被多個線程訪問,唯一的共享數(shù)據是緩存的CloseableHttpClient

          >這個代碼有什么其他根本的錯誤,可能導致這種行為的方法?

          總結

          以上是生活随笔為你收集整理的java httpclient单例_java-8 – CloseableHttpClient.execute每隔几周就会冻结一次,尽管超时...的全部內容,希望文章能夠幫你解決所遇到的問題。

          如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。