Java程序挂起在TTransport transport = new THttpClient(“this.host.com”)

时间:2014-10-14 04:08:39

标签: java thrift

我正在编写一个可以从外部网站获取输入并处理它的Thrift应用程序。但是,该计划悬而未决:

TTransport transport = new THttpClient("this.host.com");

我不确定为什么会这样。它没有给我任何错误信息,但它也没有向前发展。当我收到来自调用buildModel()的客户端的请求时,它就会挂起。这是我的代码:

private void buildModel() throws UnknownHostException {
    // Map of user preferences by Mahout user id
    FastByIDMap<Collection<Preference>> userIDPrefMap = new FastByIDMap<Collection<Preference>>();
    System.out.println("Building model");

    try {
        TTransport transport = new THttpClient(this.host);
        TProtocol protocol = new  TBinaryProtocol(transport);
        MyCustomDatabase.Client client = new MyCustomDatabase.Client(protocol);

        ConnectionParams con_params = new ConnectionParams();
        con_params.setUser(this.username);
        con_params.setPassword(this.password);
        Connection con = client.open_connection(con_params);

        ResultSet res = client.execute_sql(con, "select * from " + this.database + "." + this.tableName, null);

        for (Tuple t : res.getTuples()) {
            List<ByteBuffer> cells = t.getCells();
            int userID = Integer.parseInt(new String(cells.get(0).array()));
            int itemID = Integer.parseInt(new String(cells.get(1).array()));
            int ratingValue = Integer.parseInt(new String(cells.get(2).array()));

            Collection<Preference> userPrefs = userIDPrefMap.get(userID);
            if (userPrefs == null) {
                userPrefs = Lists.newArrayListWithCapacity(2);
                userIDPrefMap.put(userID, userPrefs);
            }
            userPrefs.add(new GenericPreference(userID, itemID, ratingValue));
        }
    } catch(Exception e) {
        e.printStackTrace();
    }

    this.delegate = new GenericDataModel(GenericDataModel.toDataMap(userIDPrefMap, true));
}

任何帮助都非常感谢!

1 个答案:

答案 0 :(得分:0)

很难确切地说明问题是什么,但有些事情要注意:

1)new THttpClient(this.host)应该使用URI字符串而不是主机名。在Thrift Java库中,这是您正在调用的构造函数:

public THttpClient(String url) throws TTransportException {
    try {
      url_ = new URL(url);
      this.client = null;
      this.host = null;
    } catch (IOException iox) {
      throw new TTransportException(iox);
    }
}

如果您只是传递主机名而不是URI,则可能无法获得预期的结果。

2)在THttpClient类的javadoc中,无论如何都建议使用Apache HttpClient构造函数而不是默认的HttpURLConnection,以获得更好的性能并避免在高负载下耗尽开放文件描述符限制。

我建议尝试将有效的URI传递给构造函数,如果不起作用,请尝试使用HttpClient,或者使用这两种方法。