Stop a Channel if it Errors - rbeckman-nextgen/test-mc GitHub Wiki

  1. Mirth Connect
  2. Home
  3. Examples and Tutorials

Mirth Connect : Stop a Channel if it Errors

Created by Jacob Brauer, last modified by Marc H on Jan 09, 2016

This example is a simple function that will stop a channel if it detects any errors. It is a simplified version of code written by glenn71 on the forums.

It consists of two parts. The first is a function stored in code templates, and the second is a function call to the first from a channel.

The goal of this setup is to provide a way to ensure that the messages are delivered in order, and if any message raises an error stop the channel. In order to restart the channel, the statistics will need to be cleared (at least for errors), as this just scans the total number of errors.

Now, onto the code.

In the postprocessor tab of the channel, put this code:

handleError(channelId);

This will call the function we'll be adding to the code templates and pass in the channelId of the current channel. Don't worry about trying to get the channelId from somewhere, it's already defined.

In the "Code Templates" section, define a new function with global scope.  The code to insert depends on Mirth Connect version:

Mirth >3.0.0, use ChannelUtil

function handleError(cid){
    if (ChannelUtil.getErrorCount(cid) > 0) {
        logger.info('Stopping channel "' + ChannelUtil.getChannelName(cid) + '" due to error count > 0');
        ChannelUtil.stopChannel(cid);
    }
}

Earlier versions (<3.0.0):

function handleError(cid){
    var channelStatisticsController = Packages.com.mirth.connect.server.controllers.ChannelStatisticsController.getInstance();
    var channelStatusController = Packages.com.mirth.connect.server.controllers.ChannelStatusController.getInstance(); 
    var stats = channelStatisticsController.getStatistics(cid);  
    //If there's errors, stop the channel
    if(stats.error > 0 ) channelStatusController.stopChannel(cid); 
}

It's important that you remember to clear the channel statistics before restarting the channel, as this checks the total number of errors, and does not reset the error count after stopping the channel.  If you restart the channel without clearing the error count, it will immediately stop again after one message is processed even if no further errors have occurred.

As a final note, if you'd like to have it stop after a predefined number of errors, change the 0 in the code to the maximum number of allowable errors. 

Comments:

Does not work in 3.2

Posted by spycom at Mar 06, 2015 06:45

In 3.x use ChannelUtil instead: http://javadocs.mirthcorp.com/connect/3.2.0/user-api/com/mirth/connect/server/userutil/ChannelUtil.html

Posted by narupley at Mar 06, 2015 07:03

Code updated with example using ChannelUtil.

Posted by mjh_ca at Jan 09, 2016 13:11

in 3.2.2:

function handleError(cid){
    if (ChannelUtil.getErrorCount(cid) > 0) {
        logger.info('Stopping channel "' + ChannelUtil.getDeployedChannelName(cid) + '" due to error count > 0');
        ChannelUtil.stopChannel(cid);
    }
}

 

Posted by nawaf at Jan 18, 2016 22:46

Hello all,

This is really helpful. However, I have one doubt. In the code which is getting error count using inbuilt packages, there is no date range mentioned. I don't want to get full error count. I just need error count of last 2 days. How, can I give date range here in above handleError function ?

Any help will be appreciated.

Thank you.

Posted by rakesh_waghulde at Sep 30, 2017 11:16

Document generated by Confluence on Nov 11, 2019 08:40

Atlassian

⚠️ **GitHub.com Fallback** ⚠️