Java程序卡在TTransport Transport = new THttpClient ("this.host.com"上)

标签 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) 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,或者同时使用这两种措施。

关于Java程序卡在TTransport Transport = new THttpClient ("this.host.com"上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26352510/

相关文章:

c++ - 在Thrift中,有没有一种方法可以避免生成C++ setter?

serialization - 支持和类型的高性能对象序列化库

c++ - 为什么 thrift TBinaryProtocol 读取 recv 数据比大小 + 内容更复杂

Java开源捕获包含滚动的页面截图

java - "@annotations must be on separate line"的 Checkstyle 规则

java - Java 中的正则表达式模式

java - Apache 节俭,Java : Object Data Types

java - 如何让Java访问Cassandra 1.0.10

java - OpenJdk 初始启动时间很慢

java - 为什么不让 StringBuilder 和 StringBuffer 实现一个公共(public)接口(interface)呢?