How to create an application in Java EE that listens for an incoming TCP / IP socket request?

I need to have business logic that runs in GlassFish 2.1 Appserver that listens for incoming TCP connections and serves them. I feel that such a task is not entirely suitable for the application server - perhaps I should publish web service interfaces, etc., but I cannot, at least not directly to the client.

The client will connect to my application via TCP and will exchange text commands and responses.

Do I need an external mediator program that translates client TCP to rmi calls? Or does Java EE have built-in support for listening on sockets and doing direct I / O on them?

+4
source share
4 answers

JCA 1.5 is the standard solution for such tasks, but it is not the easiest part of Java EE, and you will not find many examples to get started. You can take a look at the life-cycle modules if you are not opposed to the Glassfish special solution, and JAFS , the ftp server that can be built into fish, probably contains a lot of inspiration to get you started.

+3
source

JavaEE does not have explicit support for exchanging raw sockets, but basically you have nothing to do with java.net.Socket . Glassfish can potentially block this if the security manager is configured to restrict access to the socket, but this should not be the case if it is not explicitly configured in this way.

0
source

You can also consider using BPEL - openesb has bpel modules for listening on sockets.

0
source

I used a slightly similar solution according to my need. Perhaps this may help someone here.

You need to start a new Socket port when starting the server or when loading the application. I used the @scheduler annotation, whereas you can also use a listener based solution.

 @Scheduled(fixedDelay = 1000 * 60 * 60 * 24 * 365) public void startListenerPort() { ServerSocket socket = new ServerSocket(9999); // do some stuff here } 

Just make sure that you allow TCP traffic on the port that you assigned to Socket (Firewall Settings).

Thus, you can have TCP traffic on port 9999, where your application server will continue to work on another port, as usual.

0
source

All Articles