简体   繁体   中英

declare an interface thrift

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.

There is an RPC framework that uses the standard thrift Protocol named "thrifty", and it is the same effect as using thrift IDL to define the service, that is, thrify can be compatible with code that uses thrift IDL, which is very helpful for cross-platform and it uses DotNetty (windows is iocp, linux is nio)

[ThriftStruct]
public class LogEntry
{

    [ThriftConstructor]
    public LogEntry([ThriftField(1)]String category, [ThriftField(2)]String message)
    {
        this.Category = category;
        this.Message = message;
    }

    [ThriftField(1)]
    public String Category { get; }

    [ThriftField(2)]
    public String Message { get; }
}  

[ThriftService("scribe")]
public interface IScribe
{
    [ThriftMethod("getMessages")]
    List<LogEntry> GetMessages();

    [ThriftMethod]
    ResultCode Log(List<LogEntry> messages);
}

public class Scribe : IScribe
{
    public List<LogEntry> GetMessages()
    {
        return new List<LogEntry>
        {
            new LogEntry { Category = "c1", Message = Guid.NewGuid().ToString() },
            new LogEntry { Category = "c2", Message = Guid.NewGuid().ToString() },
            new LogEntry { Category = "c3", Message = Guid.NewGuid().ToString() }
        };
    }

    public ResultCode Log(List<LogEntry> messages)
    {
        return ResultCode.TRY_LATER;
    }
}

more detail: https://github.com/endink/Thrifty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM