Thrift(3)IDL language/Structure and Generate Base Codes
RMI use java seserial to transfer the java object data.
Thrift, same interface and implementation, but it use socket to transfer the data, and we use Object ----> String ----> Object.
So thrift supports many languages.
thrift also can work like this, java object ---> thrift object ---> php object
1. Thrift Introduction
Thrift is IDL(interface definition language) implementation. It works something like CORBA.
Client Server
IDL Stub IDL Skeleton /POA
ORB on Client Side ORB on server side
------> IIOP ----->
Thrift is something like XML-RPC + Java-to-IDL + Serialization Tool. It consist of TProtocol and TTransports. The structure is as follow:
Client Server
Your code Your code
FooService.Client FooService.Proccessor (Generated Code)
Foo.write()/read() Foo.read()/write() (Generated Code)
TProtocol TProtocol
TTransport TTransport
... ...
Underlying I/O Underlying I/O
2. Data Type and Protocol
Base Types
bool : (true or false), one byte
byte
i16
i32
i64
double:
string encoding agnostic text or binary string
Struct
Containers --- List Set Map
list<t1> type is t1, and the collection is ordered, the elements of the collection can be reduplicate.
set<t1> type is t1, not ordered, no reduplicate.
map<t1,t2> key is t1, value is t2
Exception
Services
Comments
# this is a valid comment
/*
* multiple comments
*/
// c++/Java style single-line comments
TProtocol
There are 2 main types, text and binary.
TBinaryProtocol
TCompactProtocol
TJSONProtocol
TSimpleJSONProtocol
TDebugProtocol this is for developing, text.
TTransport
TSocket I/O
TFramedTransport NIO
TFileTransport No java implementation
TMemoryTransport memory I/O, ByteArrayOutputStream
TZlibTransport zlib, No java implementation
Server Types
TSimpleServer single thread I/O
TThreadPoolServer multiple threads I/O
TNonblockingServer multiple threads NIO
3. First Example
java com.sillycat.easytalker.plugins.thrift.gen.code # package name
struct Blog { # POJO
1: string topic
2: binary content
3: i64 createdTime
4: string id
5: string ipAddress
6: map<string,string> props
}
service BlogService { # interface class
string createBlog(1:Blog blog)
list<string> batchCreateBlog(1:list<Blog> blogs)
string deleteBlog(1:string id)
list<Blog> listAll()
Blog getOne(1:string id)
string updateBlog(1:Blog blog)
}
>thrift.exe -out d:\work\easy\easytalker\src\main\java\ -r -gen java BlogService.thrift
Warning Message:
[WARNING:D:/work/easy/easytalker/scripts/thrift/BlogService.thrift:14] No field key specified for blog, resulting protocol may have conflicts or not be backwards compatible!
[WARNING:D:/work/easy/easytalker/scripts/thrift/BlogService.thrift:16] No field key specified for id, resulting protocol may have conflicts or not be backwards compatible!
Solution:
Because we need the number with integer before the properties of struct and parameters of methods.
struct Blog { # POJO
1: string topic
2: binary content
3: i64 createdTime
4: string id
5: string ipAddress
6: map<string,string> props
}
and
list<string> batchCreateBlog(1:list<Blog> blogs)
Once the java code is generate, add this lib to my pom.xml
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.8.0</version>
</dependency>
There are lot of warning message in generated codes.
I tried the following command, but it does not help.
>thrift.exe -out d:\work\easy\easytalker\src\main\java\ -r -gen java:java5 BlogService.thrift
references:
http://gemantic.iteye.com/blog/1199214
http://www.cnblogs.com/birdshover/archive/2010/03/16/1687301.html
http://www.javabloger.com/article/thrift-java-code-example.html
http://www.javabloger.com/article/apache-thrift-architecture.html
http://yangfanchao.iteye.com/blog/1271737
http://thrift.apache.org/docs/idl/
http://thrift.apache.org/docs/types/
http://www.cnblogs.com/tianhuilove/archive/2011/09/05/2167669.html
http://svn.apache.org/repos/asf/thrift/trunk/test/ThriftTest.thrift
http://li3huo.com/2011/10/creating-a-thrift-service-step-by-step/
RMI use java seserial to transfer the java object data.
Thrift, same interface and implementation, but it use socket to transfer the data, and we use Object ----> String ----> Object.
So thrift supports many languages.
thrift also can work like this, java object ---> thrift object ---> php object
1. Thrift Introduction
Thrift is IDL(interface definition language) implementation. It works something like CORBA.
Client Server
IDL Stub IDL Skeleton /POA
ORB on Client Side ORB on server side
------> IIOP ----->
Thrift is something like XML-RPC + Java-to-IDL + Serialization Tool. It consist of TProtocol and TTransports. The structure is as follow:
Client Server
Your code Your code
FooService.Client FooService.Proccessor (Generated Code)
Foo.write()/read() Foo.read()/write() (Generated Code)
TProtocol TProtocol
TTransport TTransport
... ...
Underlying I/O Underlying I/O
2. Data Type and Protocol
Base Types
bool : (true or false), one byte
byte
i16
i32
i64
double:
string encoding agnostic text or binary string
Struct
Containers --- List Set Map
list<t1> type is t1, and the collection is ordered, the elements of the collection can be reduplicate.
set<t1> type is t1, not ordered, no reduplicate.
map<t1,t2> key is t1, value is t2
Exception
Services
Comments
# this is a valid comment
/*
* multiple comments
*/
// c++/Java style single-line comments
TProtocol
There are 2 main types, text and binary.
TBinaryProtocol
TCompactProtocol
TJSONProtocol
TSimpleJSONProtocol
TDebugProtocol this is for developing, text.
TTransport
TSocket I/O
TFramedTransport NIO
TFileTransport No java implementation
TMemoryTransport memory I/O, ByteArrayOutputStream
TZlibTransport zlib, No java implementation
Server Types
TSimpleServer single thread I/O
TThreadPoolServer multiple threads I/O
TNonblockingServer multiple threads NIO
3. First Example
java com.sillycat.easytalker.plugins.thrift.gen.code # package name
struct Blog { # POJO
1: string topic
2: binary content
3: i64 createdTime
4: string id
5: string ipAddress
6: map<string,string> props
}
service BlogService { # interface class
string createBlog(1:Blog blog)
list<string> batchCreateBlog(1:list<Blog> blogs)
string deleteBlog(1:string id)
list<Blog> listAll()
Blog getOne(1:string id)
string updateBlog(1:Blog blog)
}
>thrift.exe -out d:\work\easy\easytalker\src\main\java\ -r -gen java BlogService.thrift
Warning Message:
[WARNING:D:/work/easy/easytalker/scripts/thrift/BlogService.thrift:14] No field key specified for blog, resulting protocol may have conflicts or not be backwards compatible!
[WARNING:D:/work/easy/easytalker/scripts/thrift/BlogService.thrift:16] No field key specified for id, resulting protocol may have conflicts or not be backwards compatible!
Solution:
Because we need the number with integer before the properties of struct and parameters of methods.
struct Blog { # POJO
1: string topic
2: binary content
3: i64 createdTime
4: string id
5: string ipAddress
6: map<string,string> props
}
and
list<string> batchCreateBlog(1:list<Blog> blogs)
Once the java code is generate, add this lib to my pom.xml
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.8.0</version>
</dependency>
There are lot of warning message in generated codes.
I tried the following command, but it does not help.
>thrift.exe -out d:\work\easy\easytalker\src\main\java\ -r -gen java:java5 BlogService.thrift
references:
http://gemantic.iteye.com/blog/1199214
http://www.cnblogs.com/birdshover/archive/2010/03/16/1687301.html
http://www.javabloger.com/article/thrift-java-code-example.html
http://www.javabloger.com/article/apache-thrift-architecture.html
http://yangfanchao.iteye.com/blog/1271737
http://thrift.apache.org/docs/idl/
http://thrift.apache.org/docs/types/
http://www.cnblogs.com/tianhuilove/archive/2011/09/05/2167669.html
http://svn.apache.org/repos/asf/thrift/trunk/test/ThriftTest.thrift
http://li3huo.com/2011/10/creating-a-thrift-service-step-by-step/