Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ApacheHttpClient线程_Java_Apache_Httpclient - Fatal编程技术网

Java ApacheHttpClient线程

Java ApacheHttpClient线程,java,apache,httpclient,Java,Apache,Httpclient,我正在使用ApacheHttpClient 4.3.6,在一个特定的实例中,我很难使用它 通常,我所有的HttpPost调用都在一个主线程中,并且是连续发生的。然而,有一个实例,一旦发生特定类型的HttpPost,我需要启动一个侧线程来监视它。此侧线程定期尝试连接到服务器,以查看服务器是否仍处于运行状态。如果与服务器的连接中断,我需要在main中中止()连接,这样它就不会在超时发生之前卡住(我可以合法地进行一个可能需要45分钟才能完成的操作) 我在main的开头设置了一个poolghttpcli

我正在使用ApacheHttpClient 4.3.6,在一个特定的实例中,我很难使用它

通常,我所有的HttpPost调用都在一个主线程中,并且是连续发生的。然而,有一个实例,一旦发生特定类型的HttpPost,我需要启动一个侧线程来监视它。此侧线程定期尝试连接到服务器,以查看服务器是否仍处于运行状态。如果与服务器的连接中断,我需要在main中中止()连接,这样它就不会在超时发生之前卡住(我可以合法地进行一个可能需要45分钟才能完成的操作)

我在main的开头设置了一个poolghttpclientconnectionmanager和一个CloseableHttpClient,然后我有一个send方法,main和side线程都使用该方法来设置该方法本地的HttpPost对象。但是,send方法的side线程实例中的client.execute(httpPost)局部变量result以某种方式被分配给send方法的主线程实例中的局部变量result。他们被激怒了。在过去的几个小时里,我到处寻找,但没有找到任何解决办法


编辑:添加了示例代码。必须通过将各种内容更改为使用类的基本变量来混淆公司的内容,所以在此期间请原谅一些拼写错误

类变量:

CloseableHttpClient client;
HttpPost longHttpPost;
boolean diePlz = false;
大体上:

clientPool = new PoolingHttpClientConnectionManager();
clientPool.setMaxTotal(20);
clientPool.setDefaultMaxPerRoute(20);
client = HttpClients.custom().setConnectionManager(clientPool).build();
//Lots of logic here to set up things to be 'send()'ed
发送方法:

private String send(String message, boolean longCall){
    RequestConfig.Builder requestConfig = RequestConfig.custom();
    requestConfig.setConnectTimeout(10000);
    String url = "http://" + ip + ":" + port + "/connector";
    HttpPost httpPost;
    if(longCall){
        httpPost = longHttpPost = new HttpPost(url);
    } else {
        httpPost = new HttpPost(url);
    }
    httpPost.setConfig(requestConfig.build());
    httpPost.setHeader("Content-Type", "application/xml");
    httpPost.setHeader("charset", "UTF-8");
    httpPost.setEntity(getEntity(message)); //getEntity() is a local method that overrides AbstractHttpEntity
    try {
        if(longCall && !threadRunning){
            diePlz = false;
            ConnectionCheck watcher = new ConnectionCheck();
            watcher.start();
        }
         //httpReponse is what seems to get crossed between invocations of send()
        CloseableHttpResponse httpResponse = client.execute(longCall ? longHttpPost : httpPost);
        //more stuff here to work with response
    } catch (Exception e) {
        LOG.error(e, e);
        return null;
    } finally {
        if(longCall){
            longHttpPost.releaseConnection();
        } else {
            httpPost.releaseConnection();
        }
    }
}
主类内的嵌套线程类:

private class ConnectionCheck extends Thread implements Runnable, Serializable {
    @Override
    public void run(){
        threadRunning = true;
        try{Thread.sleep(5000);}catch(Exception e){LOG.error(e, e);}
        boolean registerReturn = register(false); //this calls send()
        if(registerReturn){
            String response = checkStatus(); //this calls send()
            if(response == null){ //if the connection we're watching drops...
                if(longHttpPost != null){
                    longHttpPost.abort(); //...then kill it
                }
            } else {
                while(response != null && !diePlz){
                    try {
                        Thread.sleep(5000);
                    } catch (Exception e){
                        LOG.error(e, e);
                        register(true);
                        threadRunning = false;
                        return;
                    }
                    response = checkStatus();
                    if(response == null){
                        if(longHttpPost != null){
                            longHttpPost.abort();
                        }
                    }
                }
                register(true); //this calls send()
                threadRunning = false;
            }
        } else {
            register(true); //this calls send()
            threadRunning = false;
        }
    }
}

HTTP被设计成一个无状态协议,为什么不试试WebSocket呢?你可以发布一些你正在做的代码方面的示例吗?添加了示例代码。HTTP被设计成一个无状态协议,为什么不试试WebSocket呢?你可以发布一些你正在做的代码方面的示例吗?添加了示例代码。