如何在javax.ws.rs.client中为客户端重用java中的tcp会话

如何在javax.ws.rs.client中为客户端重用java中的tcp会话,java,spring,web-services,tcp,Java,Spring,Web Services,Tcp,我尝试在https服务上重复发出10个请求,我能够做到,但是我使用了10个tcp连接。 我想为10个请求重用在开始时创建的相同tcp连接,如何为下面的代码做到这一点 这里的客户机是javax.ws.rs.client java代码: static SslConfigurator sslConfig = SslConfigurator .newInstance() .securityProtocol("TLS") .keyStoreFile("/path") .keyStorePa

我尝试在https服务上重复发出10个请求,我能够做到,但是我使用了10个tcp连接。 我想为10个请求重用在开始时创建的相同tcp连接,如何为下面的代码做到这一点

这里的客户机是javax.ws.rs.client

java代码:

static SslConfigurator sslConfig = SslConfigurator
  .newInstance()
  .securityProtocol("TLS")
  .keyStoreFile("/path")
  .keyStorePassword("password")
  .keyStoreType("JKS")
  .trustStoreFile("/path");

static SSLContext sslCtx = sslConfig.createSSLContext();
static Client client = ClientBuilder.newBuilder().sslContext(sslCtx).build();            

for (i = 0; i < 10; i++) {
  Response response = client.target(target).path(path)
    .request(MediaType.APPLICATION_JSON)
    .post(Entity.entity(jsonRequest.toString(), MediaType.APPLICATION_JSON));
}

只是猜测,但你可以试试这个:

Request req =  (Request) client.target(target).path(path).request(MediaType.APPLICATION_JSON);
for (i = 0; i < 10; i++ {
  Response response = req.post(
    Entity.entity(jsonRequest.toString(), MediaType.APPLICATION_JSON));
}

只是猜测,但你可以试试这个:

Request req =  (Request) client.target(target).path(path).request(MediaType.APPLICATION_JSON);
for (i = 0; i < 10; i++ {
  Response response = req.post(
    Entity.entity(jsonRequest.toString(), MediaType.APPLICATION_JSON));
}

有一篇关于它的好文章:但实际上它现在已经过时了。当前Jersey客户端的示例:

static PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
static Client client;
static{
    connectionManager.setMaxTotal(100);
    connectionManager.setDefaultMaxPerRoute(10);
    //you can provide per route settings
    //connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost("localhost")), 40);

    SslConfigurator sslConfig = SslConfigurator.newInstance()
            .securityProtocol("TLS")
            .keyStoreFile("/path")
            .keyStorePassword("password")
            .keyStoreType("JKS")
            .trustStoreFile("/path");

    ClientConfig clientConfig = new ClientConfig();
    clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
    clientConfig.connectorProvider(new ApacheConnectorProvider());
    client = ClientBuilder.newBuilder().withConfig(clientConfig).sslContext(sslConfig.createSSLContext()).build();
}
您还应该调用response.close;将连接标记为空闲

默认情况下,您使用BasicHttpClientConnectionManager。它有以下注意事项:此连接管理器将努力为具有相同HttpRoute路由的后续请求重用连接。但是,如果持久连接的路由与连接请求的路由不匹配,它将关闭现有连接并为给定路由打开它


所以在简单的情况下,默认情况下会重用连接。

有关于它的好文章:但实际上它现在已经过时了。当前Jersey客户端的示例:

static PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
static Client client;
static{
    connectionManager.setMaxTotal(100);
    connectionManager.setDefaultMaxPerRoute(10);
    //you can provide per route settings
    //connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost("localhost")), 40);

    SslConfigurator sslConfig = SslConfigurator.newInstance()
            .securityProtocol("TLS")
            .keyStoreFile("/path")
            .keyStorePassword("password")
            .keyStoreType("JKS")
            .trustStoreFile("/path");

    ClientConfig clientConfig = new ClientConfig();
    clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
    clientConfig.connectorProvider(new ApacheConnectorProvider());
    client = ClientBuilder.newBuilder().withConfig(clientConfig).sslContext(sslConfig.createSSLContext()).build();
}
您还应该调用response.close;将连接标记为空闲

默认情况下,您使用BasicHttpClientConnectionManager。它有以下注意事项:此连接管理器将努力为具有相同HttpRoute路由的后续请求重用连接。但是,如果持久连接的路由与连接请求的路由不匹配,它将关闭现有连接并为给定路由打开它



因此,在简单的情况下,默认情况下会重用连接。

getting org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder无法转换为javax.ws.rs.core.Request您必须将其转换为Request。我更新了上面的代码。org.glassfish.jersey.client.jersey调用$Builder不能转换为javax.ws.rs.core.Request for Request req=Request client.targettarget.pathpath.requestMediaType.APPLICATION_JSON;您指的是哪个请求导入?我使用了Invocation.Builder req=client.targettarget.pathpath.requestMediaType.APPLICATION_JSON;response=req.post Entity.entityjsonRequest.toString,MediaType.APPLICATION\u JSON;然后,我没有发现任何错误,但在wireshark捕获中,它显示了建立10个连接Getting org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder无法转换为javax.ws.rs.core.Request您必须转换为Request。我更新了上面的代码。org.glassfish.jersey.client.jersey调用$Builder不能转换为javax.ws.rs.core.Request for Request req=Request client.targettarget.pathpath.requestMediaType.APPLICATION_JSON;您指的是哪个请求导入?我使用了Invocation.Builder req=client.targettarget.pathpath.requestMediaType.APPLICATION_JSON;response=req.post Entity.entityjsonRequest.toString,MediaType.APPLICATION\u JSON;然后我没有收到任何错误,但在wireshark capture中,它显示了建立10个连接我无法找到有效的认证路径如何使用客户端打开连接您的代码示例应在不做任何更改的情况下工作。我的示例:Response=client.targethttp://localhost:8080.path/ .requestMediaType.APPLICATION_JSON.postEntity.entity{},MediaType.APPLICATION_JSON;是的,我没有改变它,是因为在客户端添加了池配置吗?org.glassfish.jersey.apache.connector.ApacheClientProperties for apache客户端属性我找不到有效的认证路径如何使用客户端打开连接您的代码示例应该可以在没有任何更改的情况下工作。我的示例:Response=client.targethttp://localhost:8080.path/ .requestMediaType.APPLICATION_JSON.postEntity.entity{},MediaType.APPLICATION_JSON;是的,我没有改变它,是因为向客户端添加池配置吗?org.glassfish.jersey.apache.connector.ApacheClientProperties for apache客户端属性