Skip to content

Instantly share code, notes, and snippets.

@wataru420
Created September 3, 2012 03:34
Show Gist options
  • Save wataru420/3606572 to your computer and use it in GitHub Desktop.
Save wataru420/3606572 to your computer and use it in GitHub Desktop.
finagle-thriftをJavaで動かすSample
namespace java jp.hoge.finagleExample.thrift
service Hello {
string hi();
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jp.hoge</groupId>
<artifactId>finagle-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.twitter</groupId>
<artifactId>finagle-core</artifactId>
<version>5.3.8</version>
</dependency>
<dependency>
<groupId>com.twitter</groupId>
<artifactId>finagle-thrift</artifactId>
<version>5.3.8</version>
</dependency>
<dependency>
<groupId>com.twitter</groupId>
<artifactId>finagle-stream</artifactId>
<version>5.3.8</version>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.8.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.twitter</groupId>
<artifactId>maven-finagle-thrift-plugin</artifactId>
<version>0.0.2</version>
<configuration>
<thriftGenerators>
<thriftGenerator>finagle</thriftGenerator>
</thriftGenerators>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>twitter-repo</id>
<name>twitter-repo</name>
<layout>default</layout>
<url>http://maven.twttr.com</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>twitter-repo</id>
<name>twitter-repo</name>
<layout>default</layout>
<url>http://maven.twttr.com</url>
</pluginRepository>
</pluginRepositories>
</project>
public class ThriftClient {
public static void main(String[] args) {
new com.twitter.finagle.thrift.thrift.ClientId();
//Thrift プロトコルベースのリクエストを送信するクライアントを生成
Service<ThriftClientRequest, byte[]> service = ClientBuilder
.safeBuild(ClientBuilder.get()
.hosts(new InetSocketAddress(8080))
.codec(ThriftClientFramedCodec.get())
.hostConnectionLimit(1));
Hello.ServiceIface client = new Hello.ServiceToClient(service,
new TBinaryProtocol.Factory());
//レスポンスが届いたら onSuccess コールバックが実行され、結果が出力さる
client.hi().addEventListener(new FutureEventListener<String>() {
public void onSuccess(String s) {
System.out.println(s);
}
public void onFailure(Throwable t) {
System.out.println("Exception! " + t.toString());
}
});
}
}
public class ThriftServer {
public static void main(String[] args) {
//Thrift サービスインタフェースを実装した Thrift プロセッサの作成
Hello.ServiceIface processor = new Hello.ServiceIface() {
//サービスインターフェイスの実行
@Override
public Future<String> hi() {
return Future.value("hi");
}
};
//Thrift プロセッサから Finagle サービスへのアダプタ作成
ServerBuilder.safeBuild(
//サーバの生成とサービスへの関連付け
new Hello.Service(processor, new TBinaryProtocol.Factory()),
ServerBuilder.get().name("HelloService")
.codec(ThriftServerFramedCodec.get())
.bindTo(new InetSocketAddress(8080)));
System.out.println("server started...");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment