Announcement

Collapse
No announcement yet.

Stop & Start Channel based on Certain Conditions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Stop & Start Channel based on Certain Conditions

    Hi,

    Can anyone suggest the best way to stop and start a channel based on a condition.

    In my case, I need to stop and start a channel if the queue message is > 0. The function will trigger once a message is received. So here's what I've tried:

    Here's what I have in the postprocessor of my channel:
    Code:
    var cid = 'channel ID';
    
    if (ChannelUtil.getQueuedCount(cid) > 0) {
         logger.info('Stopping channel "' + ChannelUtil.getChannelName(cid) + '" due to queue count > 0');
         ChannelUtil.stopChannel(cid);
         logger.info('start channel func');
         
    	ChannelUtil.startChannel(cid);
    }
    Problem with this is that I don't know if channel was actually stopped before it was started again.

    I was looking for a delay, wait or pause function. So far I found the java.lang.Thread.sleep() function. But this holds the actual process which mean when the stop function triggers and you set a sleep after it, the channel status will sleep to "Stopping".

    Basically, to have an accurate trigger of events, I need to wait for the channels to have a state of "Stopped" before I trigger the start function. I searched for this again and found the "Future" java class. Below are some links about it:
    http://javadocs.mirthcorp.com/connec...il/Future.html

    https://docs.oracle.com/javase/7/doc...nt/Future.html


    Unfortunately, I am not that familiar on the Java language. Does this Java class need to be compiled just like what is mentioned in this guide: http://www.mirthcorp.com/community/w...Mirth+Connect?

    I hope you can help me on this.
    Thanks.

  • #2
    Nope, you don't need to compile anything yourself to use ChannelUtil. You can call get() to wait until the operation finishes:

    Code:
    ChannelUtil.stopChannel(cid).get();
    ChannelUtil.startChannel(cid);
    If you need to have a channel restart itself, then do it inside a separate thread:

    Code:
    new java.lang.Thread({
        run:function() {
            ChannelUtil.stopChannel(cid).get();
            ChannelUtil.startChannel(cid);
        }
    }).start();
    Last edited by narupley; 07-12-2017, 07:00 AM.
    Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

    Nicholas Rupley
    Work: 949-237-6069
    Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


    - How do I foo?
    - You just bar.

    Comment


    • #3
      Thank you! Looks like you put me on the right track

      Comment

      Working...
      X