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.
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:
- 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.