1 package net.avcompris.commons3.notifier.impl; 2 3 import static com.google.common.base.Preconditions.checkNotNull; 4 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import javax.annotation.Nullable; 9 10 import org.apache.commons.logging.Log; 11 import org.springframework.stereotype.Component; 12 13 import net.avcompris.commons3.api.exception.ServiceException; 14 import net.avcompris.commons3.notifier.ErrorNotifier; 15 import net.avcompris.commons3.triggered.ErrorTriggered; 16 import net.avcompris.commons3.utils.LogFactory; 17 18 @Component 19 public final class ErrorNotifierInMemory implements ErrorNotifier { 20 21 private static final Log logger = LogFactory.getLog(ErrorNotifierInMemory.class); 22 23 private static ErrorNotifierInMemory INSTANCE; 24 25 private final List<ErrorTriggered> allTriggered = new ArrayList<>(); 26 27 private ErrorNotifierInMemory() { 28 29 } 30 31 public static ErrorNotifierInMemory getInstance() { 32 33 if (INSTANCE == null) { 34 35 loadInstance(); 36 } 37 38 return INSTANCE; 39 } 40 41 private static synchronized void loadInstance() { 42 43 if (INSTANCE != null) { 44 return; 45 } 46 47 INSTANCE = new ErrorNotifierInMemory(); 48 } 49 50 @Override 51 public void notifyError(@Nullable final String correlationId, @Nullable final String username, 52 final Throwable throwable) { 53 54 checkNotNull(throwable, "throwable"); 55 56 for (final ErrorTriggered triggered : allTriggered) { 57 58 new Thread() { 59 60 @Override 61 public void run() { 62 63 try { 64 65 ((ErrorTriggeredInMemory) triggered).trigger(correlationId, username, throwable); 66 67 } catch (final ServiceException | RuntimeException | Error e) { 68 69 logger.error("Triggered: " + triggered.getName(), e); 70 } 71 } 72 73 }.start(); 74 } 75 } 76 77 void register(final ErrorTriggered triggered) { 78 79 checkNotNull(triggered, "triggered"); 80 81 allTriggered.add(triggered); 82 } 83 }