I have two process ProcessA
and ProcessB
. I want to run these two process independent of each other. There is no relation between them at all.
- Each process should have a different Properties object.
- Each process should have a different thread pool configuration since they can be run in multithreaded way if needed independent of other process.
Below is my design:
Process class (abstract):
public abstract class Process implements Runnable {
private final Properties props;
private final String processName;
public Process(String processName, Properties props) {
this.processName = processName;
this.props = props;
}
public abstract void shutdown();
}
ProcessA class:
public class ProcessA implements Process {
public ProcessA(String processName, Properties props) {
super(processName, props);
}
@Override
public void run() {
// add run code here
// all the logic of executing my process comes here
// here it uses "processName" and "props" object to do certain things
}
@Override
public void shutdown() {
// shut down code
}
}
Process B class:
// similar to `ProcessA` but with specific details of B
ProcessHandler class (abstract):
public abstract class ProcessHandler {
private final ExecutorService executorServiceProcess;
private final List<Process> processList;
private final int poolSize;
protected ProcessHandler(int poolSize) {
this.executorServiceProcess = Executors.newFixedThreadPool(poolSize);
this.processList = new ArrayList<>();
this.poolSize = poolSize;
}
public void postInit(Process process) {
for (int i = 0; i < poolSize; i++) {
processList.add(process);
executorServiceProcess.submit(process);
}
}
public void shutdown() {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
for (Process process : processList) {
process.shutdown();
}
executorServiceProcess.shutdown();
try {
executorServiceProcess.awaitTermination(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
});
}
}
ProcessHandlerA class:
public class ProcessHandlerA extends ProcessHandler {
public ProcessHandlerA() {
super(3);// configure pool size properly w.r.to ProcessA
}
public void postInit() {
ProcessA processA = new ProcessA("processA", properties);
super(processA);
}
public void shutdown() {
super.shutdown();
}
}
ProcessHandlerB class:
// similar to ProcessHandlerA but with specific details for B
Below is my main class in one of my project where I execute my ProcessA
and ProcessB
.
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Singleton;
@Singleton
@DependencyInjectionInitializer
public class Initializer {
@Inject
private ProcessHandlerA processHandlerA;
@Inject
private ProcessHandlerB processHandlerB;
@PostConstruct
public void init() {
processHandlerA.postInit();
ProcessHandlerB.postInit();
}
@PreDestroy
public void shutdown() {
processHandlerB.shutdown();
processHandlerB.shutdown();
}
}
And in some other project my main class will be:
public class Initializer {
public static void main(String[] args) {
// call both processes from here
}
}
Is this the right design for this kind of problem? Let me know if there is any better and efficient way to solve this problem.
ProcessHandler
I have shutdown method which will shutdown all my processes.