Preparing for Real Time Java (RTSJ) Development

This post is moved from my old blog at http://denizoguz.blogspot.com. I have slightly modified it.

To start development with Real time java you need suitable virtual machine and a real time operating system that meets criterias required by your virtual machine.
Specification of real-time java is defined through JSR 1 and JSR 282 (small improvements). Followings are most known implementations of RTSJ.

  • Sun’s Java RTS : Current version is 2.2. It supports real-time Linux on x86 and Solaris 10 on x86 and sparc. It also supports 64 bits systems. Although previous versions did not have a real-time garbage collector (a more predictable garbage collector), latest versions has one. It supports a head of time compilation. These two features are not required by real time java specification. More information can be found at : http://java.sun.com/javase/technologies/realtime/
  • IBM’s Websphere Realtime : Current version is 2.0. It only works  on real-time Linux. It fully supports real-time java specification and contains a real-time garbage collector (metronome) and supports a head of time compilation.. More information can be found at : http://www.ibm.com/software/webservers/realtime/
  • Aonix’ PERC : Has different versions Ultra, Pico and Raven. It has also a real-time garbage collector, and supports a head of time compilation. More information can be found at : http://www.aonix.com/perc.html
  • Timesys’ RTSJ Reference Implementation : This is reference implementation and can not be used in production environment. Although you can use it four practice, I don’t recommend it, because you can download evaluation version of Sun RTSJ implementation. More information can be found at : http://www.timesys.com/java/

STEP 1 : Install Real Time Linux Kernel
I think the easiest way is to install Ubuntu and later download a real-time extensions by following the instructions at http://wiki.ubuntu.com/RealTime. It is very simple, just execute apt-get just like installing a normal application. After that reboot your machine and select real-time kernel at startup from grub menu.

STEP 2 : Download and Install a Real Time JVM
You can obtain a 90 day fully working evaluation version of Sun’ RTS 2.1 for Linux by registering with Sun’s developer network. Just fill the Sun Java Real-Time System Survey form and a download link will be mailed to you. Althoug it is not supported on ubuntu, as far as I see it is working.
You may use other versions but I could not recomend working with Timesys reference implementation because it based on a very old virtual machine and does not support new classes and feature after java version 1.3. Sun’s implemantation is fully compliant with JDK 5.0. Actually all RTSJ compliant implementations are required to pass test for JavaSE 5.0. That means you can run your JavaSE 5.0 applications as unmodified on a RTSJ compliant virtual machine by enjoying more determinism provided by real time garbage collector.

STEP 3: Prepare Eclipse
Open Window->Preferences and add Java RTS to Java->Installed JREs. Real time classes are in rt2.jar file in Sun’s implementation. Right click on this file in eclipse and add javadoc located in RTS_INSTALLATION_FOLDER/doc/rtsj-docs as javadoc for this jar file.

STEP 4 : HelloRealTime
Following program will schedule a real time thread at every 100 miliseconds and put current time in an array allocated from immortal memory. At the end prints content of this array.  By the way, you may need special privileges to access some real-time features, you can add your user to a special group or invoke JVM using sudo.

import javax.realtime.ImmortalMemory;
import javax.realtime.PeriodicParameters;
import javax.realtime.RealtimeThread;
import javax.realtime.RelativeTime;

public class HelloWorld extends RealtimeThread {
    //allocate long array from immortal memory
    public static long[] times = (long[]) ImmortalMemory.instance().newArray(long.class, 100);

    public HelloWorld(PeriodicParameters pp) {
        super(null, pp);
    }

    public void run() {
        for (int i = 0; i < 100; i++) {
            times[i] = System.currentTimeMillis();
            waitForNextPeriod(); //wait for next period
        }
    }

    public static void main(String[] argv) {
        //schedule real time thread at every 100 milisecond
        PeriodicParameters pp = new PeriodicParameters(new RelativeTime(100, 0));
        HelloWorld rtt = new HelloWorld(pp);
        rtt.start();

        //wait real time thread to terminate
        try {
            rtt.join();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

        //print results
        for (int i = 0; i < 100; i++) {
            System.out.println(times[i]);
        }
    }
}