Thrift
参考
必須
Apache Thrift
本家。Windows版のバイナリがある。Thrift wiki
資料いろいろ。しかしドキュメントは貧弱Thrift: The Missing Guide
唯一まともなマニュアル
その他
バイナリの取得とビルド
現時点(2009/1/1)で、バイナリを配布しているところは無い模様。上記Apache Thriftからもソースコードしか取得できない。 ただし、上記「Thriftを触ってみる」ではバイナリダウンロードをしているようだが、これはFreeBSDバージョンであるらしい。
現時点では、上記「Ubuntu 8.04 で Thrift を試してみた」の通り、Ubuntu(Debian系列?)でビルドするのが最も手早い。
-Redhat系列(CentOS-5.2, Fedora-10)ではどうしてもうまくビルドできなかった。 -Windows上では非常に難しいと思われる。
困難の理由はひとえに、boostというC++ライブラリに依存していることがあげられる。これはとても巨大なライブラリであり、redhat系列のrpmパッケージはきちんとビルドされていないのか(あるいは相性が悪いのか)、Thriftをどうしてもビルドできない。また、Windows用のバイナリも提供されていない模様。
ビルドの方法
ビルドの方法としては、上記の「Installing Apache Thrift on Ubuntu and Leopard」の通りに行えばよいのだが、ただしJava, Ruby, PHP用のライブラリは自動的にはビルド・インストールされない(トップのREADMEをよく読むこと)。 これを行うにはlibディレクトリ以下の各言語用ディレクトリにてビルドを行う。
Javaの場合
READMEに記述があるように、lib/javaディレクトリにてantを動作させればlibthrift.jarが生成される。
フラグ
Usage: thrift [options] file
Options:
-version Print the compiler version
-php Generate PHP output files
-phpi Generate PHP inlined files
-phps Generate PHP server stubs (with -php)
-phpl Generate PHP-lite (with -php)
-phpa Generate PHP with autoload (with -php)
-phpo Generate PHP with object oriented subclasses (with -php)
-xsd Generate XSD output files
-erl Generate Erlang output files
-o dir Set the output directory for gen-* packages
(default: current directory)
-I dir Add a directory to the list of directories
searched for include directives
-rest Generate PHP REST processors (with -php)
-nowarn Suppress all compiler warnings (BAD!)
-strict Strict compiler warnings on
-v[erbose] Verbose mode
-r[ecurse] Also generate included files
-debug Parse debug trace to stdout
--gen STR Generate code with a dynamically-registered generator.
STR has the form language[:key1=val1[,key2,[key3=val3]]].
Keys and values are options passed to the generator.
Many options will not require values.
Available generators (and options):
cpp (C++):
dense: Generate type specifications for the dense protocol.
include_prefix: Use full include paths in generated files.
hs (Haskell):
html (HTML):
java (Java):
beans: Generate bean-style output files.
nocamel: Do not use CamelCase field accessors with beans.
hashcode: Generate quality hashCode methods.
perl (Perl):
py (Python):
new_style: Generate new-style classes.
rb (Ruby):※
- -phpと-phpiはインラインとされるかどうか、処理内容は変わりない模様。
- -phpiにすると、gen-phpiというディレクトリに出力される。
- -phpと-phpsは全く同じ
- -phplは、-phpからProcessorを除去したもの。
- -phpaは、-phpとはかなり異なる。-phpaのみにて作成されるファイルもある。
- -phpoは、-phpのクラスにextends TBase等が付加される。
※
- java:beansは全く効果なし。
- java:hashcodeを指定すると、常に0を返していたhashCodeメソッドがきちんと値を返すようになる。
- java:nocamelも特に効果なし。
} else if (strcmp(arg, "-php") == 0) {
gen_php = true;
} else if (strcmp(arg, "-phpi") == 0) {
gen_phpi = true;
} else if (strcmp(arg, "-phps") == 0) {
gen_php = true;
gen_phps = true;
} else if (strcmp(arg, "-phpl") == 0) {
gen_php = true;
gen_phps = false;
} else if (strcmp(arg, "-phpa") == 0) {
gen_php = true;
gen_phps = false;
gen_phpa = true;
} else if (strcmp(arg, "-phpo") == 0) {
gen_php = true;
gen_phpo = true;
} else if (strcmp(arg, "-rest") == 0) {
gen_rest = true;
サービスのマルチプレックス化
例えばJavaのサーバを作成する場合、
public class Server {
public static class DebugHandler implements Debug.Iface {
public void info(String value) {
System.out.println(value);
}
}
public static void main(String[]args) throws Exception {
DebugHandler debugHandler = new DebugHandler();
Debug.Processor processor = new Debug.Processor(debugHandler);
TServerTransport transport = new TServerSocket(9090);
TServer server = new TSimpleServer(processor, transport);
server.serve();
}
}などとすればよいが、一つのサーバには一つのサービスしか登録できず、一つのサーバには一つのポートが必要である。したがって、上記Debug.IFaceのほかにSample.IFaceやTest.IFaceのサーバを作成する場合は、それぞれのプロセッサ・トランスポート・ポートが必要になる。
これを避けるために、https://issues.apache.org/jira/browse/THRIFT-66というパッチがリリースされている。これを用いると、一つのポートで複数のサービスを提供できるらしいが、ただしクライアント側も変更しなければならない。
現在はJava用しか提供されておらず、また正式なリリースではなくテスト的なものであるため、とりあえず避けておくのが無難と思われる。
Thriftのスレッドセーフティについて
