use of jp.ossc.nimbus.service.writer.Category in project nimbus by nimbus-org.
the class WsServiceMetricsHandlerService method consume.
/**
* 出力先が設定されていれば、。<p>
*
* @param dequeued null
* @param ctrl DaemonControlオブジェクト
*/
public void consume(Object dequeued, DaemonControl ctrl) {
Date timestamp = new Date();
if (keyAndCategoryMap != null && keyAndCategoryMap.size() != 0) {
final Iterator keys = keyAndCategoryMap.keySet().iterator();
while (keys.hasNext()) {
final String key = (String) keys.next();
final Category category = (Category) keyAndCategoryMap.get(key);
final MetricsInfo info = (MetricsInfo) metricsInfos.get(key);
if (info != null && category != null) {
try {
category.write(createRecord(timestamp, info));
} catch (MessageWriteException e) {
// TODO ログ出力
}
}
}
}
if (metricsCategory != null) {
final MetricsInfo[] infos = (MetricsInfo[]) metricsInfos.values().toArray(new MetricsInfo[metricsInfos.size()]);
Arrays.sort(infos, COMP);
for (int i = 0; i < infos.length; i++) {
try {
metricsCategory.write(createRecord(timestamp, i + 1, infos[i]));
} catch (MessageWriteException e) {
// TODO ログ出力
}
}
}
if (isResetByOutput) {
final MetricsInfo[] infos = (MetricsInfo[]) metricsInfos.values().toArray(new MetricsInfo[metricsInfos.size()]);
for (int i = 0; i < infos.length; i++) {
infos[i].reset();
}
}
}
use of jp.ossc.nimbus.service.writer.Category in project nimbus by nimbus-org.
the class WsServiceMetricsHandlerService method startService.
/**
* サービスの開始処理を行う。<p>
*
* @exception Exception 開始処理に失敗した場合
*/
public void startService() throws Exception {
metricsInfos.clear();
if (keyAndCategoryServiceNameMapping != null && keyAndCategoryServiceNameMapping.size() != 0) {
final ServiceNameEditor nameEditor = new ServiceNameEditor();
nameEditor.setServiceManagerName(getServiceManagerName());
final Iterator keys = keyAndCategoryServiceNameMapping.keySet().iterator();
while (keys.hasNext()) {
final String key = (String) keys.next();
final String nameStr = keyAndCategoryServiceNameMapping.getProperty(key);
nameEditor.setAsText(nameStr);
final ServiceName name = (ServiceName) nameEditor.getValue();
final Category category = (Category) ServiceManagerFactory.getServiceObject(name);
keyAndCategoryMap.put(key, category);
}
}
if (categoryServiceName != null) {
metricsCategory = (Category) ServiceManagerFactory.getServiceObject(categoryServiceName);
}
if ((keyAndCategoryMap != null && keyAndCategoryMap.size() != 0) || metricsCategory != null) {
writerDaemon = new Daemon(this);
writerDaemon.setName("Nimbus MetricsWriteDaemon " + getServiceNameObject());
writerDaemon.start();
}
}
use of jp.ossc.nimbus.service.writer.Category in project nimbus by nimbus-org.
the class MethodMetricsInterceptorServiceTest method test6.
public void test6() throws Throwable {
ServiceManagerFactory.registerManager("Test");
MethodMetricsInterceptorService interceptor = new MethodMetricsInterceptorService();
ServiceManagerFactory.registerService("Test", "MethodMetricsInterceptor", interceptor);
class TestCategory implements Category {
public Object elements;
public boolean isEnabled() {
return true;
}
public void setEnabled(boolean enable) {
}
public void write(Object elements) {
this.elements = elements;
synchronized (this) {
this.notify();
}
}
}
;
TestCategory category1 = new TestCategory();
ServiceManagerFactory.registerService("Test", "Category1", category1);
TestCategory category2 = new TestCategory();
ServiceManagerFactory.registerService("Test", "Category2", category2);
try {
ServiceManagerFactory.findManager("Test").createAllService();
Properties mapping = new Properties();
mapping.setProperty("java.util.HashMap#get(java.lang.Object)", "Test#Category1");
mapping.setProperty("java.util.HashMap#put(java.lang.Object,java.lang.Object)", "Test#Category2");
interceptor.setMethodAndCategoryServiceNameMapping(mapping);
interceptor.setDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
interceptor.setOutputInterval(1000L);
interceptor.setOutputCount(true);
interceptor.setOutputExceptionCount(true);
interceptor.setOutputErrorCount(true);
interceptor.setOutputLastTime(true);
interceptor.setOutputLastExceptionTime(true);
interceptor.setOutputLastErrorTime(true);
interceptor.setOutputBestPerformance(true);
interceptor.setOutputBestPerformanceTime(true);
interceptor.setOutputWorstPerformance(true);
interceptor.setOutputWorstPerformanceTime(true);
interceptor.setOutputAveragePerformance(true);
ServiceManagerFactory.findManager("Test").startAllService();
assertEquals("test", new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {
public Object invoke(InvocationContext context) throws Throwable {
Thread.sleep(new Random().nextInt(400) + 100);
return "test";
}
}).invokeNext(new DefaultMethodInvocationContext(null, HashMap.class.getMethod("get", new Class[] { Object.class }), null)));
try {
new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {
public Object invoke(InvocationContext context) throws Throwable {
Thread.sleep(new Random().nextInt(400) + 100);
throw new IllegalArgumentException();
}
}).invokeNext(new DefaultMethodInvocationContext(null, HashMap.class.getMethod("put", new Class[] { Object.class, Object.class }), null));
fail();
} catch (IllegalArgumentException e) {
}
synchronized (category1) {
if (category1.elements == null) {
category1.wait(2000);
}
}
assertNotNull(category1.elements);
assertTrue(category1.elements instanceof Map);
Map record = (Map) category1.elements;
assertEquals(12, record.size());
assertEquals("java.util.HashMap#get(java.lang.Object)", record.get(MethodMetricsInterceptorService.RECORD_KEY_METHOD));
assertEquals(new Long(1), record.get(MethodMetricsInterceptorService.RECORD_KEY_COUNT));
assertEquals(new Long(0), record.get(MethodMetricsInterceptorService.RECORD_KEY_EXCEPTION_COUNT));
assertEquals(new Long(0), record.get(MethodMetricsInterceptorService.RECORD_KEY_ERROR_COUNT));
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_TIME));
assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_TIME)) > 0);
assertNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_EXCEPTION_TIME));
assertNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_ERROR_TIME));
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE));
assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE)) < 0);
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE_TIME));
assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE_TIME)) > 0);
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE));
assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE)) < 0);
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE_TIME));
assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE_TIME)) > 0);
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_AVERAGE_PERFORMANCE));
assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_AVERAGE_PERFORMANCE)) < 0);
synchronized (category2) {
if (category2.elements == null) {
category2.wait(2000);
}
}
assertNotNull(category2.elements);
assertTrue(category2.elements instanceof Map);
record = (Map) category2.elements;
assertEquals(12, record.size());
assertEquals("java.util.HashMap#put(java.lang.Object,java.lang.Object)", record.get(MethodMetricsInterceptorService.RECORD_KEY_METHOD));
assertEquals(new Long(0), record.get(MethodMetricsInterceptorService.RECORD_KEY_COUNT));
assertEquals(new Long(1), record.get(MethodMetricsInterceptorService.RECORD_KEY_EXCEPTION_COUNT));
assertEquals(new Long(0), record.get(MethodMetricsInterceptorService.RECORD_KEY_ERROR_COUNT));
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_TIME));
assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_TIME)) > 0);
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_EXCEPTION_TIME));
assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_EXCEPTION_TIME)) > 0);
assertNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_ERROR_TIME));
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE));
assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE)) < 0);
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE_TIME));
assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE_TIME)) > 0);
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE));
assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE)) < 0);
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE_TIME));
assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE_TIME)) > 0);
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_AVERAGE_PERFORMANCE));
assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_AVERAGE_PERFORMANCE)) < 0);
} finally {
ServiceManagerFactory.findManager("Test").stopAllService();
ServiceManagerFactory.findManager("Test").destroyAllService();
ServiceManagerFactory.unregisterManager("Test");
}
}
use of jp.ossc.nimbus.service.writer.Category in project nimbus by nimbus-org.
the class MethodMetricsInterceptorServiceTest method test5.
public void test5() throws Throwable {
ServiceManagerFactory.registerManager("Test");
MethodMetricsInterceptorService interceptor = new MethodMetricsInterceptorService();
ServiceManagerFactory.registerService("Test", "MethodMetricsInterceptor", interceptor);
class TestCategory implements Category {
public Object elements;
public boolean isEnabled() {
return true;
}
public void setEnabled(boolean enable) {
}
public void write(Object elements) {
this.elements = elements;
synchronized (this) {
this.notify();
}
}
}
;
TestCategory category = new TestCategory();
ServiceManagerFactory.registerService("Test", "Category", category);
try {
ServiceManagerFactory.findManager("Test").createAllService();
interceptor.setCategoryServiceName(new ServiceName("Test", "Category"));
interceptor.setOutputInterval(1000L);
interceptor.setOutputCount(true);
interceptor.setOutputExceptionCount(true);
interceptor.setOutputErrorCount(true);
interceptor.setOutputLastTime(true);
interceptor.setOutputLastExceptionTime(true);
interceptor.setOutputLastErrorTime(true);
interceptor.setOutputBestPerformance(true);
interceptor.setOutputBestPerformanceTime(true);
interceptor.setOutputWorstPerformance(true);
interceptor.setOutputWorstPerformanceTime(true);
interceptor.setOutputAveragePerformance(true);
ServiceManagerFactory.findManager("Test").startAllService();
assertEquals("test", new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {
public Object invoke(InvocationContext context) throws Throwable {
Thread.sleep(new Random().nextInt(400) + 100);
return "test";
}
}).invokeNext(new DefaultMethodInvocationContext(null, HashMap.class.getMethod("get", new Class[] { Object.class }), null)));
synchronized (category) {
if (category.elements == null) {
category.wait(2000);
}
}
assertNotNull(category.elements);
assertTrue(category.elements instanceof Map);
Map record = (Map) category.elements;
assertEquals(13, record.size());
assertEquals(new Integer(1), record.get(MethodMetricsInterceptorService.RECORD_KEY_ORDER));
assertEquals("java.util.HashMap#get(java.lang.Object)", record.get(MethodMetricsInterceptorService.RECORD_KEY_METHOD));
assertEquals(new Long(1), record.get(MethodMetricsInterceptorService.RECORD_KEY_COUNT));
assertEquals(new Long(0), record.get(MethodMetricsInterceptorService.RECORD_KEY_EXCEPTION_COUNT));
assertEquals(new Long(0), record.get(MethodMetricsInterceptorService.RECORD_KEY_ERROR_COUNT));
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_TIME));
assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_TIME)) > 0);
assertNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_EXCEPTION_TIME));
assertNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_ERROR_TIME));
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE));
assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE)) < 0);
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE_TIME));
assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE_TIME)) > 0);
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE));
assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE)) < 0);
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE_TIME));
assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE_TIME)) > 0);
assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_AVERAGE_PERFORMANCE));
assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_AVERAGE_PERFORMANCE)) < 0);
} finally {
ServiceManagerFactory.findManager("Test").stopAllService();
ServiceManagerFactory.findManager("Test").destroyAllService();
ServiceManagerFactory.unregisterManager("Test");
}
}
Aggregations