Announcement

Collapse
No announcement yet.

Mirth Connect and PubNub

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

  • Mirth Connect and PubNub

    Has anyone used Mirth to post messages to a pub-sub model API? Or specifically PubNub?

    I am having trouble with an asynchronous process that leads to a serious memory leak.

    Example destination:
    There are two prior destinations: 1) create filename and save to channel map, and 2) create blank placeholder file for later.
    The code below is the portion of our JS destination that calls out into the custom Java package for the PubNub API.
    Code:
     importPackage(<customJavaPackage>);
    
    // PubNub vars
    var subscribeKey= new java.lang.String("<subscriberKeyString>");
    var publishKey=new java.lang.String("<publishKeyString>");
    var cipherKey=new java.lang.String("<cipherKeyString>");
    var uuId = new java.lang.String("<uuIdString>");
    var pubnubChannel = new java.lang.String("<pubNubChannelString>");
    
    // Mirth vars
    var jsonBody= new java.lang.String($('jObj'));
    var filename = new java.lang.String("/data/mirthconnect/<filepath>/"+$('filename'));
    
    	// publish to PubNub
    	PubnubPublisher.main(subscribeKey, publishKey, cipherKey, uuId, jsonBody, pubnubChannel, filename);
    
    	var errFile = new java.io.File(filename);
    	java.lang.Thread.sleep(1000); //slow down for .async to finish, multi threaded;
    
    	// read file for errors
            <omitted for brevity ... reading in the errors returned by PubNub Java package>
    }
    try{
    	FileUtil.deleteFile(new java.io.File(filename));
    	}catch(e){
    		channelMap.put("filedeletion error",e.toString);}
    if ($('stack trace') != ""){throw("Publishing to Pubnub Failed");}

    Example PubNub Java package:
    This was exported as a runnable JAR and included in the Mirthconnect .../custom-lib/ resource directory (Settings>>Resources>>Directory).
    Code:
    package <name>.pubnub;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.StringWriter;
    import java.util.Calendar;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
    import com.pubnub.api.PNConfiguration;
    import com.pubnub.api.PubNub;
    import com.pubnub.api.PubNubException;
    import com.pubnub.api.callbacks.PNCallback;
    import com.pubnub.api.enums.PNLogVerbosity;
    import com.pubnub.api.models.consumer.PNPublishResult;
    import com.pubnub.api.models.consumer.PNStatus;
    import java.util.logging.*;
    
    public class PubnubPublisher {
    	public static void main(String subscribeKey, String publishKey, String cipherKey, String uuId,
    			String json, String channelName, String messageFile) throws PubNubException, ParseException {
    			//read in the file name
    			JSONParser parser = new JSONParser();
    			JSONObject jsonBody = (JSONObject) parser.parse(json);
    			final String filename = messageFile;
    			
    			//create pubnub configuration
    			PNConfiguration pnConfiguration = new PNConfiguration(); 
    			pnConfiguration.setSubscribeKey(subscribeKey);
    			pnConfiguration.setPublishKey(publishKey);
    			pnConfiguration.setCipherKey(cipherKey);
    			pnConfiguration.setUuid(uuId);
    			pnConfiguration.setSecure(true);
    			
    			PubNub pubnub = new PubNub(pnConfiguration); // initialize pubnub
    			
    			//publish asynchronously
    			pubnub.publish().message(jsonBody).channel(channelName) // publish						// channel
    					.usePOST(true).async(new PNCallback<PNPublishResult>() {
    						@Override
    						public void onResponse(PNPublishResult result, PNStatus status) {
    							int statusCode = status.getStatusCode();//200 for success, 400 for failure		
    							Long timetoken= null; //only returned on success									
    							String stackTrace = null; //only returned on failure								 
    							if (status.isError()) {  //if error
    								try { //stack trace to string
    								StringWriter sw= new java.io.StringWriter();
    								PrintWriter pw = new java.io.PrintWriter(sw);
    								status.getErrorData().getThrowable().printStackTrace(pw);
    								stackTrace=sw.toString();
    									sw.close();
    									pw.close();
    								} catch (IOException e) {
    								}
    							}else{ //if success
    								timetoken = result.getTimetoken();
    							}
    							try { //write output to a file
    								<omitted for brevity ... custom error handling writing to a file>
    								writer.close();
    							} catch (IOException e) {
    							}
    						}
    					});
    
    	}
    
    }

    Posting to PubNub is asynchronous, so the Java package makes a post with one process, and we have to have a delay within the Javascript destination to wait for that to complete.
    The kicked off Java process or file writer must not clean everything up, since there is a significant memory leak.
    Have anyone A) integrated with PubNub, B) sent HL7 messages to a publish/subscribe API model, or C) had issues with background jobs in Mirth?
    Eric Richards | Datica | Madison, WI

  • #2
    It sounds like you are looking for JMS Listener. I have no exposure to that though.
    Last edited by siddharth; 08-01-2017, 02:18 AM. Reason: foo
    HL7v2.7 Certified Control Specialist!

    Comment

    Working...
    X