Commit 988c6bf3 authored by amir.yosef's avatar amir.yosef

Applying ServerBuilder correctly

parent a16f4887
import builder.Director;
import builder.ServerBuilder;
import director.Director;
import server.ServerBuilder;
import server.Server;
public class Main {
......
package builder;
package director;
import server.ServerBuilder;
public class Director {
public void buildMaster(ServerBuilder builder) {
......
......@@ -16,7 +16,6 @@ public class Server implements AutoCloseable {
private final String role;
StorageManager manager = new StorageManager();
private static volatile Server instance;
Server(int port, String role) {
PORT = port;
......@@ -24,15 +23,6 @@ public class Server implements AutoCloseable {
this.executor = Executors.newVirtualThreadPerTaskExecutor();
}
public static Server getInstance(int PORT, String role) throws IOException {
if (role.equalsIgnoreCase("master")) {
if (instance == null) {
instance = new Server(PORT, role);
}
}
return instance;
}
public void start() {
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("Server started on port " + PORT);
......
package builder;
import server.Server;
import java.io.IOException;
package server;
public class ServerBuilder {
private int port;
......@@ -17,10 +13,6 @@ public class ServerBuilder {
}
public Server build() {
try {
return Server.getInstance(port, role);
} catch (IOException e) {
throw new RuntimeException("Failed to build the Server");
}
return new Server(port, role);
}
}
package storage;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
......@@ -11,22 +12,17 @@ public class StorageManager {
public StorageManager() {
this.storage = Storage.getInstance();
this.scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(this::performMaintenance, 5, 5, TimeUnit.MINUTES);
scheduler.scheduleAtFixedRate(this::performMaintenance, 2, 2, TimeUnit.MINUTES);
}
public Storage getStorage() {
return storage;
}
private void performMaintenance() {
storage.runCachePolicy();
System.out.println("Maintenance performed at: " + System.currentTimeMillis());
System.out.println("Maintenance performed at: " + Date.from(java.time.Clock.systemUTC().instant()));
}
public void shutdown() {
scheduler.shutdown();
try {
if (!scheduler.awaitTermination(60, TimeUnit.SECONDS)) {
if (!scheduler.awaitTermination(20, TimeUnit.SECONDS)) {
scheduler.shutdownNow();
}
} catch (InterruptedException e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment