How can I declare an interface in Thrift? I mean I have the interface IClient and I use it as parameter in login function in Server:
public interface IServer {
void login(Pers pers, IClient client) throws ConcursException;
}
I have to use Thrift (Java server, C# client) and I don't know how to declare the interface for using it in login function.
Here is the IClient interface:
public interface IClient
{
void increasedNrParticipants(Proba proba);
}
Thank you!
As the tutorial and the syntax documentation show, and even though the language used with Thrift is called an IDL (interface definition language), one does not declare interfaces in Thrift, instead one declares a service
:
service MyCoolService {
void login(1: Pers pers, 2: Client client) throws (1: ConcursException ce)
}
Any types to be used besides the built-in basic datatypes and containers must be declared as well:
enum creditibility {
excellent, quite_good, medium_ok, could_be_worse, omg
}
struct Pers {
1: string surname
2: string firstname
3: i32 age
}
struct Client {
1: i32 client_number
2: string company_name
3: Pers contact
4: creditibility creditibility
}
Exceptions are declared in the same manner:
exception ConcursException {
1: bool chapter7
2: bool chapter11
3: double outstanding_amount
}
All the subtleties of the syntax above are explained in more detail on the Apache Thrift web site and all the other, additional documentation available on the market today.
By using the Thrift compiler, some code is generated from that IDL, which is then compiled and linked into your program as usual. In our case, we want Java code. Assumed, we saved all declarations from above in a file called myfile.thrift
, we enter:
thrift -gen java myfile.thrift
Again, it is highly recommended to walk through the tutorial. It takes not that much time, but teaches a lot of basics about how Thrift works.
Additionally, have a look at the test suite code to learn more about enhanced concepts like alternative endpoint-transports, layered transports and protocols in Thrift.
I have the interface IClient and I use it as parameter in login function in Server
All types used with Thrift must be either built-in basic types, or have to be defined using the IDL. So that approach does not work, because IClient
is not an IDL-defined type.