Search in sources :

Example 6 with DefaultInterceptorChain

use of jp.ossc.nimbus.service.aop.DefaultInterceptorChain in project nimbus by nimbus-org.

the class MethodMetricsInterceptorServiceTest method test3.

public void test3() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    MethodMetricsInterceptorService interceptor = new MethodMetricsInterceptorService();
    ServiceManagerFactory.registerService("Test", "MethodMetricsInterceptor", interceptor);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        ServiceManagerFactory.findManager("Test").startAllService();
        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 OutOfMemoryError();
                }
            }).invokeNext(new DefaultMethodInvocationContext(null, HashMap.class.getMethod("get", new Class[] { Object.class }), null));
            fail();
        } catch (OutOfMemoryError e) {
        }
        MetricsInfo info = interceptor.getMetricsInfo(HashMap.class.getMethod("get", new Class[] { Object.class }));
        assertNotNull(info);
        assertEquals("java.util.HashMap#get(java.lang.Object)", info.getKey());
        assertEquals(1L, info.getTotalCount());
        assertEquals(0L, info.getCount());
        assertTrue(info.getLastTime() > 0);
        assertEquals(0L, info.getExceptionCount());
        assertEquals(0L, info.getLastExceptionTime());
        assertEquals(1L, info.getErrorCount());
        assertTrue(info.getLastErrorTime() > 0);
        assertTrue(info.getBestPerformance() > 0);
        assertTrue(info.getBestPerformanceTime() > 0);
        assertTrue(info.getWorstPerformance() > 0);
        assertTrue(info.getWorstPerformanceTime() > 0);
        assertTrue(info.getAveragePerformance() > 0);
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) Invoker(jp.ossc.nimbus.service.aop.Invoker) Random(java.util.Random) HashMap(java.util.HashMap) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 7 with DefaultInterceptorChain

use of jp.ossc.nimbus.service.aop.DefaultInterceptorChain in project nimbus by nimbus-org.

the class MethodSynchronizeInterceptorServiceTest method test2.

public void test2() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    MethodSynchronizeInterceptorService interceptor = new MethodSynchronizeInterceptorService();
    ServiceManagerFactory.registerService("Test", "MethodSynchronizeInterceptor", interceptor);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setScope(MethodSynchronizeInterceptorService.SCOPE_CLASS);
        ServiceManagerFactory.findManager("Test").startAllService();
        class Counter {

            public volatile int count;
        }
        final Counter counter = new Counter();
        final InterceptorChain chain1 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                try {
                    counter.count++;
                    Thread.sleep(500);
                    return "test";
                } finally {
                    counter.count--;
                }
            }
        });
        final InterceptorChain chain2 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                assertEquals(1, counter.count);
                return "test";
            }
        });
        final InterceptorChain chain3 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                assertEquals(0, counter.count);
                return "test";
            }
        });
        Thread thread = new Thread() {

            public void run() {
                try {
                    chain1.invokeNext(new DefaultMethodInvocationContext(new HashMap(), HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
                } catch (Throwable th) {
                }
            }
        };
        thread.start();
        Thread.sleep(100);
        chain2.invokeNext(new DefaultMethodInvocationContext(new ArrayList(), ArrayList.class.getMethod("add", new Class[] { Object.class }), new Object[] { "A" }));
        chain3.invokeNext(new DefaultMethodInvocationContext(new HashMap(), HashMap.class.getMethod("put", new Class[] { Object.class, Object.class }), new Object[] { "A", new Integer(1) }));
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) Invoker(jp.ossc.nimbus.service.aop.Invoker) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 8 with DefaultInterceptorChain

use of jp.ossc.nimbus.service.aop.DefaultInterceptorChain in project nimbus by nimbus-org.

the class MethodSynchronizeInterceptorServiceTest method test1.

public void test1() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    MethodSynchronizeInterceptorService interceptor = new MethodSynchronizeInterceptorService();
    ServiceManagerFactory.registerService("Test", "MethodSynchronizeInterceptor", interceptor);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setScope(MethodSynchronizeInterceptorService.SCOPE_VM);
        ServiceManagerFactory.findManager("Test").startAllService();
        class Counter {

            public volatile int count;
        }
        final Counter counter = new Counter();
        final InterceptorChain chain1 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                try {
                    counter.count++;
                    Thread.sleep(500);
                    return "test";
                } finally {
                    counter.count--;
                }
            }
        });
        final InterceptorChain chain2 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                assertEquals(0, counter.count);
                return "test";
            }
        });
        Thread thread = new Thread() {

            public void run() {
                try {
                    chain1.invokeNext(new DefaultMethodInvocationContext(new HashMap(), HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
                } catch (Throwable th) {
                }
            }
        };
        thread.start();
        Thread.sleep(100);
        chain2.invokeNext(new DefaultMethodInvocationContext(new ArrayList(), ArrayList.class.getMethod("add", new Class[] { Object.class }), new Object[] { "A" }));
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) Invoker(jp.ossc.nimbus.service.aop.Invoker) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 9 with DefaultInterceptorChain

use of jp.ossc.nimbus.service.aop.DefaultInterceptorChain in project nimbus by nimbus-org.

the class MethodSynchronizeInterceptorServiceTest method test4.

public void test4() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    MethodSynchronizeInterceptorService interceptor = new MethodSynchronizeInterceptorService();
    ServiceManagerFactory.registerService("Test", "MethodSynchronizeInterceptor", interceptor);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setScope(MethodSynchronizeInterceptorService.SCOPE_INSTANCE);
        ServiceManagerFactory.findManager("Test").startAllService();
        class Counter {

            public volatile int count;
        }
        final Counter counter = new Counter();
        final InterceptorChain chain1 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                try {
                    counter.count++;
                    Thread.sleep(500);
                    return "test";
                } finally {
                    counter.count--;
                }
            }
        });
        final InterceptorChain chain2 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                assertEquals(1, counter.count);
                return "test";
            }
        });
        final InterceptorChain chain3 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                assertEquals(0, counter.count);
                return "test";
            }
        });
        final HashMap target = new HashMap();
        Thread thread = new Thread() {

            public void run() {
                try {
                    chain1.invokeNext(new DefaultMethodInvocationContext(target, HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
                } catch (Throwable th) {
                }
            }
        };
        thread.start();
        Thread.sleep(100);
        chain2.invokeNext(new DefaultMethodInvocationContext(new HashMap(), HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
        chain3.invokeNext(new DefaultMethodInvocationContext(target, HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) HashMap(java.util.HashMap) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) Invoker(jp.ossc.nimbus.service.aop.Invoker) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 10 with DefaultInterceptorChain

use of jp.ossc.nimbus.service.aop.DefaultInterceptorChain in project nimbus by nimbus-org.

the class NoCalledMethodMetricsInterceptorServiceTest method test1.

public void test1() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    NoCalledMethodMetricsInterceptorService interceptor = new NoCalledMethodMetricsInterceptorService();
    ServiceManagerFactory.registerService("Test", "NoCalledMethodMetricsInterceptor", interceptor);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setOutputSystemOut(false);
        interceptor.setTargetClassName("jp\\.ossc\\.nimbus\\..*");
        ServiceManagerFactory.findManager("Test").startAllService();
        final InterceptorChain chain = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                return null;
            }
        });
        Method method = StringOperator.class.getMethod("makeSpace", new Class[] { Integer.TYPE });
        assertNotNull(interceptor.getNoCalledMethodSet());
        assertTrue(interceptor.getNoCalledMethodSet().contains(method));
        chain.invokeNext(new DefaultMethodInvocationContext(null, method, new Object[] { new Integer(1) }));
        assertNotNull(interceptor.getNoCalledMethodSet());
        assertFalse(interceptor.getNoCalledMethodSet().contains(method));
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) Invoker(jp.ossc.nimbus.service.aop.Invoker) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Method(java.lang.reflect.Method) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Aggregations

DefaultInterceptorChain (jp.ossc.nimbus.service.aop.DefaultInterceptorChain)47 DefaultInterceptorChainList (jp.ossc.nimbus.service.aop.DefaultInterceptorChainList)45 InvocationContext (jp.ossc.nimbus.service.aop.InvocationContext)43 DefaultMethodInvocationContext (jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext)40 Interceptor (jp.ossc.nimbus.service.aop.Interceptor)34 Invoker (jp.ossc.nimbus.service.aop.Invoker)33 InterceptorChain (jp.ossc.nimbus.service.aop.InterceptorChain)28 HashMap (java.util.HashMap)20 Map (java.util.Map)10 Context (jp.ossc.nimbus.service.context.Context)9 Properties (java.util.Properties)6 Random (java.util.Random)6 AttributeMetaData (jp.ossc.nimbus.core.AttributeMetaData)6 ServiceName (jp.ossc.nimbus.core.ServiceName)6 MethodInvocationContext (jp.ossc.nimbus.service.aop.MethodInvocationContext)6 Method (java.lang.reflect.Method)5 ServiceMetaData (jp.ossc.nimbus.core.ServiceMetaData)5 DefaultInvocationContext (jp.ossc.nimbus.service.aop.DefaultInvocationContext)5 DefaultContextService (jp.ossc.nimbus.service.context.DefaultContextService)5 MethodReflectionCallInvokerService (jp.ossc.nimbus.service.aop.invoker.MethodReflectionCallInvokerService)4