There is a spring-mvc web project. If the user does something important, the application must record log and send a message to the user's email in the service layer (btw, the user will do many important things). The code like below:
private static final Logger LOGGER = LoggerFactory.getLogger(MessageServiceImpl.class);
private static final ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
fixedThreadPool.execute(new Runnable() {
@Override
public void run() {
if (emailParams != null && EmailUtil.sendEmail(emailParams)) {
LOGGER.info("send email succeed");
} else {
LOGGER.warn("send email failed");
}
}
});
There is two question I have met:
Can I use static LOGGER object in fixedThreadPool?
If I use static logger object in fixedThreadPool, what will happen? Deadlock? Race condition? or nothing?
Should I define the fixedThreadPool static or not static?
In my opinion:
if fixedThreadPool is static and I don't invoke its shutdown(), so it will exist util web project is shutdown.
if fixedThreadPool is
not
static and every request may produce one fixedThreadPool. Finally, this web project will have many useless fixedThreadPool, may produce Out-Of-Memery.