@SuppressWarnings( "unchecked" ) public T proxy() { TransientComposite composite = get(); Stream<Class<?>> types = PolygeneAPI.FUNCTION_COMPOSITE_INSTANCE_OF.apply( composite ).types(); return (T) Proxy.newProxyInstance( composite.getClass().getClassLoader(), types.toArray( Class[]::new ), new ContextInvocationhandler() ); }
@Test public void testThreadScope() throws InterruptedException { for( int i = 0; i < 5; i++ ) { TransientBuilder<MyCompositeContext> builder = transientBuilderFactory.newTransientBuilder( MyCompositeContext.class ); builder.prototypeFor( MyData.class ).data().set( 0 ); MyCompositeContext context = new CompositeContext<>( module, MyCompositeContext.class ).proxy(); Worker w1; Worker w2; MyCompositeContext c1 = builder.newInstance(); { w1 = new Worker( "w1", context, 100, 0 ); w2 = new Worker( "w2", context, 400, 20 ); w2.start(); w1.start(); } w1.join(); w2.join(); System.out.println( "W1: " + w1.getData() ); System.out.println( "W2: " + w2.getData() ); assertEquals( 0, (int) c1.data().get() ); assertEquals( 100, w1.getData() ); assertEquals( 400, w2.getData() ); } }
@SuppressWarnings( "unchecked" ) public T proxy() { TransientComposite composite = get(); Stream<Class<?>> types = PolygeneAPI.FUNCTION_COMPOSITE_INSTANCE_OF.apply( composite ).types(); return (T) Proxy.newProxyInstance( composite.getClass().getClassLoader(), types.toArray( Class[]::new ), new ContextInvocationhandler() ); }