I got it to work. Not sure I understand exactly, but I am guessing it has 
something to do with when the appserver/bean agree that the objects truly come 
into existence.
 
Ultimately this shows how helpful the HAPI Library is.  The amount of code I 
had to write to get an essential part of HL7 message transport functional is 
small thanks to the work done by those contributing to the library.
 
More work ahead for me as I ensure that it is robust enough to use in 
production.
 
Thanks
Ian
 
Here is the code:
 
-----
The Bean to set up the listener
-----
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.gov.qld.health.sit.hl7tcp;
 
import ca.uhn.hl7v2.app.SimpleServer;
import ca.uhn.hl7v2.llp.LowerLayerProtocol;
import ca.uhn.hl7v2.parser.PipeParser;
import ca.uhn.hl7v2.validation.impl.NoValidation;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
 
/**
 *
 * @author Ian
 */
@Startup
@Singleton
public class Hl7InBean {
 
    @Resource(mappedName = "jms/qHl7InboundConnectionFactory")
    private QueueConnectionFactory cfHl7In;
    @Resource(mappedName = "jms/qHl7Inbound")
    private Queue qHl7Inbound;
    private SimpleServer ss = null;
    private static final Logger logger = 
Logger.getLogger(Hl7InBean.class.getName());
    private Connection hl7Connection;
    private Session hl7Session;
 
    @PostConstruct
    public void StartListening() {
        try {
            logger.log(Level.INFO, "Inbound session has started");
            Hl7InProcessor processor = new Hl7InProcessor();
            this.hl7Connection = cfHl7In.createConnection();
            this.hl7Session = hl7Connection.createSession(false, 
Session.AUTO_ACKNOWLEDGE);
            processor.setHl7Session(this.hl7Session);
            if (qHl7Inbound == null) {
                logger.log(Level.SEVERE,"qHl7Inbound object is null");
            } else {
                logger.log(Level.INFO,"qHl7Inbound has a value: " + 
qHl7Inbound.getQueueName());
            }
            processor.setHl7Inbound(qHl7Inbound);
 
            PipeParser pp = new PipeParser();
            pp.setValidationContext(new NoValidation());
            // Create a new SimpleServer that listens on port 8888
            ss = new SimpleServer(8888, LowerLayerProtocol.makeLLP(), pp);
            // Create and register Hl7InProcessor that implements messages
            ss.registerApplication("*", "*", processor);
            ss.start();
 
        } catch (JMSException ex) {
            Logger.getLogger(Hl7InBean.class.getName()).log(Level.SEVERE, null, 
ex);
        }
    }
 
    @PreDestroy
    public void Cleanup() {
        ss.stop();
    }
}
 

-----
The Processor
-----
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.gov.qld.health.sit.hl7tcp;
 
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Message;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import ca.uhn.hl7v2.app.Application;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
 
/**
 *
 * @author Ian
 */
public class Hl7InProcessor implements Application {
 
    private static final Logger logger = 
Logger.getLogger(Hl7InProcessor.class.getName());
    private Queue qHl7Inbound;
    private Session hl7Session;
    private MessageProducer messageProducer;
 
    public void setHl7Inbound(Queue qHl7Inbound) {
        this.qHl7Inbound = qHl7Inbound;
    }
 
    public void setHl7Session(Session hl7Session) {
        this.hl7Session = hl7Session;
    }
 
    public Hl7InProcessor() {
            logger.log(Level.INFO, "Hl7InProcessor is being constructed");
    }
 
    @Override
    public Message processMessage(Message msg) {
            Message ack = null;
        try {
            logger.log(Level.INFO, "Received Message");
            logger.log(Level.INFO, msg.encode());
            logger.log(Level.INFO,"Attempt to create a jms textmessage from the 
session");
            TextMessage textMessage = hl7Session.createTextMessage();
            textMessage.setText(msg.encode());
            logger.log(Level.INFO,"About to attempt send to queue");
            this.messageProducer = hl7Session.createProducer(qHl7Inbound);
            logger.log(Level.INFO,"Message producer destination: " + 
this.messageProducer.getDestination());
            messageProducer.send(textMessage);
            logger.log(Level.INFO,"It's supposed to have been sent");
            ack = msg.generateACK();
        } catch (JMSException ex) {
            Logger.getLogger(Hl7InProcessor.class.getName()).log(Level.SEVERE, 
null, ex);
        } catch (IOException ex) {
            logger.log(Level.SEVERE, null, ex);
        } catch (HL7Exception ex) {
            logger.log(Level.SEVERE, null, ex);
        }
        return ack;
    }
 
    @Override
    public boolean canProcess(Message msg) {
        return true;
    }
 
}



********************************************************************************
This email, including any attachments sent with it, is confidential and for the 
sole use of the intended recipient(s). This confidentiality is not waived or 
lost, if you receive it and you are not the intended recipient(s), or if it is 
transmitted/received in error.
Any unauthorised use, alteration, disclosure, distribution or review of this 
email is strictly prohibited.  The information contained in this email, 
including any attachment sent with it, may be subject to a statutory duty of 
confidentiality if it relates to health service matters.
If you are not the intended recipient(s), or if you have received this email in 
error, you are asked to immediately notify the sender by telephone collect on 
Australia +61 1800 198 175 or by return email.  You should also delete this 
email, and any copies, from your computer system network and destroy any hard 
copies produced.
If not an intended recipient of this email, you must not copy, distribute or 
take any action(s) that relies on it; any form of disclosure, modification, 
distribution and/or publication of this email is also prohibited.
Although Queensland Health takes all reasonable steps to ensure this email does 
not contain malicious software, Queensland Health does not accept 
responsibility for the consequences if any person's computer inadvertently 
suffers any disruption to services, loss of information, harm or is infected 
with a virus, other malicious computer programme or code that may occur as a 
consequence of receiving this email.
Unless stated otherwise, this email represents only the views of the sender and 
not the views of the Queensland Government.
**********************************************************************************

------------------------------------------------------------------------------
Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
infrastructure or vast IT resources to deliver seamless, secure access to
virtual desktops. With this all-in-one solution, easily deploy virtual 
desktops for less than the cost of PCs and save 60% on VDI infrastructure 
costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox
_______________________________________________
Hl7api-devel mailing list
Hl7api-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/hl7api-devel

Reply via email to