不执行来自Java-Application on HCP的发布请求

时间:2017-02-19 19:01:56

标签: apache tomcat7 apache-httpclient-4.x onenote-api hana-cloud-platform

我遇到了来自Java-Application的httpPost问题,该问题在Tomcat7-Container中的HCP-Trial-Account上运行。我使用HttpClient 4.5.3。

代码在我的本地Tomcat7-Server上运行并运行正常。但是,如果将其部署到HCP则会出现问题。

public static Notebook getAllNotebooks(String code, String redirectUri) throws IOException, URISyntaxException{
        ClassLoader classLoader = Connection.class.getClassLoader();
        URL resource = classLoader.getResource("org/apache/http/impl/client/HttpClientBuilder.class");
        String returnUri = "https://login.live.com/oauth20_token.srf";
        HttpPost tokenRequest = new HttpPost(returnUri);
        HttpClient client = new DefaultHttpClient();//HttpClientBuilder.create().build(); //Exception: http://stackoverflow.com/questions/22330848/httpclient-example-exception-in-thread-main-java-lang-nosuchfielderror-inst

        tokenRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
        tokenRequest.setEntity(new UrlEncodedFormEntity(Connection.getParametersForURLBody(code, redirectUri), Consts.UTF_8));
        tokenRequest.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0");
        HttpResponse tokenResponse = client.execute(tokenRequest); //Here it gets stuck

执行tokenRequest时会出现问题。应用程序卡住了,tokenRequest永远运行。

以下是永远运行的线程,我可以在调试模式下看到:

SAP JVM Debug Target    
Daemon Thread [NioBlockingSelector.BlockPoller-1] (Running) 
Daemon Thread [NioBlockingSelector.BlockPoller-2] (Running) 
Daemon Thread [RMI TCP Connection(1)-127.0.0.1] (Running)   
Thread [Timer-0] (Running)  
Daemon Thread [RMI TCP Connection(2)-10.117.35.76] (Running)    
Thread [pool-1-thread-1] (Running)  
Thread [Timer-1] (Running)  
Daemon Thread [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (Running)    
Daemon Thread [JCoTimeoutChecker] (Running) 
Daemon Thread [http-bio-8001-Acceptor-0] (Running)  
Daemon Thread [http-bio-8001-AsyncTimeout] (Running)    
Daemon Thread [http-nio-127.0.0.1-9001-ClientPoller-0] (Running)    
Daemon Thread [http-nio-127.0.0.1-9001-Acceptor-0] (Running)    
Daemon Thread [http-nio-8041-ClientPoller-0] (Running)  
Daemon Thread [http-nio-8041-Acceptor-0] (Running)  
Daemon Thread [ajp-bio-8009-Acceptor-0] (Running)   
Daemon Thread [ajp-bio-8009-AsyncTimeout] (Running) 
Daemon Thread [RMI TCP Connection(10)-127.0.0.1] (Running)  
Daemon Thread [RMI TCP Connection(11)-10.117.10.34] (Running)   
Thread [main] (Running) 
Daemon Thread [http-nio-8041-exec-1] (Running)  
Daemon Thread [http-nio-8041-exec-2] (Running)  
Daemon Thread [http-nio-8041-exec-3] (Running)  
Daemon Thread [http-nio-8041-exec-4] (Running)  
Daemon Thread [http-nio-8041-exec-5] (Running)  
Thread [pool-2-thread-1] (Running)  
Daemon Thread [http-nio-8041-exec-6] (Stepping) 
Daemon Thread [http-nio-8041-exec-7] (Running)  
Daemon Thread [http-nio-8041-exec-8] (Running)  
Daemon Thread [http-nio-8041-exec-9] (Running)  
Daemon Thread [http-nio-8041-exec-10] (Running) 
Daemon Thread [RMI TCP Connection(idle)] (Running)  
Daemon Thread [RMI TCP Connection(idle)] (Running)  

此时程序陷入困境。

我不知道该做什么,并且非常感谢一些提示和帮助:)。

问候 Maverin

2 个答案:

答案 0 :(得分:0)

通常,您应该使用连接服务,以便从HCP部署的应用程序中访问Internet资源。我想尝试直接这样做会导致应用冻结/崩溃。可能也是我想念的其他东西。

您可以在官方文档中找到有关此操作的信息:help.sap.com

简而言之,您必须:

  • 在指定网站上创建一个目的地(在您的HCP帐户中或直接在Java应用程序中)。
  • 由于您使用HTTPS,因此您很可能必须将远程主机的证书导入信任存储区(docu link)。
  • 在web.xml中声明连接配置。
  • 使用JNDI(即InitialContext)获取java代码中的连接配置。然后,可以使用此配置来获取指向远程资源的URL。您可以使用此URL打开直接连接或将其传递给HttpClient(URL类具有toURI方法)。

答案 1 :(得分:0)

我也遇到过这个问题,最后,我找到了一个适用于Tomcat8的解决方案。关键是与构建器建立连接,然后使用useSystemProperties()。这样您就可以获得HCP所需的代理属性。连接管理器的额外内容并不是严格需要的,但它有助于提高性能。 param-syncro是遗留物。

org.apache.http.impl.conn.PoolingHttpClientConnectionManager cm
public static HttpClient getHttpClient(){
    if(cm == null){
        synchronized(params){
            cm = new PoolingHttpClientConnectionManager();
            // Increase max total connection to 200
            cm.setMaxTotal(200);
            // Increase default max connection per route to 20
            cm.setDefaultMaxPerRoute(200);
        }
    }
    org.apache.http.impl.client.CloseableHttpClient httpClient = HttpClients.custom()
            .setConnectionManager(cm)
            .useSystemProperties()
            .build();
    return httpClient;
}