Elektron SDK - Java

API Family: Elektron

EMA NI Provider - A barebones EMA NIP application shell

Download tutorial source code

Click here to download

Last update January 2017
Compilers

JDK 1.7.x, JDK 1.8.x

Prerequisites

EMA NI Provider - Introduction

Declare the NI_PUB and TEST_NI_PUB services in your TREP (see Before you start).

Tutorial purpose

The goal of this tutorial is to teach you how to setup your development environment and also to describe the layout of the barebones EMA NIP application we will use in this series of tutorials. This barebones appllication lays out the foundation of our EMA Non-Interactive Provider application we will build in the next tutorials.

To this aim we will go through the following:

Download the tutorials source code

The tutorials in this series rely on source code projects that demonstrate the purpose of each tutorial. These projects are contained in zip archive that can be downloaded here.

Before you move to the next section, please do the following:

  1. Download and extract EMA Java and its dependency libraries shipped with Elektron SDK Java package
  2. Download the tutorials series source code
  3. Extract the downloaded file in the folder of your choice

Once the file is extracted, the folder contains the scripts file for Windows(.bat) and Linux(.ksh). These script files support both Elektron SDK 1.1.x or lower and Elektron SDK 1.2.x or higher which are difference in directory structure and the libraries’ name. The folder also contain a src folder that contains the source files for each tutorial in this series.

Setup the development environment

The tutorials source code package provides the following convenient scripts:

  • buildNiProvider.bat and runNiProvider.bat for Windows
  • buildNiProvider.ksh and runNiProvider.ksh for Linux

Note: see Build and run the application below for more details.

Because EMA can be installed anywhere within your environment and EMA shipped with any version of Elektron SDK package can be used, our build and run scripts make an assumption about how to resolve the location of EMA's JAR files regardless any version of Elektron SDK package. While there are multiple ways to configure setup, this project assumes the following:

  • Elektron SDK for Java Home directory

    The environment variable ELEKTRON_JAVA_HOME is used within build and run scripts to resolve the location of your Elektron SDK installation. Defined for both the Windows and Linux scripts, this will allow developers to easily copy/duplicate projects to other members and to themselves without having to re-define directory locations within their own projects.

    Note: If the Elektron SDK - Java edition is not yet installed on your machine, please follow the steps of the Elektron Message API Quick Start guide.
  • Elektron SDK for Java version 1.2

    The environment variable ELEKTRON_JAVA_12 is used within build and run scripts to resolve the location of EMA’s JAR files in your Elektron SDK installation according to the version of Elektron SDK package.  The environment variable is required to be set because the directory structure and the name of EMA’s JAR files in Elektron SDK version 1.2.x or higher has changed from the version 1.1.x or lower. If Elektron SDK Java is version 1.2.x or higher, set this environment variable to be yes. Otherwise, set this environment variable to be no.
  • Java build environment

    The environment variable JAVA_HOME is used within the build and run scripts to resolve the location of your Java environment required to compile and run the tutorials.

Alternatively, you can modify the scripts to refer to your installation if you choose not to define environment variables.  The scripts refer to these references at the top of the files.  Once set up, future tutorials will work without further configuration.

Understanding the barebones application structure

The source code of this first tutorial is contained in the src\main\java\com\thomsonreuters\ema\tutorials\nip\tutorial1 folder. This project is made of two Java files (Main.java and NiProvider.java). We will update them as we progress along this series of tutorials. All along, we will keep this simple structure, with the responsibilities described below.

Main.java

This is the entry point of the application and where the main workflow is defined. In this first tutorial it does almost nothing. It just creates a NIProvider object and waits for 5 seconds before exiting. It also catches and prints any exception that would be raised by EMA.

 

public static void main(String[] args)
{
    try
    {
        System.out.println("-------------------------------------------------------------------------------");
        System.out.println("|                    Non Interactive Provider EMA Tutorial                    |");
        System.out.println("|                                                                             |");
        System.out.println("|              Tutorial 1 - A barebones EMA NIP application shell             |");
        System.out.println("-------------------------------------------------------------------------------");

        NiProvider provider = new NiProvider(); 

        waitFor(5);
    } 
    catch (OmmException exception)
    {
        System.out.println(exception.getMessage());
    }

    System.out.println("  Exiting the application");
}   

In later steps, we will complete this workflow with other publication actions that leverage the Elektron Message API. But we will take care of keeping it very simple and easy to read so that the main points of the workflow remain clearly visible. 

Main.java also defines the waitFor()  convenience static method that we extensively use throughout these tutorials to control the timing of the workflow.

NiProvider.java

NiProvider.java defines a class and the high level methods used by the main workflow. It is in this class that the interactions with the Elektron Message API mainly take place. You can consider this class as the interface between the application and the EMA library. You can refer to these methods when you want to better understand how to use EMA for performing specific actions like listening to connection events or updating an instrument.

In this first tutorial, the NiProvider class is just an empty shell that does nothing except write a message to the console when it’s created.

Build and run the application

The package includes convenient scripts to build and run the tutorials. For this series of tutorials:

To Build a specific tutorial:

  • > buildNiProvider.bat <1 to 9> - Windows
  • > sh buildNiProvider.ksh <1 to 9> - Linux

To Run a specific tutorial:

  • > runNiProvider.bat <1 to 9> - Windows
  • > sh runNiProvider.ksh <1 to 9> - Linux

Eg (Windows):

> buildNiProvider 1 

Building tutorial 1...

Done.



> runNiProvider 1

Running this tutorial will simply display the following:

-------------------------------------------------------------------------------
|                    Non Interactive Provider EMA Tutorial                    |
|                                                                             |
|              Tutorial 1 - A barebones EMA NIP application shell             |
-------------------------------------------------------------------------------
  Provider created
  Waiting for 5 seconds...
  Exiting the application

After 5 seconds the application should exit automatically.

Troubleshooting

Q: When I build or run the tutorial, it fails with an error like:

The system cannot find the path specified

A: The JAVA_HOME environment variable is not set, or set to the wrong path. See Setup the development environment.

 

Q: When I build the tutorial, I get ”<path>/javac: No such file or directory” or when I run the tutorial, I get  ”<path>/java: No such file or directory” error like

line 59: /home/user/jdk/bin/javac: No such file or directory

A: The JAVA_HOME environment variable is not set, or set to the wrong path. See Setup the development environment.

 

Q: When I build or run the tutorial, I get "Finding Jar files in <path>” and “The system cannot find the path specified.” errors like

Build the NIP application with Elektron SDK Java version 1.2.x or higher.

Finding Jar files in C:\Elektron-SDK\Java\Ema\Libs\
The system cannot find the path specified.

A: There are 2 possible causes:

 

Q: When I build or run the tutorial, I get "<path to /*jar> : No such file or directory” error like

/home/user/Elektron-SDK1.1.1.E2.java.eload/Java/Ema/Libs/*.jar: No such file or directory

A: There are 2 possible causes:

 

Q: When I build the tutorial, I get "package ... does not exist" and "cannot find symbol" errors like:

Main.java:20: error: package com.thomsonreuters.ema.access does not exist
import com.thomsonreuters.ema.access.OmmException;
                                    ^
Main.java:56: error: cannot find symbol
                catch (OmmException exception)
                       ^
  symbol:   class OmmException
  location: class Main

A: The ELEKTRON_JAVA_HOME environment variable is not set, or set to the wrong path. See Setup the development environment.

 

Q: When I run the tutorial, I get a JNI error with a NoClassDefFoundError exception like:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/thomsonreuters/ema/access/OmmException
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
        at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
        at java.lang.Class.getMethod0(Class.java:3018)
        at java.lang.Class.getMethod(Class.java:1784)
        at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.thomsonreuters.ema.access.OmmException
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 7 more

A: The ELEKTRON_JAVA_HOME environment variable is not set, or set to the wrong path. See Setup the development environment.

 

Tutorial Group: 
EMA NI Provider