Skip to content

RexPro Java

spmallette edited this page Feb 18, 2013 · 14 revisions

RexPro is designed to be language agnostic, such that virtually any programming language, especially those languages that support MsgPack, can connect to a RexPro Server. Rexster comes bundled with a Java client, called RexterClient, that allows low-level connectivity via RexPro, sending sessionless messages.

The following Maven dependency is required for accessing RexsterClient classes:

<dependency>
    <groupId>com.tinkerpop.rexster</groupId>
    <artifactId>rexster-protocol</artifactId>
    <version>x.y.z</version>
</dependency>

Use the RexsterClientFactory to create a RexsterClient instance:

RexsterClient client = RexsterClientFactory.open();
RexsterClient client = RexsterClientFactory.open("localhost");
RexsterClient client = RexsterClientFactory.open("localhost", 8184);

All three of the above initializations of RexsterClient are equivalent. When no arguments are specified, the factory will initialize the RexsterClient to connect to localhost on the standard RexPro port 8184. There are numerous settings and configurations for initializing a RexsterClient instance which are described further below. Once RexsterClient is initialized, send a Gremlin script to the RexPro Server:

final List<Map<String, Object>> results = client.execute("g=rexster.getGraph("tinkergraph");g.v(1).map");
final Map<String, Object> map = results.get(0);
System.out.println(map.get("name"));

As RexsterClient uses sessionless requests, g must be initialized on each request. To get around this problem set the graph-name configuration setting when instantiating RexsterClient through RexsterClientFactory or simply use this convenience method overload:

RexsterClient client = RexsterClientFactory.open("localhost", "tinkergraph");
final List<Map<String, Object>> results = client.execute("g.v(1).map");
final Map<String, Object> map = results.get(0);
System.out.println(map.get("name"));

Note that RexsterClient makes some assumptions about data types, as it is limited by MsgPack data types:

final List<Map<String, Object>> mapResultsObject = client.execute("[n:1+1,b:true,f:1234.56f,s:'string',a:[1,2,3],m:[one:1]]");
Assert.assertEquals(1, mapResultsObject.size());
final Map<String, Object> mapResultObject = mapResultsObject.get(0);
Assert.assertEquals(2l, mapResultObject.get("n"));
Assert.assertEquals(true, mapResultObject.get("b"));
Assert.assertEquals(1234.56d, (Double) mapResultObject.get("f"), 0.001d);
Assert.assertEquals("string", mapResultObject.get("s"));
Assert.assertEquals(3, ((Object []) mapResultObject.get("a")).length);
Assert.assertEquals(1l, ((Map) mapResultObject.get("m")).get("one"));

Settings

The following settings are available when configuring a RexsterClient instance. These settings can be passed to the RexsterClientFactory.open() method as a Map<String,Object> or Apache Configuration object.

name default description
hostname localhost The RexPro Server host to connect to.
port 8184 The RexPro Server port to connect to.
timeout-connection-ms 8000 How long to wait for a connection to open to the RexPro Server.
timeout-write-ms 4000 How long to wait for a write of a message to the RexPro Server.
timeout-read-ms 16000 How long to wait for a read of a response from the RexPro Server.
message-retry-count 16 The number of times to retry the request if any of the above fail.
message-retry-wait-ms 50 The amount of time to wait if any of the above fail..
max-async-write-queue-size 512000 The size in bytes of the queue on the client for sending requests to the RexPro Server. This queue can backup if network performance is poor or Rexster slows/stops.
deserialize-array-size-limit 4194304 Max size of an array/list. Default size from MsgPack.
deserialize-map-size-limit 2097152 Max number of elements in a map. Default size from MsgPack.
deserialize-raw-size-limit 134217728 Max size in bytes of a message. Default size from MsgPack.
language groovy The Gremlin flavor to use to execute the script.
graph-name The name of the graph configured in Rexster to use as the subject of the script. Typically, the graph must be retrieved in the script via rexster.getGraph(). This setting in conjunction with graph-obj-name help to simplify a script by putting the graph specified here in the Script Engine bindings so that it is ready for the script.
graph-obj-name g If graph-name is specified then the graph instance is assigned to the value of this setting in the bindings so that it can be instantly referenced in the script.
channel 2 The serialization channel for RexPro. By default this value is set to MsgPack.
Clone this wiki locally