Fetching Data From APIGEE and pushing into Mirth Via RabbitMQ

This will be a interesting blog post. In this post I will explain the following:

  1. Fetching the data from APIGEE (URL re-routing) in JAVA
  2. Pushing the fetched data from APIGEE and inserting to RabbitMQ queue.
  3. Pull the data from Rabbit MQ with Mirthconnect.

1. CREATE APIGEE URL:

APIGEE is available for both enterprise and normal non-payable version. You can sign into Apigee. There are various purposes and use-cases available to use Apigee but i’m currently using this Apigee as a URL re-routing mechanism.

Take any source of JSON data which can be available free. Sign in to APIGEE and click on API Proxies. In the opening page click on +PROXY button on the right hand side top. This will open a new page with the following information

apigee-1

Once the Above screen appears select the first option “REVERSE PROXY” this will act as a URL re-routing mechanism. You will have a actual URL but you will not use that URL for communicating with the clients instead, you will give one URL which which will be mapped to the original URL.

Click next on selecting the first option. Then you will see the below screen as shown:

apigee-2

In the above screen on Proxy Name you have to fill out the name that you wish to give as a proxy name in the first text box in my case I have provided (vibinces-eval-test). In Proxy Base Path  you need to provide a sub-context of your API name I have provided (apigeejsonprofile). In the Existing API you need to provide the full URL path of existing JSON API. Description is optional field, you can either provide it or not.

Once it is created my url looked like this http://vibinces-eval-test.apigee.net/apigeejsonprofile you might get a URL as well with the name of your choice. In the Security  tab, It is advised to select CORS headers on browse. Because it is always possible to get the cross-origin error when you try to access data from browsers which are not verified properly. Im also using no authorization for the API.

apigee-3

In the Next tab you can see how the data provided is converted to your URL. It is also fascinating that APIGEE provides you two types of URL that can be used both in Testing or BETA and as well as one for PROD.

apigee-4

Now your URL re-router is created. That is if  you hit the URL http://vibinces-eval-test.apigee.net/apigeejsonprofile you can see the JSON data that actually belongs to some other URL.

2. JAVA CODE – TO FETCH DATA FROM APIGEE:

I’m going to create the below two classes:

  1. FetchJsonFromApigee.java
  2. PushApigeeDataToRabbitMQ.java

1. FetchJsonFromApigee.java:

import java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class FetchJsonFromApigee {
public static String call_me() throws Exception {
String url = “http://vibinces-eval-test.apigee.net/apigeejsonprofile”;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod(“GET”);
con.setRequestProperty(“User-Agent”, “Mozilla/5.0”);
int responseCode = con.getResponseCode();
System.out.println(“Response Code : ” + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in .readLine()) != null) {
response.append(inputLine);
} in .close();
System.out.println(“response : ” + response.toString());
return response.toString();
}
public String sendingMessage() throws Exception {
String pushedJsonMessage = FetchJsonFromApigee.call_me();
return pushedJsonMessage;
}
}

2. PushApigeeDataToRabbitMQ.java:

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
* @author Vibinchander.V
*
*/
public class PushApigeeDataToRabbitMQ {
private final static String QUEUE_NAME = “TestQueuing”;

public static void passMessage(String message) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(“localhost”);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
channel.basicPublish(“”, QUEUE_NAME, null, message.getBytes());
System.out.println(” [x] Sent ‘” + message + “‘”);
channel.close();
connection.close();
}

public static void main(String[] args) throws IOException, TimeoutException {
FetchJsonFromApigee getData = new FetchJsonFromApigee();
String passMessage = null;
try {
passMessage = getData.sendingMessage();
} catch (Exception e) {
e.printStackTrace();
}
PushApigeeDataToRabbitMQ.passMessage(passMessage);
System.out.println(“Executed Main Method !!!”);
}
}

When you run the first class you can see that the data is fetched from APIGEE and pushed into RabbitMQ message queue.

3. Write a JAR file that will pull data from RabbitMQ:

I’m writing the below class. this class will be used inside the Mirth tool which will act as a consumer to pull the data out of RabbitMQ.

import java.io.IOException;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.QueueingConsumer;
/**
 *
@author Vibinchander.V
 *
 */
public class QueueConsumer {
 public String returnMessage(String queueName) throws IOException, TimeoutException {
  ConnectionFactory factory = new ConnectionFactory(); 
factory.setHost(“localhost”); 
Connection connection = factory.newConnection(); 
Channel channel = connection.createChannel(); 
channel.queueDeclare(queueName, false, false, false, null); 
boolean noAck = false; 
@SuppressWarnings(“deprecation”) 
QueueingConsumer consumerVal = new QueueingConsumer(channel);  channel.basicConsume(queueName, noAck, consumerVal); 
boolean runInfinite = true; 
QueueingConsumer.Delivery delivery = null; 
//while (runInfinite) {  //QueueingConsumer.Delivery delivery; 

try {   
delivery = consumerVal.nextDelivery(); 
} catch (InterruptedException ie)
{   // continue; 
}  //System.out.println(“Message received” + new String(delivery.getBody()));  channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); 
//} 
channel.close(); 
connection.close();
  return new String(delivery.getBody()); 
}
}

Make the above program in a JAR file put this inside the custom lib folder of Mirth. you might have to include other JAR files as well. Inside the Mirth source connector Javascript reader write the below code:

var queueConsumer = new org.envision.queuing.QueueConsumer();
msg = queueConsumer.returnMessage(“TestQueuing”);
logger.debug(msg);
return msg;

When you run the first FetchDataFromApigee.java you can see that data will be fetched from Apigee and pushed to RabbitMQ queue and immediately pulled by Mirth consumer.

Happy Integrations!!!!!

 

Advertisements

Connect Mirth with MongoDB

It is an essential feature to connect Mirth to any flat-file Database. The reason for the shift is because in future we are expecting data in the form of an non-aligned  structure like an API call.

Most of the business people now are using Healthcare market just to increase the care co-ordination they offer.To achieve this business need, they are targeting people with mobile applications. Looking at from this perspective, the front end of this mobile application depends on latest technologies like  ReactJS or AngularJS.  These technologies primarily focus on efficiently binding the received data. This data is usually in a JSON format.

From the interoperability perspective we can see the need for inserting incoming data to parse and provide them in the flat-file MongoDB.

I basically use Java to try out this and to implement them in Mirth.

Pre-requisite:

MongoDB – connectivity Jar : please download the jar file from here  Please  Download 2.11.1/  I was using this version as it a stable release. Download mongo-java-driver-2.11.1.jar This contains only the connectivity Jar to MongoDB

Install MongoDB : Make sure MongoDB is available in your system, if you are using Windows system then make sure you navigate to the MongoDB folder and open to run Mongod.exe this will make the mongo server to listen to the port 27017 by default. Then open run Mongo.exe this will open the MongoShell console.

——————————————————–

MongoDB :

—————– Create Database ————–

show dbs;
use FHIRtest
type : db;
you should get : FHIRtest

—————– Create Collection ————–

db.createCollection(“SampleFHIR”);
type : show collections;
you should get : SampleFHIR

—————– Insert Data ——————–

db.SampleFHIR.insertOne({“Data”:”PatientInfo- GETtest”});
type : db.SampleFHIR.find();
you should get : { “_id” : ObjectId(“57c45d8403114b848da5b516”), “Data” : “PatientInfo- GETtest”}

—————————————————

Now we have a Database ready with a collection in it, also it has one collection.

We will write a JAVA code, that will primarily read the data from the MongoDB and display it to check for the connection.

package com.fhir;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class FhirConnector {
public static void main(String[] args){
try{
//Connect to MongoDB client
MongoClient mongoClient = new MongoClient(“localhost”, 27017);
DB db = mongoClient.getDB(“FHIRtest”);
System.out.println(“- Database: ” + db);
//get collection data
DBCollection col = db.getCollection(“SampleFHIR”);
System.out.println(“- Collections: ” + col);
//Print Data in MongoDB
DBCursor cursor = col.find();
while(cursor.hasNext()) {
System.out.println(cursor.next());
}

}catch(IOException e){
throw new RuntimeException(e);
}
}
}

Output :

You should be getting an output like this:
consoleJava
Note: I have used a different collection name in my code, so the collection name is different in the console screen.

Now Let’s try to insert some data inside the same collection and then try to read the output. My making a a new code change in the above existing code.

package com.fhir;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class FhirConnector {
public static void main(String[] args){
try{
MongoClient mongoClient = new MongoClient(“localhost”, 27017);
DB db = mongoClient.getDB(“FHIRtest”);
System.out.println(“- Database: ” + db);

DBCollection col = db.getCollection(“FHIRdump”);
System.out.println(“- Collections: ” + col);

//Insert data into MongoDB
BasicDBObject document = new BasicDBObject();
document.put(“database”, “FHIRtest”);
document.put(“collection_mark”, “insertFHIR”);

BasicDBObject documentDetail = new BasicDBObject();
documentDetail.put(“records”, 99);
documentDetail.put(“index”, “vps_index1”);

document.put(“detail”, documentDetail);
col.insert(document);
System.out.println(“Inserted into DB”);

//Print Data in MongoDB
DBCursor cursor = col.find();
while(cursor.hasNext()) {
System.out.println(cursor.next());
}

}catch(IOException e){
throw new RuntimeException(e);
}
}
}

Output :

You should be getting a console output like this:
consoleJava2

In the Mongo shell you will get an output like this:

mongoshell output

Now we have written the Java code that will insert a data into MongoDB. Next step we will incorporate this into Mirth.To achieve this in Mirth, deploy the MongoDB connectivity Jar in Mirth custom-lib folder. and If you have the Mirth running, restart the service.

Channel :

Source : set the source as FHIR listener set the port of your convenience

Source Transformer : 
We will write a code here, that will fetch the incoming JSON data . We will send the data to Mirth via POSTMAN chrome application tool.

var jsonData = connectorMessage.getRawData();
var parseData = JSON.parse(jsonData)
// parsed Data contents
var status = parseData.text.status;
var resource= parseData.resourceType;
var identity = parseData.id;
var sex = parseData.gender;
var dob = parseData.birthDate;
logger.info(status+” “+resource+” “+identity+” “+sex+” “+dob);
channelMap.put(‘statusDetail’,status);
channelMap.put(‘resource’,resource);
channelMap.put(‘id’,identity);
channelMap.put(‘gender’,sex);
channelMap.put(‘DateofBirth’,dob);

Destination:
Now set the destination as Javasscript writer. and type the below code in the destination

try{
var mongoClient = new Packages.com.mongodb.MongoClient(“localhost”,27017);
var db = mongoClient.getDB(“FHIRtest”);
var col = db.getCollection(“SampleFHIR”);

// Insert data into MongoDB
var document = new Packages.com.mongodb.BasicDBObject();
document.put(“Status”,$(‘statusDetail’));
document.put(“resourceType”, $(‘resource’));

var documentDetail = new Packages.com.mongodb.BasicDBObject();
documentDetail.put(“identity”, $(‘id’));
documentDetail.put(“sex”, $(‘gender’));
documentDetail.put(“dob”, $(‘DateofBirth’));

document.put(“detail”, documentDetail);

col.insert(document);

//get Data from MongoDB
var cursor = col.find();
while(cursor.hasNext())
{
logger.info(“Inside Collection : “+cursor.next());
}

}catch(e){}

You can now deploy the channel, and pass the below JSON as the message in POSTMAN

{
“text”: {
“status”: “generated”
},
“resourceType”: “”,
“id”: “210321-120321”,
“gender”: “male”,
“birthDate”: “1994-01-04”
}

You will now get the following output in the Mongo Shell.

mongoshell output 2

You can see that data is a simple JSON. But you can see all the packages, databases  are named as FHIR, the reason behind this is, It is essential to put json to a flat-file format in a enhanced way. FHIR being transferred as both XML as well as JSON it is essential to know how well we handle this and store in a flatfile format.

In future any server side based codes can read this FHIR resources as it is from MongoDB put it as an API, from where all  the mobile technologies like AngularJS, ReactJS will parse the incoming API and publish it in a wonderful user viewing manner.

— Hope this is useful.

 

 

Blog at WordPress.com.

Up ↑