Controlling and Monitoring a Channel with the Statistics and Status Controllers - rbeckman-nextgen/test-mc GitHub Wiki

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

Mirth Connect : Controlling and Monitoring a Channel with the Statistics and Status Controllers

Created by Mike Precup, last modified by David Tombs on Mar 13, 2013

Mirth provides several controllers to monitor and control channels. The Status controller allows you to check whether a channel is currently running, paused, etc, and allows you to change the state of the channel. The Statistics controller allows you to check the current error, sent, received, filtered, and queued counts and to set them.

In both cases, you must first get an instance of the controller.

Getting an Instance of a Controller

//get an instance of the statistics controller
Packages.com.mirth.connect.server.controllers.ChannelStatisticsController.getInstance();
 
//get an instance of the status controller
Packages.com.mirth.connect.server.controllers.ChannelStatusController.getInstance(); 

 These controllers can access all of your channels. Let's start off with the Statistics controller and its functionality.

Its important functions include

  • ChannelStatistics getStatistics(String channelId)
  • incrementReceivedCount(String channelId)
  • incrementSentCount(String channelId)
  • incrementErrorCount(String channelId)
  • incrementFilteredCount(String channelId)
  • incrementQueuedCount(String channelId)
  • decrementReceivedCount(String channelId)
  • decrementSentCount(String channelId)
  • decrementErrorCount(String channelId)
  • decrementFilteredCount(String channelId)
  • decrementQueuedCount(String channelId)

Of these, 10 of them are fairly self-explanatory. They either increment or decrement a particular statistic for a particular channel. Since this can access all channels, a channelId must be specified. If called from a channel, you can use the variable channelId as the argument. It will already be defined in the channel. Here's an example of something you could put in your postprocessor:

 

Incrementing Filtered Count in the Postprocessor

var statsController = Packages.com.mirth.connect.server.controllers.ChannelStatisticsController.getInstance();
statsController.incrementFilteredCount(channelId);

 None of these functions are particularly useful, especially considering what getStatistics can do, but in the event you do need to increment or decrement, these do it. It will not decrement below 0.

The function getStatistics is more useful. It returns a ChannelStatistics object based on the channelId passed in. The ChannelStatistics object can only be used to get and set data for the channelId that was specified in getStatistics. The ChannelStatistics object contains seven variables. It also has getters and setters for them, but you should be able to view and edit them without needing to use those functions. The only useful function it contains is toString(), which behaves as a toString() function typically would.

ChannelStatistics Variables:

  • sent
  • received
  • error
  • filtered
  • queued
  • channelId
  • serverId

The top five variables correspond to the statistics on the dashboard. They can be get and set. The bottom two are exactly what their names imply, and can be read.

 

Alright, that wraps up the Statistics Controller. Now, onto the Status Controller.

Similarly to the Statistics Controller, the Status Controller can access all of your channels.

The functions I'll cover are as follows:

  • void startChannel(int channelId)
  • void stopChannel(int channelId)
  • void resumeChannel(int channelId)
  • void pauseChannel(int channelId
  • ChannelStatus.State getState(int channelId)

The first four change the channels state, and yet again require a channelId, since they can access all channels. The stopChannel function will call the resumeChannel function before running is the channel is currently paused. The fifth is used to read the current channel state.

By using both of these controllers, you can check and respond to channel states in your code. If, for instance, you have a channel with a filter to prevent certain types of messages coming through that aren't supposed to, you could check the number of filtered messages and stop the channel and create an error message if too many were appearing, because it would be a sign of something going wrong. 

 

Stop a Channel Based on Filtered Count

//get an instance of the statistics controller and of the channel statistics
statisticsController = Packages.com.mirth.connect.server.controllers.ChannelStatisticsController.getInstance();
channelStats = statisticsController.getStatistics(channelId);
 

//get an instance of the status controller
statusController = Packages.com.mirth.connect.server.controllers.ChannelStatusController.getInstance(); 
 
//Checks if there are more than 10 filtered messages
if(channelStats.filtered > 10)
{
    //Increments the error count
    channelStats.error++;
    //Stops the channel
    statusController.stopChannel(channelId);
    //Outputs to the server log on the Dashboard as an error
    java.lang.System.err.println("Too many filtered messages received");
}

Comments:

In version 3.x we should be use ChannelUtil

Posted by obsessio at Nov 28, 2015 13:44

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

Atlassian

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