ron
ron

Reputation: 9364

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

I need to have a business logic running inside GlassFish 2.1 Appserver, which listens for inbound TCP connections and serves them. I feel that this kind of task is not really fit inside the appserver - maybe I should publish web service interfaces, etc, but I can't, at least not directly for the client.

The client will connect to my app via TCP, and will exchange plain-text commands and responses.

Do I need an external mediator program which translates the client TCP to rmi calls? Or does Java EE has native support for listening on sockets and doing direct I/O on them?

Upvotes: 4

Views: 2046

Answers (4)

Badal
Badal

Reputation: 4188

I have used somewhat similar solution as per my need. Might be this can help someone here around.

You need to Start a new Socket Port on server start up or on application load. I used @scheduler annotation whereas you can use listener based solution as well.

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

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

In this way, you can have TCP traffic on port 9999, where as your app server will continue to run on different port as normal.

Upvotes: 0

fvu
fvu

Reputation: 32953

JCA 1.5 is the standard solution for this kind of tasks, but it's not the easiest part of Java EE, and you won't find tons of examples to get you started. You could have a look at lifecycle modules if you don't mind a Glassfish specific solution, and JAFS, an ftp server that can be embedded in the 'fish probably contains a lot of inspiration to get you started.

Upvotes: 3

Michael Wiles
Michael Wiles

Reputation: 21186

You could also consider using BPEL - the openesb solution has bpel modules for listening via sockets.

Upvotes: 0

skaffman
skaffman

Reputation: 403501

JavaEE has no explicit support for raw socket communication, but in principle there's nothing to stop you using java.net.Socket directly. Glassfish may potentially block this, if the security manager is configured to restrict socket access, but that shouldn't be the case unless it's been explicitly configured that way.

Upvotes: -1

Related Questions