*/
package org.sonar.process;
+import org.apache.commons.io.IOUtils;
import org.slf4j.LoggerFactory;
import java.io.File;
* </ul>
*/
final MappedByteBuffer mappedByteBuffer;
+ private final RandomAccessFile sharedMemory;
private static final int MAX_PROCESSES = 50;
private static final int BYTE_LENGTH_FOR_ONE_PROCESS = 1 + 1 + 8;
private static final int MAX_SHARED_MEMORY = BYTE_LENGTH_FOR_ONE_PROCESS * MAX_PROCESSES; // With this shared memory we can handle up to MAX_PROCESSES processes
}
try {
- RandomAccessFile sharedMemory = new RandomAccessFile(new File(directory, "sharedmemory"), "rw");
+ sharedMemory = new RandomAccessFile(new File(directory, "sharedmemory"), "rw");
mappedByteBuffer = sharedMemory.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, MAX_SHARED_MEMORY);
} catch (IOException e) {
throw new IllegalArgumentException("Unable to create shared memory : ", e);
return mappedByteBuffer.get(offset() + 1) == STOP;
}
+ public void endWatch() {
+ IOUtils.closeQuietly(sharedMemory);
+ }
+
int offset() {
return BYTE_LENGTH_FOR_ONE_PROCESS * processNumber;
}
@Override
public void run() {
- while (watching) {
- if (commands.askedForStop()) {
- LoggerFactory.getLogger(getClass()).info("Stopping process");
- stoppable.stopAsync();
- watching = false;
- } else {
- try {
- Thread.sleep(delayMs);
- } catch (InterruptedException ignored) {
+ try {
+ while (watching) {
+ if (commands.askedForStop()) {
+ LoggerFactory.getLogger(getClass()).info("Stopping process");
+ stoppable.stopAsync();
watching = false;
+ } else {
+ try {
+ Thread.sleep(delayMs);
+ } catch (InterruptedException ignored) {
+ watching = false;
+ }
}
}
+ } finally {
+ commands.endWatch();
}
}