This guide will show you how to create a custom Java class, compile/package it into a JAR, include it in Mirth Connect, and invoke it in JavaScript. Your custom code can be invoked from any JavaScript context, including the global/channel scripts, filters/transformers, and JavaScript connectors.

Requirements

A working development environment, including the JDK (Java Development Kit). Generally you should use the same version of Java that your Mirth Connect server is using.

You can look here to see how to set up Eclipse to checkout and develop Mirth Connect, but it's an optional step. For this example we'll just use a single .java file and a command-line terminal.

Creating the Java code

First create the class(es) you want to invoke from Mirth Connect. You may import internal Mirth Connect packages if you need to extend some class or implement some interface (see here for an example of extending AcceptMessage for a custom Web Service Listener), but you certainly don't need to. This example will just use a simple POJO called JarTest:

package com.example;

public class JarTest {

    public String returnSomething() {
        return "Hello, World!";
    }
}

Note that it must have the package name declared. You can implement your code in a single class or using many classes.

Using a package directory structure

Put your newly created JarTest.java file in the following structure:

Note how there are two top-level folders, src and classes. The src folder will contain your source files (.java), and the classes folder will contain your compiled class files (.class).

Compiling your code

Now you need to convert your source code into compiled class files, which will be included in the JAR. In a command-line terminal, navigate to the top-level folder that contains your src and classes folders. Then do this:

javac -d classes src/com/example/*

If you need to include other libraries in the classpath, use the -cp option. Type "javac -help" for the full set of options.

Creating the JAR file

Now you should have JarTest.class in the classes/com/example folder, so the next step is to archive it into a JAR file. In the same top-level directory as before, do this:

jar -cf JarTest.jar -C classes com

The -c option means you're creating a new archive, and -f specifies the filename to output with. The -C option changes the working directory before adding files. Finally, the "com" at the end indicates what files/folders to include in the archive.

You can also compile source files and create a JAR in one step in Eclipse, with the export feature.

Installing and testing

You will be able to use the custom class anywhere JavaScript is used. For example, you can create a new channel that uses a JavaScript Writer destination, and use the following code:

var obj = new Packages.com.example.JarTest();
logger.info(obj.returnSomething());

At first this channel won't yet work; we need to include the JAR that we just created.

Mirth Connect 3.1.1 or earlier

Mirth Connect 3.2.0 or later

 

Now that you've included the JAR, send a message through the channel. You should see something like this in the server log:

[2015-05-12 10:45:54,666]  INFO  (js-connector:?): Hello, World!