gpt4 book ai didi

java - Executor 服务只执行第一个任务

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:45:52 29 4
gpt4 key购买 nike

我编写了一个程序来执行两个在 shell 前台运行的命令,直到在终端上按下 ^c。

外壳命令

./weed master -mdir=/var/lib/qualebs/weed

上面命令的输出是

qualebs@qualebs-HP-655-Notebook-PC:~$ cd weed_0.64_linux_386/
qualebs@qualebs-HP-655-Notebook-PC:~/weed_0.64_linux_386$ ./weed master -mdir=/var/lib/qualebs/weed -port=9333
I0518 00:40:16 24540 file_util.go:19] Folder /var/lib/qualebs/weed Permission: -rwxr-xr-x
I0518 00:40:16 24540 topology.go:84] Using default configurations.
I0518 00:40:16 24540 master_server.go:56] Volume Size Limit is 30000 MB
I0518 00:40:16 24540 master.go:66] Start Seaweed Master 0.64 at :9333
I0518 00:40:16 24540 raft_server.go:97] Recovered from log
I0518 00:40:22 24540 master_server.go:82] [ localhost:9333 ] localhost:9333 becomes leader.

然后我启动volume server,命令和输出分别如下

qualebs@qualebs-HP-655-Notebook-PC:~$ cd weed_0.64_linux_386/
qualebs@qualebs-HP-655-Notebook-PC:~/weed_0.64_linux_386$ ./weed volume -dir=/var/lib/qualebs/weed -port=9444
I0518 00:42:18 24583 file_util.go:19] Folder /var/lib/qualebs/weed Permission: -rwxr-xr-x
I0518 00:42:18 24583 volume.go:103] loading file /var/lib/qualebs/weed/1.idx readonly false
I0518 00:42:18 24583 store.go:212] data file /var/lib/qualebs/weed/1.dat, replicaPlacement=000 v=2 size=2021448 ttl=
I0518 00:42:18 24583 volume.go:103] loading file /var/lib/qualebs/weed/2.idx readonly false
I0518 00:42:18 24583 store.go:212] data file /var/lib/qualebs/weed/2.dat, replicaPlacement=000 v=2 size=728808 ttl=
I0518 00:42:18 24583 volume.go:103] loading file /var/lib/qualebs/weed/3.idx readonly false
I0518 00:42:18 24583 store.go:212] data file /var/lib/qualebs/weed/3.dat, replicaPlacement=000 v=2 size=1215160 ttl=
I0518 00:42:18 24583 volume.go:103] loading file /var/lib/qualebs/weed/4.idx readonly false
I0518 00:42:18 24583 store.go:212] data file /var/lib/qualebs/weed/4.dat, replicaPlacement=000 v=2 size=1272992 ttl=
I0518 00:42:18 24583 volume.go:103] loading file /var/lib/qualebs/weed/5.idx readonly false
I0518 00:42:18 24583 store.go:212] data file /var/lib/qualebs/weed/5.dat, replicaPlacement=000 v=2 size=404944 ttl=
I0518 00:42:18 24583 volume.go:103] loading file /var/lib/qualebs/weed/6.idx readonly false
I0518 00:42:18 24583 store.go:212] data file /var/lib/qualebs/weed/6.dat, replicaPlacement=000 v=2 size=834640 ttl=
I0518 00:42:18 24583 volume.go:103] loading file /var/lib/qualebs/weed/7.idx readonly false
I0518 00:42:18 24583 store.go:212] data file /var/lib/qualebs/weed/7.dat, replicaPlacement=000 v=2 size=588240 ttl=
I0518 00:42:18 24583 store.go:219] Store started on dir: /var/lib/qualebs/weed with 7 volumes max 7
I0518 00:42:18 24583 volume.go:90] Start Seaweed volume server 0.64 at 0.0.0.0:9444

自动启动主服务器和卷服务器的Java代码

public class ContextListener implements ServletContextListener {

private final ExecutorService executor = Executors.newFixedThreadPool(2);

@Override
public void contextInitialized(ServletContextEvent sce) {
final ServletContext servletContext = sce.getServletContext();
executor.execute(new WeedMaster());
executor.execute(new WeedVolume());
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
executor.shutdown();
try {
executor.awaitTermination(0, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
Logger.getLogger(ContextListener.class.getName()).log(Level.SEVERE, null, ex);
}
}

private class WeedMaster implements Runnable {

@Override
public void run() {
try {
List<String> command = new ArrayList<>();
command.add("./weed");
command.add("master");
command.add("-mdir=/var/lib/qualebs/weed");
command.add("port=9333");
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File("/home/qualebs/weed_0.64_linux_386/"));
Process start = pb.start();
start.waitFor();
} catch (IOException | InterruptedException ex) {
Logger.getLogger(ContextListener.class.getName()).log(Level.SEVERE, null, ex);
}
}

}

private class WeedVolume implements Runnable {

@Override
public void run() {
try {
List<String> command = new ArrayList<>();
command.add("./weed");
command.add("volume");
command.add("-dir=/var/lib/qualebs/weed");
command.add("port=9444");
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File("/home/qualebs/weed_0.64_linux_386/"));
Process start = pb.start();
start.waitFor();
} catch (IOException | InterruptedException ex) {
Logger.getLogger(ContextListener.class.getName()).log(Level.SEVERE, null, ex);
}
}

}
}

然后我将我的应用程序部署到服务器上,结果发现执行了第一个命令并且主服务器正在运行但没有卷服务器。为什么第二个任务没有执行?

当从终端执行这两个命令时,我可以看到 master 和 volume 都在运行

qualebs@qualebs-HP-655-Notebook-PC:~$ sudo ps auwx | grep weed
[sudo] password for qualebs:
qualebs 24540 0.0 0.1 798100 7008 pts/1 Sl+ 00:40 0:00 ./weed master -mdir=/var/lib/qualebs/weed -port=9333
qualebs 24583 0.0 0.1 798100 9124 pts/6 Sl+ 00:42 0:00 ./weed volume -dir=/var/lib/qualebs/weed -port=9444
qualebs 24749 0.0 0.0 22516 1020 pts/18 S+ 00:48 0:00 grep --color=auto weed

但是当从进程构建器运行时,我只有 master 监听端口 9333

最佳答案

不要使用:

    executor.execute(new WeedMaster());
executor.execute(new WeedVolume());

你应该使用 insteed:

    executor.submit(new WeedMaster());
executor.submit(new WeedVolume());

根据文档 executor.execute(Runnable) 可以根据实现的判断在当前(调用)线程上调用 runnable。引用:

void execute(Runnable command)

Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.

关于java - Executor 服务只执行第一个任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37286813/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com