Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

an EAR (Java EE) application which listen to a socket request

I want to build a Java EE application (EAR) which not only provides web service(WAR) or direct JMS request (EJB), but I would like to also accept the socket request (e.g. UDP packet).

I have tried writing a listener with java.net.DatagramSocket, letting it run as separate process, and redirecting the request to my EAR application.

the question is.. how can I build such socket listener into my Java EE (EAR) applcation seamlessly?

thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The right approach would be to create a JCA adapter for that. JCA adapter can be used for outbound or inbound connectivity. You're allowed to start thread or schedule work in a JCA adpater. The inbound connectivity from the JCA adapter to the EJB is done using custom message-driven bean.

  • The adapter start a socket listener and manage the connection from remote clients
  • When a packet is received, a message is delivered to a custom MDB
  • The MDB can then delegate the processing to other EJBs

You can even start transaction from the JCA connector so that the delivery of the message/packet to the EJB is transacted. JCA is part of the Java EE specifications and is supported by all application servers.

Another approach (but not compliant with the spec), is to start the thread that listens to the socket from a ServletContextListener. The thread will run in the web layer and you can call the EJB as usual. Dependency injection will not work, but JNDI lookup should still be ok.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...