简体   繁体   中英

Java TCPIP EJB Threads

I am developing a TCPIP application where the client will send information to a specified port and the server will listen to that port. I would like to achieve the following:

Reconnect to the the client/port to see whether it is active after a specified time period.

I have the below code implemented:

@Stateless
@Local
public Listener implements ConnectionListener {

    public void listen() throws Exception {
         ServerSocket serverSocket = new ServerSocket(somePort);
         Socket socket = serverSocket.accept();
         while(!socket.isClosed()) {

         }
    }
}

public interface ConnectionListener {

    public void listen() throws Exception;
}

How can this be achived with EJB technology? I know the while loop is wrong but what can be included. I have two approaches:

  • Wait for a time period to reconnect
  • Thread

However, I do not wish to use the Thread appraoch.

I know in EJB there are things such as an EJBTimer. What would be the best way to implement this and how could this be implemented in the code. Could you help how I could change the while loop to do what I want it to do?

Also, the client has no API to call on this application. How can this instance be triggered on start up of the Application Server so that messages are listened to. Usually, this could be achieved through a Servlet but I don't think this will work. Any ideas?

This kind of functionality is at the "edge" of the types of things you would do in EJB. typically, EJBs do not directly do network type activity, especially long term listening.

what you really want is an EJB singleton. this is kind of like a standalone instance which can do more advanced things like custom concurrency/threading and (relevant to you) start a custom socket listener.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM