Search code examples
javajava-ee-6netty

Create Netty Listeners in Java EE


We are currently developing a small application which needs to communicate with a machine interface via a propriety tcp protocol.

For this low level communication stuff we used Netty to implement the necessary encoders and decoders. Since we also need some Java EE things like WebService, JPA etc we thought about integrating the netty server in an Java EE 6 application. Therefore we would use an ApplicationScoped managed CDI bean, where the bootstrapping is triggered in a PostConstruct method and the unregistering is done in the PreDestroy callback.

So the main question is:

Would this lead to problems, since as far as I know it is technically not allowed to start threads in a Java EE environment (I think Netty starts some threads here)?

If yes, what kind of problems? Since we don't need clustering, we would just use a standard Java EE 6 app server like GlassFish.


Solution

  • Most people will recommend against it, since improper termination and resource lock-ups can lead to catastrophic results. However, if you know what you're doing, there is no reason not to.

    That said, based on what you need it for, I would recommend looking into Java Connector Architecture first. It already provides established contracts for connection, transaction, security, life-cycle, work, etc. management. So, you have a much better chance of writing a good implementation as well as transfer thread management to the container. See this and this to get you started.