I am trying to write Observe pattern in Java in multithread program to develop own logging program. My program is used in any Java class and can create the Logger class, and write a message to the logger. Logger class will call the logger manager class , which is a Singleton class and it also have the private class which is thread, on execution a scheduler runs, So I kept that execution in the private constructor.
private void LoggerManager() {
System.out.println("IN CONSTRUCTOR");
executorThread.submit(new CreateLoggerFileForSpecifiedTime());
}
static LoggerManager getInstance() {
if (LOGMANAGER == null) {
synchronized (LoggerManager.class) {
if(LOGMANAGER == null){
System.out.println("IN MANAGER");
LOGMANAGER = new LoggerManager();
}
}
}
return LOGMANAGER;
}
Next there is a method in the LoogerManager “writeMessageToLog”.
void writeMessageToLog(String componantName, String message,
Calendar messageCreationTime) {
LoggerDetails logDetails = new LoggerDetails(componantName, message,
messageCreationTime);
LogInitiater logIntiater = new LogInitiater(logDetails, noticeOfLoggerChange,
noticeOfMessageAdded);
executorThread.submit(logIntiater);
}
LogIntitiater is a thread which is added the LogWriter to two Subject’s, One subject is a scheduler I have pasted above i.e a Timer runs for every specified time and notfiies LogWriter the need to create a new log file while the other purpose is when a message is added in the queue. Code is not working as expected:
* scheduler code is not running, I tried with execute as well. That private constructor is not being called
* thread initiater is not always called
* observe pattern: I have used J2SE interfaces i.e Observable and Observe and they are not working as expected
Please help me.
Producer – Consumer way for Logger (INCOMPLETE AND NOT FULLY TESTED) – will leave that to you
This is just for the demonstration purpose and shows one way of doing ASYNCHRONOUS logging
Main class