Search in sources :

Example 1 with DefaultThreadLocalInterceptorChain

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

the class FlowControlInterceptorServiceTest method test2.

public void test2() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    final ServiceMetaData interceptorServiceData = new ServiceMetaData();
    interceptorServiceData.setCode(FlowControlInterceptorService.class.getName());
    interceptorServiceData.setName("FlowControlInterceptor");
    interceptorServiceData.addDepends(interceptorServiceData.createDependsMetaData("Test", "Semaphore"));
    ServiceManagerFactory.registerService("Test", interceptorServiceData);
    FlowControlInterceptorService interceptor = (FlowControlInterceptorService) ServiceManagerFactory.getService("Test", "FlowControlInterceptor");
    final DefaultSemaphoreService semaphore = new DefaultSemaphoreService();
    ServiceManagerFactory.registerService("Test", "Semaphore", semaphore);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setSemaphoreServiceName(new ServiceName("Test", "Semaphore"));
        interceptor.setTimeout(200l);
        semaphore.setResourceCapacity(1);
        ServiceManagerFactory.findManager("Test").startAllService();
        final InterceptorChain chain = new DefaultThreadLocalInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                Thread.sleep(500);
                return "test";
            }
        });
        Runnable runner = new Runnable() {

            public void run() {
                try {
                    chain.invokeNext(new DefaultMethodInvocationContext());
                } catch (Throwable th) {
                }
            }
        };
        Thread thread = new Thread(runner);
        thread.start();
        Thread.sleep(100);
        try {
            chain.invokeNext(new DefaultMethodInvocationContext());
            fail();
        } catch (FailToObtainSemaphoreException e) {
        }
        thread.join();
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) ServiceMetaData(jp.ossc.nimbus.core.ServiceMetaData) DefaultThreadLocalInterceptorChain(jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain) DefaultThreadLocalInterceptorChain(jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) Invoker(jp.ossc.nimbus.service.aop.Invoker) ServiceName(jp.ossc.nimbus.core.ServiceName) DefaultSemaphoreService(jp.ossc.nimbus.service.semaphore.DefaultSemaphoreService) 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)

Example 2 with DefaultThreadLocalInterceptorChain

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

the class FlowControlInterceptorServiceTest method test1.

public void test1() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    final ServiceMetaData interceptorServiceData = new ServiceMetaData();
    interceptorServiceData.setCode(FlowControlInterceptorService.class.getName());
    interceptorServiceData.setName("FlowControlInterceptor");
    interceptorServiceData.addDepends(interceptorServiceData.createDependsMetaData("Test", "Semaphore"));
    ServiceManagerFactory.registerService("Test", interceptorServiceData);
    FlowControlInterceptorService interceptor = (FlowControlInterceptorService) ServiceManagerFactory.getService("Test", "FlowControlInterceptor");
    final DefaultSemaphoreService semaphore = new DefaultSemaphoreService();
    ServiceManagerFactory.registerService("Test", "Semaphore", semaphore);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setSemaphoreServiceName(new ServiceName("Test", "Semaphore"));
        semaphore.setResourceCapacity(2);
        ServiceManagerFactory.findManager("Test").startAllService();
        class Counter {

            public volatile int count;

            public boolean isAssertFail;
        }
        final Counter counter = new Counter();
        final InterceptorChain chain = new DefaultThreadLocalInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                try {
                    synchronized (counter) {
                        counter.count++;
                    }
                    Thread.sleep(new Random().nextInt(100));
                    return "test";
                } finally {
                    synchronized (counter) {
                        counter.count--;
                    }
                }
            }
        });
        Runnable runner = new Runnable() {

            public void run() {
                for (int i = 0; i < 10; i++) {
                    counter.isAssertFail |= counter.count > semaphore.getResourceCapacity();
                    try {
                        chain.invokeNext(new DefaultMethodInvocationContext());
                    } catch (Throwable th) {
                    }
                    counter.isAssertFail |= counter.count > semaphore.getResourceCapacity();
                }
            }
        };
        Thread[] threads = new Thread[10];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(runner);
        }
        Random random = new Random();
        for (int i = 0; i < threads.length; i++) {
            threads[i].start();
            Thread.sleep(random.nextInt(100));
        }
        for (int i = 0; i < threads.length; i++) {
            threads[i].join();
        }
        assertFalse(counter.isAssertFail);
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) ServiceMetaData(jp.ossc.nimbus.core.ServiceMetaData) DefaultThreadLocalInterceptorChain(jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain) DefaultThreadLocalInterceptorChain(jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) Invoker(jp.ossc.nimbus.service.aop.Invoker) Random(java.util.Random) ServiceName(jp.ossc.nimbus.core.ServiceName) DefaultSemaphoreService(jp.ossc.nimbus.service.semaphore.DefaultSemaphoreService) 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)

Example 3 with DefaultThreadLocalInterceptorChain

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

the class FlowControlInterceptorServiceTest method test3.

public void test3() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    final ServiceMetaData interceptorServiceData = new ServiceMetaData();
    interceptorServiceData.setCode(FlowControlInterceptorService.class.getName());
    interceptorServiceData.setName("FlowControlInterceptor");
    interceptorServiceData.addDepends(interceptorServiceData.createDependsMetaData("Test", "Semaphore"));
    ServiceManagerFactory.registerService("Test", interceptorServiceData);
    FlowControlInterceptorService interceptor = (FlowControlInterceptorService) ServiceManagerFactory.getService("Test", "FlowControlInterceptor");
    final DefaultSemaphoreService semaphore = new DefaultSemaphoreService();
    ServiceManagerFactory.registerService("Test", "Semaphore", semaphore);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setSemaphoreServiceName(new ServiceName("Test", "Semaphore"));
        interceptor.setTimeout(200l);
        interceptor.setFailToObtainSemaphore(false);
        semaphore.setResourceCapacity(1);
        ServiceManagerFactory.findManager("Test").startAllService();
        final InterceptorChain chain = new DefaultThreadLocalInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                Thread.sleep(500);
                return "test";
            }
        });
        Runnable runner = new Runnable() {

            public void run() {
                try {
                    chain.invokeNext(new DefaultMethodInvocationContext());
                } catch (Throwable th) {
                }
            }
        };
        Thread thread = new Thread(runner);
        thread.start();
        Thread.sleep(100);
        assertNull(chain.invokeNext(new DefaultMethodInvocationContext()));
        thread.join();
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) ServiceMetaData(jp.ossc.nimbus.core.ServiceMetaData) DefaultThreadLocalInterceptorChain(jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain) DefaultThreadLocalInterceptorChain(jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) Invoker(jp.ossc.nimbus.service.aop.Invoker) ServiceName(jp.ossc.nimbus.core.ServiceName) DefaultSemaphoreService(jp.ossc.nimbus.service.semaphore.DefaultSemaphoreService) 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)

Example 4 with DefaultThreadLocalInterceptorChain

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

the class AOPInterceptorAdaptorInterceptorService method startService.

public void startService() throws Exception {
    if (interceptorServiceName == null && interceptor == null) {
        throw new IllegalArgumentException("InterceptorServiceName or Interceptor must be specified.");
    }
    final DefaultInterceptorChainListService chainList = new DefaultInterceptorChainListService();
    if (interceptorServiceName != null) {
        chainList.setInterceptorServiceNames(new ServiceName[] { interceptorServiceName });
    } else if (interceptor != null) {
        chainList.setInterceptors(new jp.ossc.nimbus.service.aop.Interceptor[] { interceptor });
    }
    chainList.create();
    chainList.start();
    final Invoker invoker = new Invoker();
    chain = new DefaultThreadLocalInterceptorChain(chainList, invoker);
}
Also used : DefaultInterceptorChainListService(jp.ossc.nimbus.service.aop.DefaultInterceptorChainListService) DefaultThreadLocalInterceptorChain(jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain)

Aggregations

DefaultThreadLocalInterceptorChain (jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain)4 ServiceMetaData (jp.ossc.nimbus.core.ServiceMetaData)3 ServiceName (jp.ossc.nimbus.core.ServiceName)3 DefaultInterceptorChainList (jp.ossc.nimbus.service.aop.DefaultInterceptorChainList)3 DefaultMethodInvocationContext (jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext)3 Interceptor (jp.ossc.nimbus.service.aop.Interceptor)3 InterceptorChain (jp.ossc.nimbus.service.aop.InterceptorChain)3 InvocationContext (jp.ossc.nimbus.service.aop.InvocationContext)3 Invoker (jp.ossc.nimbus.service.aop.Invoker)3 DefaultSemaphoreService (jp.ossc.nimbus.service.semaphore.DefaultSemaphoreService)3 Random (java.util.Random)1 DefaultInterceptorChainListService (jp.ossc.nimbus.service.aop.DefaultInterceptorChainListService)1