0

ジャージークライアントがCookieを自動的に処理するためのバックエンドとしてapache httpclientを使用しようとしましたが、これが私のコードです

class ClientHelper {
  public static HttpClientConnectionManager customConnectionManager() throws Exception {
  final SSLContext sslContext = SSLContext.getInstance("SSL");

sslContext.init(null, new TrustManager[]{new X509TrustManager() {
  @Override
  public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
      throws CertificateException {
    System.out.println("========checkClientTrusted=========");
  }

  @Override
  public void checkServerTrusted(X509Certificate[] x509Certificates, String s)
      throws CertificateException {
    System.out.println("========checkServerTrusted==========");
  }

  @Override
  public X509Certificate[] getAcceptedIssuers() {
    return null;
  }
}}, new SecureRandom());

SSLConnectionSocketFactory
    sslConnectionSocketFactory =
    new SSLConnectionSocketFactory(sslContext);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
    .register("https", sslConnectionSocketFactory)
    .register("http", PlainConnectionSocketFactory.getSocketFactory())
    .build();
PoolingHttpClientConnectionManager phcc = new PoolingHttpClientConnectionManager(registry);
return phcc;
}

public static Client createClient() {
  HttpClient apacheClient = null;
  try {
  apacheClient =
      HttpClientBuilder.create().setConnectionManager(customConnectionManager()).build();

} catch (Exception e) {
  e.printStackTrace();
}
Client client = new Client(new ApacheHttpClient4Handler(apacheClient,
                                                        new BasicCookieStore(),
                                                        true));
return client;
  }
  }

ジャージークライアントのバックエンドとしてApache httpclientを使用しようとしています(Cookieを処理するため)

次に、クライアントをテストするための簡単なクラスを作成します。

import com.sun.jersey.api.client.Client;
....

public class ApiTest {

  private static Client client;

  public String getAuth(String username, String passwd) {

  Map<String, String> formParams = new HashMap<String, String>();
  formParams.put("username", String.valueOf(username));
  formParams.put("passwd", String.valueOf(passwd));

try {
  String basePath = "https://xyzhost/login";

  if (client == null) {
    client = ClientHelper.createClient();
    client.addFilter(new LoggingFilter(System.out));
  }

  WebResource webResource = client.resource(basePath);

  ClientResponse response = webResource.type("application/x-www-form-urlencoded").accept("application/json")
      .post(ClientResponse.class, processFormParams(formParams));
  if (response != null) {
    String authRes = response.getEntity(String.class);
    response.close();

    return authRes;
  } else {
    return null;
  }
} catch (Exception ex) {
  ex.printStackTrace();
  return null;
}
 }


 public String getSummary(){

try {
  String basePath = "https://xyzhost/summary";

  if (client == null) {
    client = ClientHelper.createClient();
  }

  WebResource webResource = client
      .resource(basePath);

  ClientResponse response = webResource.type("application/x-www-form-urlencoded").accept("application/json")
      .post(ClientResponse.class, processFormParams(formParams));

  if (response != null) {
    String serviceRes = response.getEntity(String.class);
    response.close();
    return serviceRes;
  } else {
    return null;
  }
  } catch (Exception ex) {
    ex.printStackTrace();
    return null;
  }
}

public static void main(String[] args) throws ApiException {
  String username = "testuser";
  String passwd = "testpasswd";
  AuthApi apiTest = new ApiTest();
  String auth =apiTest.getAuth(username, passwd);
  String reslut1 = apiTest.getSummary();
  String result2 = apiTest.getSummary();
  String result3 = apiTest.getSummary();
  String result4 = apiTest.getSummary();
}
}

そのため、同じクライアントを使用して、同じホストの下でサービスをヒットします。「auth」と「result1」の応答を正常に取得できますが、次の部分でクライアントが「result2」でスタックします

  ClientResponse response = webResource.type("application/x-www-form-urlencoded").accept("application/json")
      .post(ClientResponse.class, processFormParams(formParams));

次の部分を変更しようとしています。

PoolingHttpClientConnectionManager phcc= new PoolingHttpClientConnectionManager(registry);
phcc.setDefaultMaxPerRoute(10);

その後、ApiTest は機能し、スタックしません。デフォルトでは、poolingHttpClientConnectionManager のルートあたりの最大値は 2 であるため、接続に関する問題があると思います。そのため、私の ApiTest は 3 番目のリクエストでスタックします。応答エンティティを消費したため、接続が解放されたと思いますが、

if (response != null) {
    String serviceRes = response.getEntity(String.class);
    response.close();
    return serviceRes;
  }

しかし、まったく機能していないようで、接続が解放されていないようです。

誰でも助けることができますか?感謝!

4

1 に答える 1

0

jersey-client のバージョンを 1.17 から 1.18 に切り替えると、問題が解決しました!

<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-client</artifactId>
  <version>1.18</version>
  <scope>compile</scope>
</dependency>
于 2014-12-02T22:11:44.960 に答える