我编写了一个程序来执行两个在shell前台运行的命令,直到在终端上按下^ c。
shell命令
./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.
然后我用命令和输出启动卷服务器,分别如下
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);
}
}
}
}
然后我将我的应用程序部署到服务器,只是发现第一个命令已执行且主服务器正在运行但没有卷服务器。为什么不执行第二项任务?
当从终端执行两个命令时,我可以看到主服务器和卷都在运行
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
但是当从流程构建器运行时,我只有主端监听端口9333
答案 0 :(得分:1)
不要使用:
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)
将来某个时间执行给定的命令。命令可以 在新线程,池化线程或调用线程中执行, 由执行人执行决定。