Client basics

Let’s assume we have the following Thrift IDL, served at http://127.0.0.1:8080/hello:

namespace java com.example.thrift

service HelloService {
    string hello(1:string name)
}

Making a call starts from creating a client:

import com.linecorp.armeria.client.Clients;

HelloService.Iface helloService = Clients.newClient(
        "tbinary+http://127.0.0.1:8080/hello",
        HelloService.Iface.class); // or AsyncIface.class

String greeting = helloService.hello("Armerian World");
assert greeting.equals("Hello, Armerian World!");

Note that we added the serialization format of the call using the + operator in the scheme part of the URI. Because we are calling a Thrift server, we should choose: tbinary, tcompact, tjson or ttext.

Since we specified HelloService.Iface as the client type, Clients.newClient() will return a synchronous client implementation. If we had specified HelloService.AsyncIface, the calling code would have looked like the following:

import com.linecorp.armeria.client.Clients;
import org.apache.thrift.async.AsyncMethodCallback;

HelloService.AsyncIface helloService = Clients.newClient(
        "tbinary+http://127.0.0.1:8080/hello",
        HelloService.AsyncIface.class);

helloService.hello("Armerian World", new AsyncMethodCallback<String>() {
    @Override
    public void onComplete(String response) {
        assert response.equals("Hello, Armerian World!");
    }

    @Override
    public void onError(Exception exception) {
        exception.printStackTrace();
    }
});

You can also use the builder pattern for client construction:

import com.linecorp.armeria.common.http.HttpRequest;
import com.linecorp.armeria.common.http.HttpResponse;

HelloService.Iface helloService = new ClientBuilder("tbinary+http://127.0.0.1:8080/hello")
        .defaultResponseTimeoutMillis(10000)
        .decorator(HttpRequest.class, HttpResponse.class, LoggingClient::new)
        .build(HelloService.Iface.class); // or AsyncIface.class

String greeting = helloService.hello("Armerian World");
assert greeting.equals("Hello, Armerian World!");

As you might have noticed already, we decorated the client using LoggingClient, which logs all Thrift calls and replies. You might be interested in decorating a client using other decorators, for example to gather metrics. Please also refer to ClientBuilder for more configuration options.

Next steps