To get the Java connector for Tarantool 1.6.9, visit this GitHub page.
- Add a dependency to your
pom.xmlfile.
<dependency>
<groupId>org.tarantool</groupId>
<artifactId>connector</artifactId>
<version>1.7.4</version>
</dependency>- Configure
TarantoolClientConfig.
TarantoolClientConfig config = new TarantoolClientConfig();
config.username = "test";
config.password = "test";- Implement your
SocketChannelProvider. It should return a connectedSocketChannel.
SocketChannelProvider socketChannelProvider = new SocketChannelProvider() {
@Override
public SocketChannel get(int retryNumber, Throwable lastError) {
if (lastError != null) {
lastError.printStackTrace(System.out);
}
try {
return SocketChannel.open(new InetSocketAddress("localhost", 3301));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
};Here you could also implement some reconnection or fallback policy.
Remember that TarantoolClient adopts a
fail-fast policy
when a client is not connected.
- Create a client.
TarantoolClient client = new TarantoolClientImpl(socketChannelProvider, config);Notes:
TarantoolClientis thread-safe and asynchronous, so you should use one client inside the whole application.TarantoolClientdoes not support name resolution for fields, indexes, spaces and so on. We highly recommend to use server-side Lua when working with named items. For example, you could create a data access object (DAO) with simple CRUD functions. If, for some reason, you do need client name resolution, you could create a function that returns necessary name-to-ID mappings.
TarantoolClient provides four interfaces to execute queries:
SyncOps- returns the operation resultAsyncOps- returns the operation result as aFutureComposableAsyncOps- return the operation result as aCompletionStageFireAndForgetOps- returns the query ID
Feel free to override any method of TarantoolClientImpl. For example, to hook
all the results, you could override this:
protected void complete(long code, FutureImpl<List> q);Got problems or questions? Post them on
Stack Overflow with the
tarantool and java tags, or use these tags to search the existing knowledge
base for possible answers and solutions.