简体   繁体   English

Java TCPIP EJB线程

[英]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. 我正在开发一个TCPIP应用程序,其中客户端将信息发送到指定的端口,服务器将侦听该端口。 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? EJB技术如何做到这一点? I know the while loop is wrong but what can be included. 我知道while循环是错误的,但是可以包含什么。 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. 我知道在EJB中有诸如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? 你能帮我改变while循环来做我想做的事情吗?

Also, the client has no API to call on this application. 同样,客户端没有API可以调用此应用程序。 How can this instance be triggered on start up of the Application Server so that messages are listened to. 如何在启动Application Server时触发此实例,以便侦听消息。 Usually, this could be achieved through a Servlet but I don't think this will work. 通常,这可以通过Servlet实现,但我认为这不会起作用。 Any ideas? 有任何想法吗?

This kind of functionality is at the "edge" of the types of things you would do in EJB. 这种功能处于您在EJB中要做的事情的“边缘”。 typically, EJBs do not directly do network type activity, especially long term listening. 通常,EJB不直接进行网络类型活动,尤其是长期监听。

what you really want is an EJB singleton. 您真正想要的是EJB单例。 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. 这有点像一个独立的实例,它可以执行更多高级功能,例如自定义并发/线程和(与您相关)启动自定义套接字侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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