Thursday, August 24, 2006

Hl7 Simple server Implementation

/*
* HL7Server.java
*
* Created on August 12, 2006, 11:39 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package aaaaaaa;

import ca.uhn.hl7v2.app.Application;
import ca.uhn.hl7v2.app.Connection;
import ca.uhn.hl7v2.app.DefaultApplication;
import ca.uhn.hl7v2.app.Responder;
import ca.uhn.hl7v2.app.SimpleServer;
import ca.uhn.hl7v2.llp.LowerLayerProtocol;
import ca.uhn.hl7v2.parser.PipeParser;
import java.util.Vector;

/**
*
* @author srinivas
*/
public class MyHL7Service {

/** Creates a new instance of HL7Server */
public MyHL7Service() {
}
public static void main(String arg[]){
// 1 .In order to receive HL7 message create simple server object
// 2 .then write a class which implements Application intercafe and implement those methods accoding to ur needs
//and finally register Application obect with simpleserver
// 3. if u want a connection Listener thn implement ConnectionListener interface and register with simple server
//here i wrote implementations MisApplications and MISConnection Listener
//copy these 3 classes compile and run and this program is ready to receive the mesages
//u run amessage tester which wil send HL7 messages to the particular ip and port using
//java -jar hapi-0.5beta.jar

PipeParser pipe=new PipeParser();
SimpleServer simpleserver=new SimpleServer(2100,LowerLayerProtocol.makeLLP(),pipe);
simpleserver.start();
Application app=new MyApplication();
MyConnectionListener conlistener=new MyConnectionListener();
simpleserver.registerConnectionListener(conlistener);
simpleserver.registerApplication("*","*",app);
conlistener.showConnections();

}
}


2 . Connection Listener




/*
*
MyConnectionListener.java
*
* Created on August 14, 2006, 12:44 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package aaaaaaa;

import ca.uhn.hl7v2.app.Connection;
import ca.uhn.hl7v2.app.ConnectionListener;
import java.io.File;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;

/**
*
* @author srinivas
*/
public class
MyConnectionListener implements ConnectionListener{
Vector con=new Vector();

public
MyConnectionListener() {
}

public void connectionReceived(Connection connection) {
System.out.println(" Received from =="+connection.getRemoteAddress().getHostAddress());
con.add(connection.getRemoteAddress().getHostAddress());
System.out.println("got call to the server from ip=="+connection.getRemoteAddress().toString());
}
public void connectionDiscarded(Connection connection) {
for(String str:con){
if(str.equals(connection.getRemoteAddress().getHostAddress())){
con.remove(str);
System.out.println("Connection discarded ="+connection.getRemoteAddress().toString());

}
}

}
public void showConnections(){
System.out.println("=========Available connections are=======");
for(String str:con){

System.out.println(str);
}
System.out.println("=========================================");
}
}


3 Application
/*
* MyApplication.java
*
* Created on August 23, 2006, 10:00 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package aaaaaaa;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.app.Application;
import ca.uhn.hl7v2.app.ApplicationException;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.parser.DefaultXMLParser;
import ca.uhn.hl7v2.parser.PipeParser;
import ca.uhn.hl7v2.parser.XMLParser;

/**
*
* @author srinivas
*/
public class MyApplication implements Application{
String msg;
Message mesage;
/** Creates a new instance of MyApplication */
public MyApplication() {
}

public Message processMessage(Message message) throws ApplicationException, HL7Exception {
System.out.println("got call to theMisApplication ");
System.out.println("messageReceived *******************");
XMLParser xmlParser = new DefaultXMLParser();
PipeParser pipe=new PipeParser();
msg=pipe.encode(message);
msg.replace('\r', '\n');
String str=xmlParser.encode(message);
//Here im priinting the messages
// whrite the code and use this incoming message according to ur requirement
System.out.println(msg);
System.out.println(str);
return pipe.parse(msg);
}

public boolean canProcess(Message message) {
boolean flag=true;
PipeParser pipe=new PipeParser();
try{
msg=pipe.encode(message);
message= pipe.parse(msg);
}catch(Exception e){
flag=false;
e.printStackTrace();
}
System.out.println("got call to theMisApplication can process Method");
return flag;
}

}


copy the classes comlile and run u wil receive the hl7 messages .This u can modify according to ur needs

No comments: