Write to files asynchronously.

This commit is contained in:
Keir 2016-01-07 07:04:24 +00:00
parent abc15e3a4c
commit a63d98a2d7

View File

@ -7,6 +7,7 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.logging.Level; import java.util.logging.Level;
@ -30,8 +31,10 @@ public class RedisCommandHandler extends JedisPubSub
private static final JsonParser _jsonParser = new JsonParser(); private static final JsonParser _jsonParser = new JsonParser();
private File _directory; private final File _directory;
private Logger _logger; private final Logger _logger;
private final ExecutorService _executorService = Executors.newCachedThreadPool();
public RedisCommandHandler(File directory, Logger logger) public RedisCommandHandler(File directory, Logger logger)
{ {
@ -93,14 +96,8 @@ public class RedisCommandHandler extends JedisPubSub
} }
} }
try
{ writeFile(target, json);
Files.write(target.toPath(), Arrays.asList(json.split("\n")));
}
catch (IOException e)
{
e.printStackTrace();
}
} }
else if (channel.equals(ReportServer.CHANNEL_DESTROY)) else if (channel.equals(ReportServer.CHANNEL_DESTROY))
{ {
@ -120,6 +117,11 @@ public class RedisCommandHandler extends JedisPubSub
} }
} }
private void writeFile(File file, String json)
{
_executorService.submit(() -> Files.write(file.toPath(), Arrays.asList(json.split("\n"))));
}
@Override @Override
public void onPMessage(String s, String s1, String s2) public void onPMessage(String s, String s1, String s2)
{ {