namespace java com.example.service
struct User {
1: i32 id
2: string name
}
service UserService {
User getUserById(1: i32 id)
void saveUser(1: User user)
}
thrift --gen java UserService.thrift
import com.example.service.*;
class UserServiceImpl implements UserService {
@Override
public User getUserById(int id) {
User user = new User();
user.setId(id);
user.setName("John Doe");
return user;
}
@Override
public void saveUser(User user) {
System.out.println("Saved user: " + user.getName());
}
}
class Server {
public static void main(String[] args) {
UserServiceImpl serviceImpl = new UserServiceImpl();
UserService.Processor<UserService> processor = new UserService.Processor<>(serviceImpl);
com.twitter.finagle.Thrift.serveIface("localhost:8080", processor);
}
}
import com.example.service.*;
class Client {
public static void main(String[] args) {
com.twitter.finagle.ThriftServiceBuilder<ServiceIface> builder = com.twitter.finagle.Thrift.client()
.hostConnectionLimit(10)
.build();
UserService.Client client = new UserService.Client(builder.newClient("localhost:8080", UserService.Client.class));
User user = client.getUserById(1);
System.out.println("User name: " + user.getName());
User newUser = new User();
newUser.setId(2);
newUser.setName("Jane Smith");
client.saveUser(newUser);
}
}