如何从另一个程序启动一个程序

时间:2022-03-29 20:49:10

I have two projects, project1 and project2.

我有两个项目,project1和project2。

Case 1: Project1 and Project2 are on the same machine. If project2 crashes or terminates, Project1 should restart the project2.

案例1:Project1和Project2在同一台机器上。如果project2崩溃或终止,Project1应重新启动project2。

Case 2: Project1 and Project2 are on different machine. If project2 crashes or terminates, Project1 should restart the project2.

案例2:Project1和Project2在不同的机器上。如果project2崩溃或终止,Project1应重新启动project2。

These are java project.

这些是java项目。

How should I perform the aforementioned task?

我该如何执行上述任务?

EDIT:

编辑:

I was thinking of the following solutions:

我在考虑以下解决方案:

  1. If the projects are on the same machine then I can keep the Project2 in the classpath of Project1 and restart the project2 from project1. But, there is a problem. Then these two entities will become a single program.
  2. 如果项目在同一台机器上,那么我可以将Project2保存在Project1的类路径中,并从project1重新启动project2。但有个问题。然后这两个实体将成为一个单一的程序。
  3. If these projects are on different computers then I could have some third program running along with project2 which I will assume will never crash. And this third program could revive the project2.
  4. 如果这些项目在不同的计算机上,那么我可以让第三个程序与project2一起运行,我将假设它永远不会崩溃。第三个项目可以重振项目2。

Not sure, about my solution. Your input is required.

不确定,关于我的解决方案。您的输入是必需的。

2 个解决方案

#1


2  

We use RabbitMQ: The Project1 send requests about task using RabbitMQ and Project2 send notifications about his job unsing RabbitMQ. If Project2 didn't send notification during some time (for example, during 30 seconds) Project1 start new Project2 instance just using OS command or send requests Project3 that in some machine like Project1 and using only for starting Project1 instances using OS command.

我们使用RabbitMQ:Project1使用RabbitMQ和Project2发送有关任务的请求,发送有关他的作业的未通知RabbitMQ的通知。如果Project2在一段时间内没有发送通知(例如,在30秒内),则Project1仅使用OS命令启动新的Project2实例,或者在某些机器(如Project1)中发送请求Project3,并仅使用OS命令启动Project1实例。

Of course, you can use any message brokers, not only RabbitMQ.

当然,您可以使用任何消息代理,而不仅仅是RabbitMQ。

But, there is a problem. Then these two entities will become a single program.

但有个问题。然后这两个实体将成为一个单一的程序。

No, mush better start all project as separate programs, if the Project2 has Out Of Memory error or so on, the Project1 also died. It isn't a problem at all runing Java application using ProcessBuilder or so on.

不,最好将所有项目作为单独的程序启动,如果Project2出现Out Of Memory错误等,Project1也会死亡。使用ProcessBuilder等运行Java应用程序并不是问题。

#2


1  

Case 1: one app can start another: ProcessBuilder

案例1:一个应用程序可以启动另一个:ProcessBuilder

Executing another application from Java

从Java执行另一个应用程序

Case 2: quasi-impossible as asked. You need something running in the machine of project 2.

案例2:按要求准几乎不可能。你需要在项目2的机器上运行一些东西。

#1


2  

We use RabbitMQ: The Project1 send requests about task using RabbitMQ and Project2 send notifications about his job unsing RabbitMQ. If Project2 didn't send notification during some time (for example, during 30 seconds) Project1 start new Project2 instance just using OS command or send requests Project3 that in some machine like Project1 and using only for starting Project1 instances using OS command.

我们使用RabbitMQ:Project1使用RabbitMQ和Project2发送有关任务的请求,发送有关他的作业的未通知RabbitMQ的通知。如果Project2在一段时间内没有发送通知(例如,在30秒内),则Project1仅使用OS命令启动新的Project2实例,或者在某些机器(如Project1)中发送请求Project3,并仅使用OS命令启动Project1实例。

Of course, you can use any message brokers, not only RabbitMQ.

当然,您可以使用任何消息代理,而不仅仅是RabbitMQ。

But, there is a problem. Then these two entities will become a single program.

但有个问题。然后这两个实体将成为一个单一的程序。

No, mush better start all project as separate programs, if the Project2 has Out Of Memory error or so on, the Project1 also died. It isn't a problem at all runing Java application using ProcessBuilder or so on.

不,最好将所有项目作为单独的程序启动,如果Project2出现Out Of Memory错误等,Project1也会死亡。使用ProcessBuilder等运行Java应用程序并不是问题。

#2


1  

Case 1: one app can start another: ProcessBuilder

案例1:一个应用程序可以启动另一个:ProcessBuilder

Executing another application from Java

从Java执行另一个应用程序

Case 2: quasi-impossible as asked. You need something running in the machine of project 2.

案例2:按要求准几乎不可能。你需要在项目2的机器上运行一些东西。