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

huangapple go评论62阅读模式
英文:

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

问题

我想启动一个服务器,然后传递命令给它。我想从我的 main() 函数启动它,然后传递指令。问题是,当我启动服务器后,我的程序会等待我停止服务器以接收下一个指令。
如果可能的话,我希望避免创建一个新的类。
我的代码看起来像:

public static void main(String[] args) throws Exception {
    // 运行服务器
    Refine.main(args);
    
    // 要传递的命令

Refine 类来自于:https://github.com/OpenRefine/OpenRefine/blob/master/server/src/com/google/refine/Refine.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. 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.
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

答案1

得分: 1

I presume that Refine is your "server" main class and that calling Refine.main like that is equivalent to

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

In other words, your Refine class has a public static void main(String[] args) entrypoint method.

So how can you get this second main method start the server and then do something else while the server is running?

It depends on how Refine.main works.

  • 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.

  • 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.

UPDATE

Ah ... so this the com.google.refine.Refine from OpenRefine.

The Refine.main method is synchronous. 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?

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:

  1. In the way I suggested above.
  2. You could actually write your own modifications for the Refine class. Take a look at what it does.
英文:

I presume that Refine is your "server" main class and that calling Refine.main like that is equivalent to

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

In other words, your Refine class has a public static void main(String[] args) entrypoint method.

So how can you get this second main method start the server and then do something else while the server is running?

It depends on how Refine.main works.

  • 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.

  • 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.


UPDATE

Ah ... so this the com.google.refine.Refine from OpenRefine.

The Refine.main method is synchronous. 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?

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:

  1. In the way I suggested above.
  2. You could actually write your own modifications for the Refine class. Take a look at what it does.

huangapple
  • 本文由 发表于 2020年8月14日 15:41:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63408558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定