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

Applying ServerBuilder correctly

parent a16f4887
import builder.Director; import director.Director;
import builder.ServerBuilder; import server.ServerBuilder;
import server.Server; import server.Server;
public class Main { public class Main {
......
package builder; package director;
import server.ServerBuilder;
public class Director { public class Director {
public void buildMaster(ServerBuilder builder) { public void buildMaster(ServerBuilder builder) {
......
...@@ -16,7 +16,6 @@ public class Server implements AutoCloseable { ...@@ -16,7 +16,6 @@ public class Server implements AutoCloseable {
private final String role; private final String role;
StorageManager manager = new StorageManager(); StorageManager manager = new StorageManager();
private static volatile Server instance;
Server(int port, String role) { Server(int port, String role) {
PORT = port; PORT = port;
...@@ -24,15 +23,6 @@ public class Server implements AutoCloseable { ...@@ -24,15 +23,6 @@ public class Server implements AutoCloseable {
this.executor = Executors.newVirtualThreadPerTaskExecutor(); 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() { public void start() {
try (ServerSocket serverSocket = new ServerSocket(PORT)) { try (ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("Server started on port " + PORT); System.out.println("Server started on port " + PORT);
......
package builder; package server;
import server.Server;
import java.io.IOException;
public class ServerBuilder { public class ServerBuilder {
private int port; private int port;
...@@ -17,10 +13,6 @@ public class ServerBuilder { ...@@ -17,10 +13,6 @@ public class ServerBuilder {
} }
public Server build() { public Server build() {
try { return new Server(port, role);
return Server.getInstance(port, role);
} catch (IOException e) {
throw new RuntimeException("Failed to build the Server");
}
} }
} }
package storage; package storage;
import java.util.Date;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -11,22 +12,17 @@ public class StorageManager { ...@@ -11,22 +12,17 @@ public class StorageManager {
public StorageManager() { public StorageManager() {
this.storage = Storage.getInstance(); this.storage = Storage.getInstance();
this.scheduler = Executors.newSingleThreadScheduledExecutor(); 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() { private void performMaintenance() {
storage.runCachePolicy(); 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() { public void shutdown() {
scheduler.shutdown(); scheduler.shutdown();
try { try {
if (!scheduler.awaitTermination(60, TimeUnit.SECONDS)) { if (!scheduler.awaitTermination(20, TimeUnit.SECONDS)) {
scheduler.shutdownNow(); scheduler.shutdownNow();
} }
} catch (InterruptedException e) { } 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