Java 如何使用不同的线程读取每个日志文件
基本上,我连接到远程服务器并从日志文件夹获取所有日志文件名。我将这些文件名添加到列表中,现在我想读取或跟踪该文件上的-f,我应该查找是否有任何文件遇到“错误”,然后我应该能够写入另一个文件。 我想使用多线程,我想创建线程来分别读取每个日志文件 例如:Java 如何使用不同的线程读取每个日志文件,java,multithreading,executorservice,remote-server,threadpoolexecutor,Java,Multithreading,Executorservice,Remote Server,Threadpoolexecutor,基本上,我连接到远程服务器并从日志文件夹获取所有日志文件名。我将这些文件名添加到列表中,现在我想读取或跟踪该文件上的-f,我应该查找是否有任何文件遇到“错误”,然后我应该能够写入另一个文件。 我想使用多线程,我想创建线程来分别读取每个日志文件 例如: public List<String> excuteScript(String fileName) { List<String> result = new ArrayList<String>();
public List<String> excuteScript(String fileName) {
List<String> result = new ArrayList<String>();
try {
JSch jSch = new JSch();
Session session = jSch.getSession(USERNAME, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(PASSWORD);
session.connect();
//create the excution channel over the session
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
// Gets an InputStream for this channel. All data arriving in as messages from the remote side can be read from this stream.
InputStream in = channelExec.getInputStream();
// Set the command that you want to execute
// In our case its the remote shell script
channelExec.setCommand("sh " + fileName);
// Execute the command
channelExec.connect();
System.out.println("connected");
// Read the output from the input stream we set above
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
//Read each line from the buffered reader and add it to result list
// You can also simple print the result here
while ((line = reader.readLine()) != null) {
if(line.contains("Error") || line.contains("Exception")) {
System.out.println(line);
result.add(line);
}
}
//retrieve the exit status of the remote command corresponding to this channel
int exitStatus = channelExec.getExitStatus();
//Safely disconnect channel and disconnect session. If not done then it may cause resource leak
channelExec.disconnect();
session.disconnect();
if(exitStatus < 0) {
// System.out.println("Done, but exit status not set!");
}
else if(exitStatus > 0) {
// System.out.println("Done, but with error!");
}
else {
// System.out.println("Done!");
}
}
catch(Exception e) {
System.err.println("Error: " + e);
}
return result;
}
公共列表excuteScript(字符串文件名){
列表结果=新建ArrayList();
试一试{
JSch JSch=新的JSch();
Session Session=jSch.getSession(用户名、主机、端口);
session.setConfig(“StrictHostKeyChecking”、“no”);
session.setPassword(密码);
session.connect();
//在会话上创建执行通道
ChannelExec ChannelExec=(ChannelExec)session.openChannel(“exec”);
//获取此通道的InputStream。作为消息从远程端到达的所有数据都可以从此流读取。
InputStream in=channelExec.getInputStream();
//设置要执行的命令
//在我们的例子中,它是远程shell脚本
channelExec.setCommand(“sh”+文件名);
//执行命令
channelExec.connect();
System.out.println(“已连接”);
//从上面设置的输入流读取输出
BufferedReader reader=新的BufferedReader(新的InputStreamReader(in));
弦线;
//从缓冲读取器读取每一行,并将其添加到结果列表中
//您也可以在此处简单打印结果
而((line=reader.readLine())!=null){
if(line.contains(“Error”)| | line.contains(“Exception”)){
系统输出打印项次(行);
结果。添加(行);
}
}
//检索与此通道对应的远程命令的退出状态
int exitStatus=channelExec.getExitStatus();
//安全地断开通道并断开会话。如果不这样做,则可能导致资源泄漏
channelExec.disconnect();
session.disconnect();
if(exitStatus<0){
//System.out.println(“完成,但未设置退出状态!”);
}
否则如果(exitStatus>0){
//System.out.println(“完成,但有错误!”);
}
否则{
//System.out.println(“完成!”);
}
}
捕获(例外e){
System.err.println(“错误:+e”);
}
返回结果;
}
如果你说或[tag]你正在使用的编程语言会有帮助。如果你说或[tag]你正在使用com.jcraft.jsch.jsch
,会有帮助-我不明白。你想处理的多个文件在哪里?如果你说或[tag]你正在使用的编程语言会有帮助。如果你说或[tag]你正在使用com.jcraft.jsch.jsch
,会有帮助-我不明白。您要处理的多个文件在哪里?