@SuppressWarnings("unchecked") public static <K extends AbstractPolicyDefinitionEntity> DynamicPolicyLoader<K> getInstanceOf(Class<K> clz) { if (maps.containsKey(clz)) { return maps.get(clz); } else { DynamicPolicyLoader<K> loader = new DynamicPolicyLoader<K>(); maps.putIfAbsent(clz, loader); return maps.get(clz); } }
/** * singleton with init would be good for unit test as well, and it ensures that * initialization happens only once before you use it. * @param config * @param dao */ public void init(Map<String, Map<String, T>> initialAlertDefs, PolicyDefinitionDAO<T> dao, Config config){ if(!initialized){ synchronized(this){ if(!initialized){ internalInit(initialAlertDefs, dao, config); initialized = true; } } } }
@Override public void init() { String site = config.getString("eagleProps.site"); String application = config.getString("eagleProps.application"); Map<String, Map<String, AlertDefinitionAPIEntity>> initialAlertDefs; try { initialAlertDefs = dao.findActivePoliciesGroupbyExecutorId( site, application ); } catch (Exception ex) { LOG.error("fail to initialize initialAlertDefs: ", ex); throw new IllegalStateException("fail to initialize initialAlertDefs: ", ex); } if(initialAlertDefs == null || initialAlertDefs.isEmpty()){ LOG.warn("No alert definitions found for site: "+site+", application: "+ application); } try{ notificationManager = new NotificationPluginManagerImpl(config); }catch (Exception ex ){ LOG.error("Fail to initialize NotificationManager: ", ex); throw new IllegalStateException("Fail to initialize NotificationManager: ", ex); } DynamicPolicyLoader<AlertDefinitionAPIEntity> policyLoader = DynamicPolicyLoader.getInstanceOf(AlertDefinitionAPIEntity.class); policyLoader.init(initialAlertDefs, dao, config); for (String alertExecutorId : alertExecutorIdList) { policyLoader.addPolicyChangeListener(alertExecutorId, this); } }
DynamicPolicyLoader<T> policyLoader = DynamicPolicyLoader.getInstanceOf(policyDefinitionClz); policyLoader.init(initialAlertDefs, policyDefinitionDao, config); String fullQualifiedAlertExecutorId = executorId + "_" + partitionSeq; policyLoader.addPolicyChangeListener(fullQualifiedAlertExecutorId, this); policyLoader.addPolicyDistributionReporter(fullQualifiedAlertExecutorId, this); LOG.info("Alert Executor created, partitionSeq: " + partitionSeq + " , numPartitions: " + numPartitions); LOG.info("All policy evaluators: " + policyEvaluators);
DynamicPolicyLoader<AlertDefinitionAPIEntity> policyLoader = DynamicPolicyLoader.getInstanceOf(AlertDefinitionAPIEntity.class); policyLoader.init(initialAlertDefs, dao, config); for (String alertExecutorId : alertExecutorIdList) { policyLoader.addPolicyChangeListener(alertExecutorId, this);