HistoryStore { public synchronized void configure(HistoryKey pKey, HistoryLimit pHistoryLimit) { if (pHistoryLimit == null) { removeEntries(pKey); return; } HistoryLimit limit = pHistoryLimit.respectGlobalMaxEntries(globalMaxEntries); if (pKey.isMBeanPattern()) { patterns.put(pKey,limit); for (HistoryKey key : historyStore.keySet()) { if (pKey.matches(key)) { HistoryEntry entry = historyStore.get(key); entry.setLimit(limit); } } } else { HistoryEntry entry = historyStore.get(pKey); if (entry != null) { entry.setLimit(limit); } else { entry = new HistoryEntry(limit); historyStore.put(pKey,entry); } } } HistoryStore(int pTotalMaxEntries); synchronized int getGlobalMaxEntries(); synchronized void setGlobalMaxEntries(int pGlobalMaxEntries); synchronized void configure(HistoryKey pKey, HistoryLimit pHistoryLimit); synchronized void reset(); synchronized void updateAndAdd(JmxRequest pJmxReq, JSONObject pJson); synchronized int getSize();  }
@Test public void configure() throws MalformedObjectNameException { JmxExecRequest req = new JmxRequestBuilder(EXEC,"test:type=exec") .operation("op") .build(); store.configure(new HistoryKey(req), new HistoryLimit(2, 0L)); assertEquals("2 history entries",2,updateNTimesAsList(req,3).size()); store.configure(new HistoryKey(req), new HistoryLimit(4, 0L)); assertEquals("4 history entries",4,updateNTimesAsList(req,5).size()); store.configure(new HistoryKey(req), new HistoryLimit(12, 0L)); assertEquals("10 history entries (max. for store)",10,updateNTimesAsList(req,10).size()); store.setGlobalMaxEntries(20); assertEquals("Read max entries",20,store.getGlobalMaxEntries()); assertEquals("20 history entries (max. for store)",20,updateNTimesAsList(req,30).size()); store.reset(); store.configure(new HistoryKey(req), new HistoryLimit(20, 0L)); assertEquals("4 history entries after reset",4,updateNTimesAsList(req,5).size()); store.configure(new HistoryKey(req), null); assertEquals("History disabled",null,updateNTimesAsList(req,12)); }
@Test public void reconfigure() throws MalformedObjectNameException { JmxExecRequest req = new JmxRequestBuilder(EXEC,"test:type=exec") .operation("op") .build(); store.configure(new HistoryKey(req), new HistoryLimit(2, 100L)); store.configure(new HistoryKey(req), new HistoryLimit(2, 100L)); }
@Test public void durationBasedEvicting() throws MalformedObjectNameException { JmxReadRequest req = new JmxRequestBuilder(READ,"test:type=read") .attribute("attr") .build(); HistoryKey key = new HistoryKey(req); store.configure(key,new HistoryLimit(0,1)); JSONArray hist = updateNTimesAsListWithSleep(req, 3, 2000); assertEquals("1 History Entry", 1, hist.size()); }
@Test public void singleAttributeRead() throws Exception { JmxReadRequest req = new JmxRequestBuilder(READ,"test:type=read") .attribute("attr") .build(); store.configure(new HistoryKey(req), new HistoryLimit(3, 0L)); assertEquals("2 history entries",2,updateNTimesAsList(req,3,"42").size()); }
@Test public void singleAttributeWrite() throws Exception { JmxWriteRequest req = new JmxRequestBuilder(WRITE,"test:type=write") .attribute("attr") .value("val1") .build(); store.configure(new HistoryKey(req), new HistoryLimit(5, 0L)); assertEquals("4 history entries",3,updateNTimesAsList(req,4).size()); }
@Test public void singleAttributeAsListRead() throws Exception { JmxReadRequest req = new JmxRequestBuilder(READ,"test:type=read") .attributes("attr") .build(); store.configure(new HistoryKey(req), new HistoryLimit(5, 0L)); Map value = new HashMap(); value.put("attr","42"); JSONObject res = updateNTimesAsMap(req,4,value); assertEquals("4 history entries",3,((List) res.get("attr")).size()); }
@Test public void noAttributesRead() throws Exception { String mbean = "test:type=read"; JmxReadRequest req = new JmxRequestBuilder(READ,mbean) .build(); store.configure(new HistoryKey(mbean,"attr1",null,null), new HistoryLimit(4, 0L)); store.configure(new HistoryKey(mbean,"attr2",null,null), new HistoryLimit(5, 0L)); Map value = new HashMap(); value.put("attr1","val1"); value.put("attr2","val2"); JSONObject history = updateNTimesAsMap(req,5,value); assertEquals("Attr1 has 3 entries",4,((List) history.get("attr1")).size()); assertEquals("Attr2 has 4 entries",4,((List) history.get("attr2")).size()); }
@Test public void multipleAttributeRead() throws Exception { String mbean = "test:type=read"; JmxReadRequest req = new JmxRequestBuilder(READ,mbean) .attributes("attr1","attr2") .build(); store.configure(new HistoryKey(mbean,"attr1",null,null), new HistoryLimit(3, 0L)); store.configure(new HistoryKey(mbean,"attr2",null,null), new HistoryLimit(5, 0L)); Map value = new HashMap(); value.put("attr1","val1"); value.put("attr2","val2"); JSONObject history = updateNTimesAsMap(req,5,value); assertEquals("Attr1 has 3 entries",3,((List) history.get("attr1")).size()); assertEquals("Attr2 has 4 entries",4,((List) history.get("attr2")).size()); }
@Test public void patternConfigure() throws MalformedObjectNameException { store.configure(new HistoryKey("java.lang:type=Memory","HeapMemoryUsage",null,null), new HistoryLimit(10, 0L)); store.configure(new HistoryKey("java.lang:*", "HeapMemoryUsage", null, null), new HistoryLimit(3, 0L)); JmxReadRequest req = new JmxRequestBuilder(READ,"java.lang:type=Memory") .attribute("HeapMemoryUsage") .build(); JSONArray history = updateNTimesAsList(req, 5, 4711); assertEquals(history.size(), 3); }
@Test public void patternRemoveEntries() throws MalformedObjectNameException { store.configure(new HistoryKey("java.lang:*", "HeapMemoryUsage", null, null), new HistoryLimit(3, 0L)); store.configure(new HistoryKey("java.lang:type=Memory","HeapMemoryUsage",null,null), new HistoryLimit(10, 0L)); store.configure(new HistoryKey("java.lang:*", "HeapMemoryUsage", null, null), null); JmxReadRequest req = new JmxRequestBuilder(READ,"java.lang:type=Memory") .attribute("HeapMemoryUsage") .build(); JSONArray history = updateNTimesAsList(req, 5, 4711); assertNull(history); }
@Test public void patternGetEntries() throws MalformedObjectNameException { store.configure(new HistoryKey("java.lang:*", "HeapMemoryUsage", null, null), new HistoryLimit(3, 0L)); JmxReadRequest req = new JmxRequestBuilder(READ,"java.lang:type=Memory") .attribute("HeapMemoryUsage") .build(); JSONArray history = updateNTimesAsList(req, 5, 4711); assertEquals(history.size(), 3); }
@Test(groups = "java6") public void patternAttributeRead() throws Exception { JmxReadRequest req = new JmxRequestBuilder(READ,"test:type=*") .build(); store.configure(new HistoryKey("test:type=read","attr1",null,null), new HistoryLimit(3, 0L)); store.configure(new HistoryKey("test:type=write","attr2",null,null), new HistoryLimit(5, 0L)); Map mBeanMap = new HashMap(); Map attr1Map = new HashMap(); mBeanMap.put("test:type=read",attr1Map); attr1Map.put("attr1","val1"); Map attr2Map = new HashMap(); mBeanMap.put("test:type=write",attr2Map); attr2Map.put("attr2","val2"); JSONObject history = updateNTimesAsMap(req,4,mBeanMap); assertEquals("History has 2 entries",2,history.size()); assertEquals("bean1 has 1 entry",1,((Map) history.get("test:type=read")).size()); assertEquals("bean1 has 1 entry",1,((Map) history.get("test:type=write")).size()); assertEquals("attr1 has 3 history entries",3,((List) ((Map) history.get("test:type=read")).get("attr1")).size()); assertEquals("attr2 has 3 history entries",3,((List) ((Map) history.get("test:type=write")).get("attr2")).size()); }