I solved this problem using Camel (http://camel.apache.org) with the component mina2 that allows to open these kind of listeners:
mina2:tcp://hostname[:port][?options]
mina2:udp://hostname[:port][?options]
mina2:vm://hostname[:port][?options]
Camel (What exactly is Apache Camel?) is an open source Java framework that focuses on making integration easier. With Camel you can define some routes and, in this case, your route can be something similar to this:
<route>
<from uri="mina2:tcp://localhost:2575?sync=true&codec=#hl7codec" />
<log message="[1] ********* MINA2 Message received *********" />
<to uri="direct:HL7Process"/>
</route>
<route>
<from uri="direct:HL7Process"/>
<log message="[2] ********* MINA2 processing Message" />
<bean ref="hl7Processor" method="removeUtf8Bom" />
<process ref="hl7Processor" />
<onException>
<exception>org.apache.camel.RuntimeCamelException</exception>
<exception>ca.uhn.hl7v2.HL7Exception</exception>
<redeliveryPolicy maximumRedeliveries="0" />
<handled>
<constant>true</constant>
</handled>
<log message="*[3] ******** MINA2 sending ACKError" />
<bean ref="hl7Processor" method="sendACKError" />
</onException>
<log message="[4] ********* MINA2 sending ACK" />
<bean ref="hl7Processor" method="sendACK" />
<log message="[4] ********* MINA2 Message processed" />
</route>
First part define a listener on tcp port to receive hl7 messages. HL7Process define what to do with the message you'll receive. hl7Processor can implement the hl7ToXML translator and the logic to store the messages on the database.