Dave
Dave

Reputation: 203

Error creating a field with functions when using Apache Thrift

I started to use apache thrift (programming for java) and It's very hard to find documentation which explain in deeply about it - so I hope you'll be able to help me.

I'm trying to make an service (interface) which has a function that return a field with functions (for example: another interface).

I tried this code:

namespace java test

service A {
  string somefunc()
}

service B {
  string somefunc2(),
  A getA()
}

But I didn't success.. when I try to compile the thrift file I get an error that it in service B - A field is not defined.

I tried also:

namespace java test

struct A {
  1: string somefunc()
}

service B {
  A getA()
}

This time it compiled successfully but it didn't count somefunc as a function however as a field in type of string.

Is there anyway make something like what I want?

Thanks!

Upvotes: 0

Views: 442

Answers (1)

Wildfire
Wildfire

Reputation: 6418

Thrift sends serialized data structures over the wire. There's no standart way to send executable code. Various languages allow transferring code over the wire (i.e., java .class file or python script in text form) but can't be made interoperable and thus not supported by thrift.

However, thrift might be used for service discovery, if it's what you need. Single thrift service is always bound on specific host/port. So, thrift definition for service discovery code might look like this:

namespace java test

struct Endpoint {
  1: required string host;
  2: required i32 port;
}

service A {
  string somefunc()
}

service B {
  string somefunc2(),
  Endpoint getA()
}

The service discovery code might look like this:

B.Client bClient = <.....>

Endpoint endpoint = bClient.getA();
TTransport transport = new TSocket(endpoint.host, endpoint.port);
transport.open();
A.Client aClient = = new A.Client(new TBinaryProtocol(transport));
aClient.somefunc2();

If it's needed, Endpoint definition might be extended with protocol/transport metadata allowing to choose between binary/compact/JSON protocols and TTransport/TFramedTransport/etc..

Upvotes: 0

Related Questions