1

谁能解释一下 和 之间的区别ProcessBuilderFileHandler在什么情况下应该使用哪个。

例如,如果我们想将命令的输出重定向到一个文本文件,比如“ logfile.txt ”,ProcessBuilder需要几秒钟才能将输出输出到该文件。

FileHandler在 100 个进程必须将其输出发送到相同的“ logfile.txt ”的情况下,这可以做什么?

有没有办法在不使用方法的情况下将所有一百条记录输出到相同的“ logfile.txt ” ?这是我的代码,但是如果同时安排了 100 条记录,那么 p.waitfor() 方法将无济于事,因为执行命令并将每个进程的输出输出到日志文件需要几秒钟,而对于 100 个进程,这意味着需要更多是时候对数据库中的所有记录执行相同的命令了。但我的应用程序将每分钟启动一次。这就是问题所在, process.waitFor() 帮助我获得准确的输出,但更多的记录意味着需要更多的时间。我想要另一种方法,它不应该等待,但所有输出都应该同步附加到文件中。process.waitFor()ProcessBuilder

while(rs1.next())
    {
        instance_id = rs1.getString(1);
        startdate = rs1.getString(2);
        starttime = rs1.getString(3);
        endtime = rs1.getString(4);
        enddate = rs1.getString(5);
        if(presentdate.equals(startdate) || presentdate.equals(enddate))
        {
            if(presenttime.equals(starttime))
            {
                String[] s1 = new String[]{"cmd", "/c","ec2-start-instances",instance_id,">>","D:\\logfile.log"};
                ProcessBuilder builder1 = new ProcessBuilder(s1);
                Process p1 = builder1.start();
                p1.waitFor();
            }
            else if(presenttime.equals(endtime))
            {
                String[] s1 = new String[]{"cmd", "/c","ec2-stop-instances",instance_id,">>","D:\\logfile.log"};
                ProcessBuilder builder1 = new ProcessBuilder(s1);
                Process p1 = builder1.start();
                p1.waitFor();
            }
        }
    }
4

3 回答 3

0

They are two mostly unrelated objects and areas.

Yes a Process Builder gives us a standard output and error output stream which can be saved to a file, but it could also be saved to a data base or discarded.

ava.util.logging.FileHandler on the other hand is an appendar part of the logging system. We generally use logging for our own code, though nothing wrong with logging the output of external processes we have called in there too.

But if other classes log while process is logging then will get mixed up logs.

Similarly if your running 50 processes I suggest you dont log to the same file. But log to 50 different files - using 50 threads. Each thread would check the eroror and output streams and save when you have a new line or end of that stream.

When all are done can copy to log file or to a new file as preferred.T

于 2014-06-17T13:42:11.100 回答
0

发布您的代码实际上可以让您更多地了解正在发生的事情。现在我看到您正在使用 Windowscmd.exe重定向功能,而不是使用 java 从生成的进程中读取输出并对其进行处理。如果你喜欢任何不同的行为,你必须:

  1. 如果可能,在没有 cmd.exe 的情况下执行进程(以避免从中缓冲) -new String[]{"ec2-start-instances",instance_id)
  2. 结合进程标准输出和标准错误(更容易捕获所有输出)processBuilder.redirectErrorStream(true)
  3. 关闭进程标准输入(只是预防措施) -process.getOutputStream().close()
  4. 生成线程以读取所有并发进程的输出流
  5. 让这些线程以线程安全的方式将它们正在读取的内容写入文件

现在#4 和#5 不是微不足道的,但互联网上有很多关于如何实现的信息。在 Java 7 中,您还可以选择在不使用任何代码的情况下重定向到文件 -processBuilder.redirectOutput(File file)但我认为这不是线程安全的,因此您的多个进程不会相互配合。

这里有两个问题给出了一些如何实现#4和#5的例子:

于 2014-06-24T10:54:57.510 回答
0

ProcessBuilder根据文档:

请注意,此类不同步。如果多个线程同时访问一个 ProcessBuilder 实例,并且至少有一个线程在结构上修改了其中一个属性,则必须在外部进行同步。

例子

进程 p = new ProcessBuilder("myCommand", "myArg").start();

据我了解,ProcessBuilder用于与操作系统上的其他软件进行通信。(可能是外部日志记录脚本或某种形式的日志记录软件)


FileHandler是一个更标准的文件记录系统。但是,为了避免多次调用的问题,请将您的日志记录代码设置为静态,以确保在调用它时完成它的进程。FileHandler默认刷新(根据文档)。

希望能有所帮助

于 2014-06-17T13:20:35.813 回答