Java federates

MicroRTI has no Java client of its own. Java federates use the open-source Pitch FedProClient (Apache License 2.0), which implements the standard HLA 4 Java API, package hla.rti1516_2025, on top of the Federate Protocol of IEEE 1516.1-2025. This page gets the library, writes a small federate against the standard API and runs it against a local microrti.

What has been tested

The repository’s Java interoperability harness runs scripted scenarios with FedProClient v2.1.1 against microrti over TCP, TLS, WebSocket and secure WebSocket: federation management, publication and subscription, objects and interactions, ownership, time management, data distribution management, synchronization points, save and restore and much of the Management Object Model, with two federates at most. It does not prove conformance, and its README lists what it leaves out. The federate on this page was run with v2.1.1 built from source and with v2.1.0 from Maven Central, on macOS with JDK 11.

What you need

Get FedProClient

From Maven Central

Version 2.1.0 is on Maven Central as se.pitch.oss.fedpro:fedpro-client. It brings the standard API (hla-4-api) and its other dependencies with it. A pom.xml for the federate below:

<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>chat</groupId>
  <artifactId>hello</artifactId>
  <version>1.0</version>
  <properties>
    <maven.compiler.release>11</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>se.pitch.oss.fedpro</groupId>
      <artifactId>fedpro-client</artifactId>
      <version>2.1.0</version>
    </dependency>
  </dependencies>
</project>

Other build tools that read Maven Central take the same coordinates.

From source

Maven Central has 2.1.0; tag v2.1.1, the version the interoperability harness uses, is only on GitHub. The project’s own build uses Gradle and needs JDK 17 (see its java/README.md). Plain javac needs only JDK 11 and builds what a federate uses, the HLA 4 API and the client, from about 1,300 source files. It took 20 seconds on a laptop:

git clone --depth 1 --branch v2.1.1 https://github.com/Pitch-Technologies/FedProClient.git
cd FedProClient/java
mkdir -p build/classes
find api/HLA1516-2025/src protobuf/src/java session/src client_common/src client/src client/gensrc \
  -name '*.java' > build/sources.txt
javac --release 11 -nowarn -proc:none -encoding UTF-8 -cp "lib/*" -d build/classes @build/sources.txt
cp -R client/resources/META-INF build/classes/
jar cf build/fedpro-client-hla4.jar -C build/classes .
cd ../..

javac prints a note about unchecked operations; it is harmless. Do not skip the copy of META-INF: it holds the service files through which RtiFactoryFactory finds the client and its time factories. The jars the client needs at run time (protobuf, Java-WebSocket, SLF4J and the JCIP annotations) come with the clone, in FedProClient/java/lib.

A federate in one file

The federate joins the chat room of MicroRTI’s chat example: federation execution Chat, FOM module Chat.xml, interaction class Communication with two parameters, Sender and Message, both of type HLAunicodeString. It creates the federation execution unless somebody already has, joins, publishes and subscribes Communication, says hello once and prints every line it hears for a number of seconds. Then it resigns, destroys the federation execution if nobody else is in it, and disconnects.

Fetch the FOM module into a new directory, next to the federate:

mkdir hello-java && cd hello-java
curl -O https://raw.githubusercontent.com/microrti/microrti/main/crates/microrti-examples/fom/Chat.xml

Then Hello.java (with Maven, put it in src/main/java/ and pom.xml next to Chat.xml):

import hla.rti1516_2025.CallbackModel;
import hla.rti1516_2025.FederateHandle;
import hla.rti1516_2025.InteractionClassHandle;
import hla.rti1516_2025.NullFederateAmbassador;
import hla.rti1516_2025.ParameterHandle;
import hla.rti1516_2025.ParameterHandleValueMap;
import hla.rti1516_2025.RTIambassador;
import hla.rti1516_2025.RegionHandleSet;
import hla.rti1516_2025.ResignAction;
import hla.rti1516_2025.RtiConfiguration;
import hla.rti1516_2025.RtiFactory;
import hla.rti1516_2025.RtiFactoryFactory;
import hla.rti1516_2025.TransportationTypeHandle;
import hla.rti1516_2025.encoding.DecoderException;
import hla.rti1516_2025.encoding.EncoderFactory;
import hla.rti1516_2025.encoding.HLAunicodeString;
import hla.rti1516_2025.exceptions.FederatesCurrentlyJoined;
import hla.rti1516_2025.exceptions.FederationExecutionAlreadyExists;

/** Joins the chat room of MicroRTI's examples, says hello and prints what it hears for a while. */
public class Hello extends NullFederateAmbassador {
   private final EncoderFactory encoders;
   private ParameterHandle sender;
   private ParameterHandle message;

   private Hello(EncoderFactory encoders) {
      this.encoders = encoders;
   }

   @Override
   public void receiveInteraction(InteractionClassHandle interactionClass, ParameterHandleValueMap values,
         byte[] tag, TransportationTypeHandle transportation, FederateHandle producer, RegionHandleSet regions) {
      System.out.println("<" + text(values.get(sender)) + "> " + text(values.get(message)));
   }

   private String text(byte[] encoded) {
      HLAunicodeString string = encoders.createHLAunicodeString();
      try {
         string.decode(encoded);
         return string.getValue();
      } catch (DecoderException | NullPointerException e) {
         return "?";
      }
   }

   public static void main(String[] args) throws Exception {
      String address = args.length > 0 ? args[0] : "localhost:15164";
      String name = args.length > 1 ? args[1] : "java";
      double seconds = args.length > 2 ? Double.parseDouble(args[2]) : 10;

      RtiFactory factory = RtiFactoryFactory.getRtiFactory();
      EncoderFactory encoders = factory.getEncoderFactory();
      RTIambassador rti = factory.getRtiAmbassador();
      Hello federate = new Hello(encoders);

      rti.connect(federate, CallbackModel.HLA_EVOKED, RtiConfiguration.createConfiguration().withRtiAddress(address));
      try {
         rti.createFederationExecution("Chat", "Chat.xml");
      } catch (FederationExecutionAlreadyExists e) {
         // somebody else opened the room first
      }
      rti.joinFederationExecution(name, "hello", "Chat");
      System.out.println("joined Chat as " + name);

      InteractionClassHandle communication = rti.getInteractionClassHandle("Communication");
      federate.sender = rti.getParameterHandle(communication, "Sender");
      federate.message = rti.getParameterHandle(communication, "Message");
      rti.publishInteractionClass(communication);
      rti.subscribeInteractionClass(communication);

      ParameterHandleValueMap hello = rti.getParameterHandleValueMapFactory().create(2);
      hello.put(federate.sender, encoders.createHLAunicodeString(name).toByteArray());
      hello.put(federate.message, encoders.createHLAunicodeString("hello from Java").toByteArray());
      rti.sendInteraction(communication, hello, new byte[0]);

      long end = System.nanoTime() + (long) (seconds * 1e9);
      while (System.nanoTime() < end) {
         rti.evokeMultipleCallbacks(0.1, 0.5); // HLA_EVOKED: callbacks run only in here
      }

      rti.resignFederationExecution(ResignAction.NO_ACTION);
      try {
         rti.destroyFederationExecution("Chat");
         System.out.println("left; the room is closed");
      } catch (FederatesCurrentlyJoined e) {
         System.out.println("left; others are still in the room");
      }
      rti.disconnect();
   }
}

Nothing in the code names FedProClient: RtiFactoryFactory.getRtiFactory() finds whichever HLA 4 implementation is on the class path. A few points that are specific to this client:

  • The RTI address is host:port, without the tcp:// prefix that MicroRTI’s own tools use. The transport is a separate setting (see Other transports).
  • createFederationExecution gets the path of the FOM module. The client reads the file and sends its content, so the file only has to exist where the federate runs. If the client finds no such file, nor a resource of that name on the class path, it sends the name to the RTI as a URL, and microrti refuses it with CouldNotOpenFOM (see questions and answers).
  • With HLA_EVOKED the client delivers callbacks only inside evokeCallback or evokeMultipleCallbacks. With HLA_IMMEDIATE a thread of the client delivers them as they arrive.

Run it

Start an RTI in a terminal of its own:

microrti --listen tcp://127.0.0.1:0
listening on tcp://127.0.0.1:64006

Port 0 lets the system choose a free port, and the line says which one it chose. Without --listen, microrti listens on 0.0.0.0:15164; 15164 is the standard Federate Protocol port.

A federate does not receive its own interactions, so one copy of Hello alone hears nothing. Run two. In a second terminal, with the classes built from source:

RTI=127.0.0.1:64006      # the address your microrti printed, without tcp://
CP="../FedProClient/java/build/fedpro-client-hla4.jar:../FedProClient/java/lib/*"
javac -cp "$CP" Hello.java
java -cp "$CP:." Hello $RTI alice 20

In a third terminal, within those 20 seconds (set RTI and CP there too):

java -cp "$CP:." Hello $RTI bob 3

With Maven, the same two runs are mvn -q compile exec:java -Dexec.mainClass=Hello -Dexec.args="$RTI alice 20" and the same with bob 3. Either way, alice prints:

joined Chat as alice
<bob> hello from Java
left; the room is closed

and bob prints:

joined Chat as bob
left; others are still in the room

alice heard bob. bob heard nothing: alice said hello before bob had subscribed, and the RTI does not keep interactions for subscribers who come later. bob left first and could not destroy the federation execution; alice was the last one in it and did.

The chat example of the repository uses the same FOM module and the same federation execution name, so Hello can talk to it. Clients shows a run with a Rust, a Java and a C++ federate in one room.

Other transports

microrti serves TCP, TLS, WebSocket and secure WebSocket (see Configure the RTI). FedProClient picks the transport from the setting FedPro.connect.protocol:

microrti listenerFedProClient settings
tcp://none: tcp is the default
ws://FedPro.connect.protocol=websocket
tls:// with --tls-cert and --tls-keyFedPro.connect.protocol=tls, and a trust store unless the JDK already trusts the certificate
tls:// with --tls-self-signedFedPro.connect.protocol=tls,FedPro.tls.mode=ENCRYPTED
wss://FedPro.connect.protocol=websocketsecure, with the same TLS settings as tls://

Pass them as the additional settings of the configuration, separated by commas:

RtiConfiguration configuration = RtiConfiguration.createConfiguration()
      .withRtiAddress("rti.example.org:8080")
      .withAdditionalSettings("FedPro.connect.protocol=websocket");
rti.connect(federate, CallbackModel.HLA_EVOKED, configuration);

Settings that start with FedPro. are for the client and are not sent to the RTI. The same settings also work without touching the code, as Java system properties or, without the prefix, in the environment variable FEDPRO_CLIENT_SETTINGS. Both apply only to keys the code does not set itself:

java -DFedPro.connect.protocol=websocket -cp "$CP:." Hello $RTI alice 20
FEDPRO_CLIENT_SETTINGS=connect.protocol=websocket java -cp "$CP:." Hello $RTI alice 20

Over WebSocket the client logs one INFO line through java.util.logging when the connection closes.

Check the RTI’s certificate

FedPro.tls.mode=ENCRYPTED encrypts but accepts any certificate. To check the certificate of the RTI against your own certificate authority, put the authority’s certificate in a Java trust store and give its path and the path of a file that holds its password:

keytool -importcert -noprompt -alias rti-ca -file ca.pem -keystore trust.jks -storepass changeit
printf 'changeit' > trust.pass
FedPro.connect.protocol=tls,FedPro.keystore.path=trust.jks,FedPro.keystore.password.path=trust.pass

In a test with v2.1.1, tls accepted a certificate that chained to the trust store but was issued for another host name; websocketsecure refused the same certificate with No subject alternative DNS name matching localhost found. If you need the host name checked, use a wss listener and websocketsecure.

Send a password

When the RTI requires authorisation (see TLS and authorisation), pass the password as HLAplainTextPassword credentials, and only over tls or websocketsecure:

import hla.rti1516_2025.auth.HLAplainTextPassword;

rti.connect(federate, CallbackModel.HLA_EVOKED, configuration, new HLAplainTextPassword("lunch is ready"));

A wrong password makes connect raise Unauthorized with the message wrong password.

When it does not connect

Each of these was provoked against a local microrti. The exception comes out of connect:

ExceptionCause
ConnectionFailed: Failed to start FedPro session: Failed to connect to FedPro server at 127.0.0.1:1 (java.net.ConnectException: Connection refused ...)Nothing listens at that address.
ConnectionFailed: Setting 'connect.hostname=tcp,connect.port=//127.0.0.1:64176' is invalid: ...The address starts with tcp://. Give host:port only.
ConnectionFailed: Failed to start FedPro session: Could only read 0 of 4 bytesThe transport does not match the listener: here a TCP client against a ws:// listener.
ConnectionFailed: Failed to start FedPro session: Failed to initiate TLS handshakeThe client does not trust the certificate: add a trust store, or use FedPro.tls.mode=ENCRYPTED with --tls-self-signed.

Next steps