Java - 如何访问另一个正在运行的应用程序中的类

时间:2022-04-10 23:49:55

In my scenario, I have a server running which waits for a client to make a connection. It is necessary for a client to instantiate the connection because the computer I'm looking to communicate with has more restrictive network security than I do. The client will connect to me daily. So I have a jar file and process running that handles this. Server side on my end, and client side on the other end. Next, I have a web page running, and when a user clicks a specific button, I need to tell the Server app, "okay, now send data to the client".

在我的场景中,我有一个服务器正在运行,等待客户端建立连接。客户端必须实例化连接,因为我希望与之通信的计算机具有比我更严格的网络安全性。客户将每天与我联系。所以我有一个jar文件和进程运行来处理这个。服务器端在我的端,而客户端在另一端。接下来,我有一个网页正在运行,当用户点击特定按钮时,我需要告诉服务器应用程序,“好的,现在将数据发送到客户端”。

What is the best way to accomplish this? First of all, is there a problem with the client waiting around for data? Do I need anything special here like heartbeats etc? Is it possible to reverse the scenario and have a server connect to a client instead?

完成此任务的最佳方法是什么?首先,客户端是否存在等待数据的问题?在心跳等我需要什么特别的东西吗?是否可以反转方案并让服务器连接到客户端?

Next, how can I make it so that the application I launch when the user clicks a button actually talks to an already running jar which is handling the connection?

接下来,我如何才能使用户单击按钮时启动的应用程序实际与正在运行的正在处理连接的jar进行通信?

2 个解决方案

#1


0  

What you need is decide the transport layer. If you use RMI -which makes sense if you use Java in all the endpoints of comunication- you don't need the heatbeat or something.

您需要的是决定传输层。如果您使用RMI - 如果您在通信的所有端点中使用Java,那么这是有意义的 - 您不需要热点或其他东西。

I should define, in web-app context (are you using Spring?) a unique instance that initializes an RMI object and publishes it at RMI registry, so clients can connect to it.

我应该在web-app上下文(你使用Spring吗?)中定义一个初始化RMI对象并在RMI注册表中发布它的唯一实例,以便客户端可以连接到它。

As this object is created inside webapp process (and webapp classloader), it has access to some queue object you use. Then your webapp can add items to that queue.

由于此对象是在webapp进程(和webapp类加载器)中创建的,因此它可以访问您使用的某个队列对象。然后您的webapp可以将项添加到该队列。

Your RMI object should have to execute in a separate thread (don't create indiscriminate threads in Java EE, just one! or use some framework as Quartz) and consume from the blocking queue.

你的RMI对象应该在一个单独的线程中执行(不要在Java EE中创建不加思考的线程,只需要一个!或者使用一些框架作为Quartz)并从阻塞队列中消耗。

[webapp]
   configfile
   create thread with server object
      publish server object with getinfo method
      getinfo method consumes from queue (queue.take())


[client]
    connect via RMI to server object
    call method get info

If a client can stay a lot without reading a new item, and there are a LOT of clients, maybe it's worth to return null from the queue where there are no items (or it timeouts).

如果客户端可以在不读取新项目的情况下保持很多,并且有很多客户端,那么从没有项目(或超时)的队列返回null也许是值得的。

#2


0  

Client-Server communications can be easily achieved via a RESTful web service. And communications between applications on the same local machine can be easily achieved using sockets and a common communications protocol.

通过RESTful Web服务可以轻松实现客户端 - 服务器通信。使用套接字和通用通信协议可以轻松实现同一本地计算机上的应用程序之间的通信。

#1


0  

What you need is decide the transport layer. If you use RMI -which makes sense if you use Java in all the endpoints of comunication- you don't need the heatbeat or something.

您需要的是决定传输层。如果您使用RMI - 如果您在通信的所有端点中使用Java,那么这是有意义的 - 您不需要热点或其他东西。

I should define, in web-app context (are you using Spring?) a unique instance that initializes an RMI object and publishes it at RMI registry, so clients can connect to it.

我应该在web-app上下文(你使用Spring吗?)中定义一个初始化RMI对象并在RMI注册表中发布它的唯一实例,以便客户端可以连接到它。

As this object is created inside webapp process (and webapp classloader), it has access to some queue object you use. Then your webapp can add items to that queue.

由于此对象是在webapp进程(和webapp类加载器)中创建的,因此它可以访问您使用的某个队列对象。然后您的webapp可以将项添加到该队列。

Your RMI object should have to execute in a separate thread (don't create indiscriminate threads in Java EE, just one! or use some framework as Quartz) and consume from the blocking queue.

你的RMI对象应该在一个单独的线程中执行(不要在Java EE中创建不加思考的线程,只需要一个!或者使用一些框架作为Quartz)并从阻塞队列中消耗。

[webapp]
   configfile
   create thread with server object
      publish server object with getinfo method
      getinfo method consumes from queue (queue.take())


[client]
    connect via RMI to server object
    call method get info

If a client can stay a lot without reading a new item, and there are a LOT of clients, maybe it's worth to return null from the queue where there are no items (or it timeouts).

如果客户端可以在不读取新项目的情况下保持很多,并且有很多客户端,那么从没有项目(或超时)的队列返回null也许是值得的。

#2


0  

Client-Server communications can be easily achieved via a RESTful web service. And communications between applications on the same local machine can be easily achieved using sockets and a common communications protocol.

通过RESTful Web服务可以轻松实现客户端 - 服务器通信。使用套接字和通用通信协议可以轻松实现同一本地计算机上的应用程序之间的通信。