A project already created using build tool gradle can be found on github. Use the following command to clone the repository:

git clone https://github.com/migratorydata/getting-started-java-client-api.git

To run the application, you need Java Development Kit (JDK) 8+ and Gradle 6+.

Open the project in your IDE of choice. By default, the MigratoryData Client API is set to connect to the MigratoryData server located at address localhost:8800 and subscribe to the subject /server/status. If you don’t have a MigratoryData server installed on your machine but there is docker installed you can run the following command to start MigratoryData server, otherwise you can download and install the latest version for your os from here.

docker pull migratorydata/server:latest
docker run --platform linux/amd64 -d --name my_migratorydata -p 8800:8800 migratorydata/server:latest

Next, build and run the application using commands:

./gradlew clean build
./gradlew run

To test the application, connect to the MigratoryData server via browser at http://localhost:8800 and go to Debug Console. Subscribe and publish data to subject /server/status and see if the java application is receiving data. Also you should receive data published by the java application. The application publishes a message every 3 seconds on subject /server/status.

A typical API usage is as follows:

Step 1 - Include the library

Include in the build.gradle of your application the API library

// https://mvnrepository.com/artifact/com.migratorydata/client-java-api
implementation group: 'com.migratorydata', name: 'client-java-api', version: '6.0.1'

Import in your application the classes of this API as follows:

import com.migratorydata.client.*;

Step 2 - Define a listener to get messages and status notifications

The listener should implement the MigratoryDataListener interface.

Use the API call MigratoryDataClient.setListener() to attach your listener implementation.

Step 3 - Specify where to connect to

Specify a cluster of one or more MigratoryData servers to which the Java client will connect to using the API method MigratoryDataClient.setServers(). In fact, the client will connect to only one of the MigratoryData servers in this list. But, defining two or more MigratoryData servers is recommended in order to achieve fail-over. Supposing the MigratoryData server to which the client connected goes down, then the API will automatically reconnect the client to another MigratoryData server in the list.

Step 4 - Connect to the MigratoryData cluster

Use the API method MigratoryDataClient.connect() to connect to the cluster and start receiving real-time messages from the MigratoryData cluster as well as status notifications.

Step 5 - Subscribe to subjects and publish messages

Use the API method MigratoryDataClient.subscribe() to specify interest in receiving real-time messages having as subjects the strings provided in the parameter of this API method. You can call the API method MigratoryDataClient.subscribe() at any time to subscribe to further subjects. To unsubscribe from subscribed subjects, use the API method MigratoryDataClient.unsubscribe().

Use the API method MigratoryDataClient.publish() to publish messages.

Step 6 - Handle the real-time messages and status notifications

Handle the messages received for the subscribed subjects as well as the status notifications in your listener implementation defined at Step 2 above.