Skip to content

Commit 758ff99

Browse files
committed
Improve documentation
1 parent 396eeab commit 758ff99

File tree

4 files changed

+285
-178
lines changed

4 files changed

+285
-178
lines changed

README.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
[![Build Status](https://travis-ci.org/airlift/drift.svg?branch=master)](https://travis-ci.org/airlift/drift)
44

55
Drift is an easy-to-use, annotation-based Java library for creating Thrift
6-
clients and serializable types. The client library is similar to JAX-RS
6+
clients and serializable types. The client library is similar to JAX-RS
77
(HTTP Rest) and the serialization library is similar to JaxB (XML) and Jackson
88
(JSON), but for Thrift.
99

10-
## Example
10+
## Example
1111

1212
The following interface defines a client for a Scribe server:
1313

@@ -20,8 +20,8 @@ public interface Scribe
2020
}
2121
```
2222

23-
The `log` method above uses the `LogEntry` Thrift struct which is defined as follows:
24-
23+
The `log` method above uses the `LogEntry` Thrift struct which is defined as follows:
24+
2525
```java
2626
@ThriftStruct
2727
public class LogEntry
@@ -52,20 +52,15 @@ public class LogEntry
5252

5353
An instance of the Scribe client can be created using a `DriftClientFactory`:
5454
```java
55-
// expensive services that should only be created once
56-
ThriftCodecManager codecManager = new ThriftCodecManager();
57-
AddressSelector addressSelector = new SimpleAddressSelector(scribeHostAddresses);
58-
DriftNettyClientConfig config = new DriftNettyClientConfig();
59-
// methodInvokerFactory must be closed
60-
DriftNettyMethodInvokerFactory<?> methodInvokerFactory = DriftNettyMethodInvokerFactory.createStaticDriftNettyMethodInvokerFactory(config);
61-
DriftClientFactory clientFactory = new DriftClientFactory(codecManager, methodInvokerFactory, addressSelector);
62-
63-
// create a client (cached also)
55+
// create a client
6456
Scribe scribe = clientFactory.createDriftClient(Scribe.class);
6557

6658
// use client
6759
scribe.log(Arrays.asList(new LogEntry("category", "message")));
6860
```
6961

70-
See [Drift Codec](drift-codec) for more information on annotating Thrift types,
71-
and [Drift Client](drift-client) for more information on Thrift client usage.
62+
# Detailed Documentation
63+
64+
* [Drift Codec](drift-codec) -- Thrift type annotations and serialization
65+
* [Drift Client](drift-client) -- Thrift client usage
66+
* [Drift Server](drift-server) -- Thrift server usage

drift-client/README.md

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ Drift Client is a simple library for creating Thrift clients using annotations.
44

55
## Implementing a Client
66

7-
In Drift, a Thrift client is simply a Java interface annotated with `@ThriftService`. In
7+
In Drift, a Thrift client is simply a Java interface annotated with `@ThriftService`. In
88
addition to annotating the interface directly, Drift supports annotations on super interfaces.
99
For example, the following describes a Scribe client:
10+
1011
```java
1112
@ThriftService
1213
public interface Scribe
@@ -15,8 +16,10 @@ public interface Scribe
1516
ResultCode log(List<LogEntry> messages);
1617
}
1718
```
19+
1820
To make the client method asynchronous, simply change the return type of the
1921
corresponding method to a `ListenableFuture`, as shown here:
22+
2023
```java
2124
@ThriftService
2225
public interface Scribe
@@ -25,6 +28,7 @@ public interface Scribe
2528
ListenableFuture<List<Integer>> getValues();
2629
}
2730
```
31+
2832
The future will be completed when the server returns a value. When adding listeners
2933
that do non-trivial work when the future is completed, keep in mind that if you do not
3034
provide an executor for listeners to run on, they will run on the NIO threads, and
@@ -38,6 +42,7 @@ parameters are numbered starting with `1`. If you do not specify a name, Drift
3842
will attempt to determine the names automatically. For this to work, the code
3943
must be compiled with parameter names enabled (pass the `-parameters` option to `javac`).
4044
If you want to use a different ID or name, simply annotate the parameter as follows:
45+
4146
```java
4247
@ThriftService
4348
public interface Scribe
@@ -54,6 +59,7 @@ zero being a standard return and exceptions be stored in higher number fields.
5459
If the Java method throws only one exception annotated with `@ThriftStruct`,
5560
Drift will assume the result struct field id is `1`. Otherwise, you will need to
5661
add the extremely verbose `@ThriftException` annotations as follows:
62+
5763
```java
5864
@ThriftMethod(exception = {
5965
@ThriftException(type = MyException.class, id = 1),
@@ -67,7 +73,7 @@ void doSomething() throws MyException, MyOther;
6773
A Drift client is thread safe and concurrent, so it can be used by multiple threads
6874
at the same time. The user of the client simply calls methods on the interface and Drift
6975
manages address selection and connection pooling. For example:
70-
76+
7177
```java
7278
// create client factory (only create this expensive object once)
7379
DriftClientFactory clientFactory = // see below
@@ -76,27 +82,35 @@ DriftClientFactory clientFactory = // see below
7682
Scribe scribe = clientFactory.createDriftClient(Scribe.class);
7783

7884
// use client
79-
scribe.log(Arrays.asList(new LogEntry("category", "message")));
85+
scribe.log(ImmutableList.of(new LogEntry("category", "message")));
8086
```
8187

8288
A Drift client can either be created manually using a static factory or injected using Guice.
8389

8490
## Static Drift Client Factory
8591

86-
The following code manually constructs a `DriftClientFactory` using the Netty transport:
92+
The following code manually constructs a `DriftClientFactory` using the Netty transport:
93+
8794
```java
95+
// server address
96+
List<HostAndPort> addresses = ImmutableList.of(HostAndPort.fromParts("localhost", 1234));
97+
8898
// expensive services that should only be created once
8999
ThriftCodecManager codecManager = new ThriftCodecManager();
90-
AddressSelector addressSelector = new SimpleAddressSelector(scribeHostAddresses);
100+
AddressSelector addressSelector = new SimpleAddressSelector(addresses);
91101
DriftNettyClientConfig config = new DriftNettyClientConfig();
92-
// methodInvokerFactory must be closed
93-
DriftNettyMethodInvokerFactory<?> methodInvokerFactory = DriftNettyMethodInvokerFactory.createStaticDriftNettyMethodInvokerFactory(config);
102+
103+
// methodInvokerFactory must be closed
104+
DriftNettyMethodInvokerFactory<?> methodInvokerFactory = DriftNettyMethodInvokerFactory
105+
.createStaticDriftNettyMethodInvokerFactory(config);
106+
107+
// client factory
94108
DriftClientFactory clientFactory = new DriftClientFactory(codecManager, methodInvokerFactory, addressSelector);
95109
```
96110

97-
As you can see, the construction of a `DriftClientFactory` requires a few supporting
111+
As you can see, the construction of a `DriftClientFactory` requires a few supporting
98112
services, which are described below.
99-
113+
100114
### ThriftCodecManager
101115

102116
A `ThriftCodecManager` caches the description of every Thrift type used by the clients. Extracting
@@ -105,7 +119,7 @@ all the clients you will need.
105119

106120
### AddressSelector
107121

108-
An `AddressSelector` selects host addresses for the client to connect to. Typically,
122+
An `AddressSelector` selects host addresses for the client to connect to. Typically,
109123
this service tracks all hosts running the service and selects a random subset to try
110124
for an invocation. The `AddressSelector` is also notified of addresses that failed to
111125
connect, allowing it to perform simple tracking of host state.
@@ -119,36 +133,31 @@ method is typically provided to shutdown the pools.
119133

120134
There are currently two transport implementations of `MethodInvokerFactory`. Drift Netty, which
121135
is used in the example above, provides `DriftNettyMethodInvokerFactory`, and Apache Thrift provides
122-
`ApacheThriftMethodInvokerFactory`. Each transport requires sightly different configuration,
136+
`ApacheThriftMethodInvokerFactory`. Each transport requires sightly different configuration,
123137
so each transport provides a different configuration class.
124138

125139
## Guice Support
126140

127141
Drift includes optional support for binding clients into Guice.
128142

129-
To bind a client, add the `ThriftClientModule` and a transport implementation module (e.g.,
130-
`DriftNettyClientModule` or `ApacheThriftClientModule`), and bind the clients with the fluent
131-
`DriftClientBinder`. The following binds a `Scribe` client that will connect to
132-
`example.com:1234` by default.
143+
To bind a client, add a transport implementation module (e.g., `DriftNettyClientModule` or
144+
`ApacheThriftClientModule`), and bind the clients with the fluent `DriftClientBinder`.
145+
The following binds a `Scribe` client that will connect to `example.com:1234` by default:
133146

134147
```java
148+
// server address
149+
List<HostAndPort> addresses = ImmutableList.of(HostAndPort.fromParts("localhost", 1234));
150+
135151
// see io.airlift.bootstrap.Bootstrap for a simpler system to create Guice services with configuration
136152
Injector injector = Guice.createInjector(Stage.PRODUCTION,
137-
new ConfigurationModule(new ConfigurationFactory(ImmutableMap.<>of())),
138-
new ThriftCodecModule(),
153+
new ConfigurationModule(new ConfigurationFactory(ImmutableMap.of())),
139154
new DriftNettyClientModule(),
140-
new Module()
141-
{
142-
@Override
143-
public void configure(Binder binder)
144-
{
145-
driftClientBinder(binder).bindDriftClient(Scribe.class)
146-
.withAddressSelector(simpleAddressSelector(HostAndPort.fromParts("example.com", 1234)));
147-
}
148-
});
155+
binder -> driftClientBinder(binder).bindDriftClient(Scribe.class)
156+
.withAddressSelector(simpleAddressSelector(addresses)));
149157
```
150158

151-
Then, Guice can inject a Thrift client implementation. For example:
159+
Then, Guice can inject a Thrift client implementation:
160+
152161
```java
153162
@Inject
154163
public MyClass(Scribe scribeClient)

0 commit comments

Comments
 (0)