web stats
Skip to end of metadata
Go to start of metadata

How to invoke a Java class from a JS connector.

This mini guide aims to explain the FAQ about how to invoke a Java class from Mirth. It can be invoked from any connector (Preprocessor, Postprocessor, JS Transformer, JS Filter, JS Reader, JS Writer), the only restriction is that this connector must support JS.

As this mini guide is a remake of some (old) forum posts, I will use the same examples.

Requirements

Development environment working properly. Take a look to the eclipse guide (broken link)http://www.mirthproject.org/index.php?option=com_jd-wiki&Itemid=&id=eclipse for configuring eclipse, but this configuration is optional.

Creating the Java code

First step is (obviously) create the classes you want to call to from Mirth. There is no need to import some packages from Mirth's libs, extend from some class or implement any interface. A simple POJO will do the job.

In my example I will use this class:

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

Packing your code

Now let's pack our code into a jar file. Don't forget any class you need into the jar. In eclipse you can export as a jar file using rigth-click on the classes, export, jar file.

Installing and testing

  • Drop the jar created in the previous step into the lib/custom/ folder (for mirth connect < 2.0) or custom-lib (for mirth connect >= 2.0) under your Mirth Connect home directory. This way, the classloader should be able to locate it.
  • Restart Mirth and log in.
  • Define a test channel. I.E: channel reader, channel writer and JS Transformer step.
  • In the JS Transformer put a code like:

  • Now, send a dummy message throght the testing channel. If all the setup is fine, Mirth logs must show something like:

Notes

  • I've tested passing args from JS to Java and retrieving them from Java methods in JS code. For simple type like int, String ... etc is straightforward, for complex types I didn't tested yet, but I suppose that the solution is to create POJO's and use accessor methods to manage only simple types.
  • No labels

3 Comments

  1. If you are getting errors such as:

    TypeError: JavaPackage com.mycompany.utils.myclass is not a function, it is org.mozilla.javascript.NativeJavaPackage

    it may help to interpret it as "I can't find the class you're talking about."

    This rather misleading error can occur if the specified package doesn't exist or can't be found, e.g. you specify a completely bunk package name, have a typo, or if the package is invalid in some fashion. In my case I had unwittingly put my java source into my jar file instead of the compiled class, and got this error.

  2. Thank you, Jacob. The post is very helpful and it's exactly what I'm looking for. I'm using Mirth v2.1.1.5490, Windows7 and try to follow the instruction that you provided. Given that I have my jar exported and put in mirth/custom-lib. In the channel I made the destination connector to JS Writer and did the same as you did. But when I deploy and receive HL7 message, I didn't see the println in the logs/mirth file.
    I have a feeling that the script does executed correctly, since I tried to mispelled the method repeat it gave me error stating that repeate is not be found in the jar. Can you explain this behavior to me?

    N

  3. Article: How to invoke Java code from Mirth

    I have a question.  I am kind of a Mirth newbie, and I am puzzled as to the details on the last few steps of this page.  You kind of go a little fast for me.  What does it mean as far as where I click the mouse etc?  How do I do a Channel Reader, then a Channel Writer and then a JS Transformer?  I can't even find JS Transformer anywhere on the interface.  Is there some menu I need to click?

    I have a test class done up that sends whatever message is sent to the System.out.println and I ran the testing channel but I don't see my output anywhere.  Help please!  Thanks.