I'm working in an application that saves process data (processName, processID, sessionStart, sessionEnd, computerName, currentUser) into the database every time a specific device is being in use by a process.
My problem is that the data process is being added over and over again (every 4 seconds as per specified in the runnable task), so I end up with many similar rows, I would like to find a solution for the process just being added once, and then one more time adding a sessionEnd datetime when the device stop being used. My code is as follows:
if(found){
while(found == true){
System.out.println("\nALERT! Device in use!\nThe process currently using the device is: \n" + strLineProcess+"\n");
final String regex = "^(\\S+) pid: (\\d+) ([^\\\\s]+)\\\\(.+)";
final String processDetails = strLineProcess;
String processName = null;
String processID = null;
String computerName = null;
String currentUser = null;
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(processDetails);
if (matcher.find()) {
processName = matcher.group(1);
processID = matcher.group(2);
computerName = matcher.group(3);
currentUser = matcher.group(4);
}
//Array containing all the process data
String[] processData = new String[6];
processData[0]=""+processName;
processData[1]=""+processID;
processData[2]=getDateTime();
processData[3]="";
processData[4]=""+computerName;
processData[5]=""+currentUser;
String[] processRepeated = new String[1];
insert.insertRow(processData);
break;
}
}
I though about do a select from the application, check if the processID is the same than the one I'm trying to insert, and if so, don't do anything, but I found this a bit clumsy as all this needs to happen every 4 seconds. Just can't get my head around it!
Just to clarify, strLineProcess contain a string similar to this: Skype.exe pid: 3068 WATCHOUT\tofetopo , which I divide with regex to add each value to its corresponding variable.