The GreenBus Java API bindings are available through Apache Maven. The following dependency can be added to a Maven project to include the client library:
<dependency>
<groupId>io.greenbus</groupId>
<artifactId>greenbus-client</artifactId>
<version>3.0.0</version>
</dependency>
The Java JARs are available in the binary distribution from the downloads section.
Initiating a connection requires AMQP configuration parameters as well as a username and password. These can be loaded from Java .properties
files using the following utility methods:
// Load configuration files from paths provided in environment variables or in default locations
final String configBaseDir = System.getProperty("io.greenbus.config.base", "");
final String amqpConfigPath = System.getProperty("io.greenbus.config.amqp", configBaseDir + "io.greenbus.msg.amqp.cfg");
final String userConfigPath = System.getProperty("io.greenbus.config.user", configBaseDir + "io.greenbus.user.cfg");
// Load broker settings from config file
final AmqpSettings amqpSettings = new AmqpSettings(amqpConfigPath);
// Load user settings (login credentials) from config file
final UserSettings userSettings = UserSettings.load(userConfigPath);
Examples of these configuration files are included in the binary distribution.
With the AMQP connection parameters, the next step is to connect to the AMQP broker:
// Create ServiceConnection to the Qpid broker
final ServiceConnection connection = ServiceConnectionFactory.create(amqpSettings, QpidBroker.instance(), 10000);
The ServiceConnection
interface can then be used to acquire a logged-in Session
. The login
method returns a ListenableFuture
object from the Google Guava concurrency library. The Java API binding methods all return futures to enable asynchronous service programming.
// Get a Session object that has a valid auth token. Causes a service call to login
final ListenableFuture<Session> loginFuture = connection.login(userSettings.user(), userSettings.password());
final Session session = loginFuture.get(5000, TimeUnit.MILLISECONDS);
The GreenBus API binding interfaces can be instantiated using a session object. In the next section a ModelService
client is built and then used to make the get_points
GreenBus API service call.
// Get service interface for points
final ModelService.Client client = ModelService.client(session);
// Find the point again using the point name
Model.Point point = client.getPoints(ModelRequests.EntityKeySet.newBuilder().addNames("ExampleName").build())
.get(5000, TimeUnit.MILLISECONDS)
.get(0);
System.out.println("Found point by name: " + point.getName());
Finally, the service connection is cleaned up:
// Disconnect from AMQP and shut down thread pools
connection.disconnect();
More examples of using the service APIs from Java can be found in the GitHub repository.
git clone https://github.com/gec/greenbus-examples.git