简体   繁体   English

如何从 Java 同时启动服务器和传递命令?

[英]How do I start server and pass commands simultaneously from Java?

I want to start a server and then pass commands to it.我想启动一个服务器,然后将命令传递给它。 I want to start it from my main() and then pass the instructions.我想从我的main()开始,然后传递指令。 The issue is that when I start the server, then my program is waiting that I stop the server to take next instructions.问题是当我启动服务器时,我的程序正在等待我停止服务器以获取下一个指令。 If possible, I would like to avoid to create a new class.如果可能的话,我想避免创建一个新的 class。 My code looks like:我的代码如下所示:

public static void main(String[] args) throws Exception {
    // run server
    Refine.main(args);


    //commands to pass

The Refine class comes from: https://github.com/OpenRefine/OpenRefine/blob/master/server/src/com/google/refine/Refine.java Refine class 来自: https://github.com/OpenRefine/OpenRefine/blob/master/server/src/com/google/refine/Refine.Z93F725A07423FE1C889F448B33D

I presume that Refine is your "server" main class and that calling Refine.main like that is equivalent to我认为Refine是您的“服务器”主 class 并且像这样调用Refine.main相当于

$ java -cp ... Refine arg1 arg2 ...

In other words, your Refine class has a public static void main(String[] args) entrypoint method.换句话说,您的Refine class 有一个public static void main(String[] args)入口点方法。

So how can you get this second main method start the server and then do something else while the server is running?那么如何让第二个main方法启动服务器,然后在服务器运行时执行其他操作呢?

It depends on how Refine.main works.这取决于Refine.main工作方式。

  • If it works by starting the server on another thread and then returning, then this may work:如果它通过在另一个线程上启动服务器然后返回来工作,那么这可能工作:

     public static void main(String[] args) throws Exception { Refine.main(args); // find handle for service // send commands. }
  • If not, then you may need to do something like this:如果没有,那么您可能需要执行以下操作:

     public static void main(String[] args) throws Exception { // start server in child thread new Thread(() -> Refine.main(args)).start(); // find handle for service // send commands. }

But in either case, there are other problems to solve:但无论哪种情况,都有其他问题需要解决:

  • You need to be able to obtain the handle (or URL, or whatever) for the server so that you can send it commands.您需要能够获取服务器的句柄(或 URL 或其他),以便您可以向其发送命令。

  • You need a way to wait until the server is ready before sending it commands.您需要一种方法来等到服务器准备好后再发送命令。

The solutions to those will depend on how the Refine class is implemented.这些解决方案将取决于如何实现Refine class。


UPDATE更新

Ah... so this the com.google.refine.Refine from OpenRefine.啊...所以这是来自com.google.refine.Refine的 com.google.refine.Refine。

The Refine.main method is synchronous. Refine.main方法是同步的。 If you look at the source code you will see that it is running the server on a private child thread and then waiting for that thread to terminate.如果您查看源代码,您会看到它在私有子线程上运行服务器,然后等待该线程终止。 In other words, it behaves in the second way that I postulated above.换句话说,它以我上面假设的第二种方式运行。

So to answer your question:所以回答你的问题:

Q: Is it possible to do what you are trying to do without writing your own class?问:是否可以在不编写自己的 class 的情况下做您想做的事情?

A: No. It is not possible.答:不,这是不可能的。

Once you accept that you will have to write a new class, then there are two ways to do it:一旦你接受你必须写一个新的 class,那么有两种方法可以做到:

  1. In the way I suggested above.按照我上面建议的方式。
  2. You could actually write your own modifications for the Refine class.您实际上可以为Refine class 编写自己的修改。 Take a look at what it does.看看它的作用。

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

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