-
-
Notifications
You must be signed in to change notification settings - Fork 15
Clients
To connect to a service first import the respective client, using either thrift-clj.core/import-clients
or thrift-clj.core/import
with the :clients
key. You might have to import the used types, too.
(require '[thrift-clj.core :as thrift])
(thrift/import-clients [org.example.PersonIndex :as PI])
(thrift/import-types [org.example Person])
;; or:
(thrift/import
(:clients [org.example.PersonIndex :as PI])
(:types [org.example Person]))
This will create a new namespace containing wrappers for the service methods and alias it using PI
. Let's say the PersonIndex service is shaped like this:
service PersonIndex {
bool storePerson(1:i32 id, 2:Person p),
Person getPerson(1:i32 id)
}
You can now access these functions using PI/storePerson
and PI/getPerson
both of which need a client as their first parameter and will convert every parameter to its Thrift representation if necessary.
Connection to a service is done using thrift-clj.core/connect!
. It takes the client to create as its first and the transport to use as its second parameter (see below, "Client Transports"), as well as some additional options like :protocol
(see here for possible values):
(thrift/connect! PI ["localhost" 1234] :protocol :compact)
(thrift/connect! PI (thrift/http "http://localhost"))
The resulting client implements java.io.Closeable
and can thus be used with with-open
to ensure closing of the connection when you're done:
(with-open [c (thrift/connect! PI ["localhost" 1234])]
(PI/storePerson c 1 (Person. "Some" "One" 1234))
(prn (PI/getPerson c 1)))
And that's that. Note that you have to catch exceptions by yourself. Maybe at some point, thrift-clj will handle them in a useful manner.
The following transport functions can be used:
-
tcp:
(tcp host port)
will create a TCP connection to the given endpoint.(tcp port)
will connect to "localhost"; -
tcp-async: like
tcp
but will create a non-blocking socket; -
http:
(http url & options)
will create a HTTP connection to the given URL. Options include:-
:connect-timeout
: maximum waiting time for accepted connection -
:read-timeout
: maximum waiting time for response reading -
:custom-headers
: map of additional headers
-
-
streams:
(streams in out)
will create a low-leveljava.io.Stream
-based transport; -
framed [since 0.1.1]:
(framed transport)
will wrap the given transport to use frame size prefix; -
fast-framed [since 0.1.1]: same as
framed
but using persistent byte buffers for higher performance.
Client transports can be added by implementing thrift-clj.client.transport/TransportConvertable
which is called from connect!
. It is used to simplify endpoint specification.
-
<Integer>: interpreted as
(tcp <Integer>)
-
"http://...": interpreted as
(http <String>)
-
"<Host>:<Port>": interpreted as
(tcp <Host> <Port>)
-
[<String> <Integer>] : interpreted as
(tcp <String> <Integer>)
-
[<InputStream> <OutputStream>] and [<OutputStream> <InputStream>]: interpreted as
(streams <InputStream> <OutputStream>)
This should reduce verbosity a bit.