org.springframework.aop.target.HotSwappableTargetSource Java Examples

The following examples show how to use org.springframework.aop.target.HotSwappableTargetSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BeanFactoryTransactionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Test that we can set the target to a dynamic TargetSource.
 */
@Test
public void testDynamicTargetSource() {
	// Install facade
	CallCountingTransactionManager txMan = new CallCountingTransactionManager();
	PlatformTransactionManagerFacade.delegate = txMan;

	TestBean tb = (TestBean) factory.getBean("hotSwapped");
	assertEquals(666, tb.getAge());
	int newAge = 557;
	tb.setAge(newAge);
	assertEquals(newAge, tb.getAge());

	TestBean target2 = new TestBean();
	target2.setAge(65);
	HotSwappableTargetSource ts = (HotSwappableTargetSource) factory.getBean("swapper");
	ts.swap(target2);
	assertEquals(target2.getAge(), tb.getAge());
	tb.setAge(newAge);
	assertEquals(newAge, target2.getAge());

	assertEquals(0, txMan.inflight);
	assertEquals(2, txMan.commits);
	assertEquals(0, txMan.rollbacks);
}
 
Example #2
Source File: BeanFactoryTransactionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Test that we can set the target to a dynamic TargetSource.
 */
@Test
public void testDynamicTargetSource() {
	// Install facade
	CallCountingTransactionManager txMan = new CallCountingTransactionManager();
	PlatformTransactionManagerFacade.delegate = txMan;

	TestBean tb = (TestBean) factory.getBean("hotSwapped");
	assertEquals(666, tb.getAge());
	int newAge = 557;
	tb.setAge(newAge);
	assertEquals(newAge, tb.getAge());

	TestBean target2 = new TestBean();
	target2.setAge(65);
	HotSwappableTargetSource ts = (HotSwappableTargetSource) factory.getBean("swapper");
	ts.swap(target2);
	assertEquals(target2.getAge(), tb.getAge());
	tb.setAge(newAge);
	assertEquals(newAge, target2.getAge());

	assertEquals(0, txMan.inflight);
	assertEquals(2, txMan.commits);
	assertEquals(0, txMan.rollbacks);
}
 
Example #3
Source File: BeanFactoryTransactionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Test that we can set the target to a dynamic TargetSource.
 */
@Test
public void testDynamicTargetSource() throws NoSuchMethodException {
	// Install facade
	CallCountingTransactionManager txMan = new CallCountingTransactionManager();
	PlatformTransactionManagerFacade.delegate = txMan;

	TestBean tb = (TestBean) factory.getBean("hotSwapped");
	assertEquals(666, tb.getAge());
	int newAge = 557;
	tb.setAge(newAge);
	assertEquals(newAge, tb.getAge());

	TestBean target2 = new TestBean();
	target2.setAge(65);
	HotSwappableTargetSource ts = (HotSwappableTargetSource) factory.getBean("swapper");
	ts.swap(target2);
	assertEquals(target2.getAge(), tb.getAge());
	tb.setAge(newAge);
	assertEquals(newAge, target2.getAge());

	assertEquals(0, txMan.inflight);
	assertEquals(2, txMan.commits);
	assertEquals(0, txMan.rollbacks);
}
 
Example #4
Source File: ReloadableComponentProxy.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void afterPropertiesSet() throws Exception {
   // run this when the bean is loaded

   // auto set the name if it is not set
   if (sakaiComponentName == null || "".equals(sakaiComponentName)) {
      Class<?>[] interfaces = getProxiedInterfaces();
      if (interfaces.length > 0) {
         sakaiComponentName = interfaces[0].getName();
         log.info("Autogenerating component name from interface: " + sakaiComponentName);
      }
   }

   // get the component from the Sakai CM if it is in there
   Object obj = ComponentManager.get(sakaiComponentName);

   /*
    * If the obj is null, that means this is the first time we have loaded
    * the component, so we will add it to the Component Manager. If this
    * component is already available from the component manager, then we
    * will simply update it's proxy target.
    */
   if (obj == null) {
      this.setTargetSource(new HotSwappableTargetSource(localSakaiComponentBean));  
      ComponentManager.loadComponent(sakaiComponentName, this);
   }
   else {
      try {
         Method getTargetSource = obj.getClass().getMethod("getTargetSource");              
         HotSwappableTargetSource hsts = (HotSwappableTargetSource) getTargetSource.invoke(obj);
         hsts.swap(localSakaiComponentBean);
      } catch (Exception e) {
         log.error("Unable to update reloadable SakaiComponent: " + sakaiComponentName, e);
      }
   }
   log.info("Added component proxy from webapp to component manager: " + sakaiComponentName);
}
 
Example #5
Source File: ReloadableComponentProxy.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void afterPropertiesSet() throws Exception {
   // run this when the bean is loaded

   // auto set the name if it is not set
   if (sakaiComponentName == null || "".equals(sakaiComponentName)) {
      Class<?>[] interfaces = getProxiedInterfaces();
      if (interfaces.length > 0) {
         sakaiComponentName = interfaces[0].getName();
         log.info("Autogenerating component name from interface: " + sakaiComponentName);
      }
   }

   // get the component from the Sakai CM if it is in there
   Object obj = ComponentManager.get(sakaiComponentName);

   /*
    * If the obj is null, that means this is the first time we have loaded
    * the component, so we will add it to the Component Manager. If this
    * component is already available from the component manager, then we
    * will simply update it's proxy target.
    */
   if (obj == null) {
      this.setTargetSource(new HotSwappableTargetSource(localSakaiComponentBean));  
      ComponentManager.loadComponent(sakaiComponentName, this);
   }
   else {
      try {
         Method getTargetSource = obj.getClass().getMethod("getTargetSource");              
         HotSwappableTargetSource hsts = (HotSwappableTargetSource) getTargetSource.invoke(obj);
         hsts.swap(localSakaiComponentBean);
      } catch (Exception e) {
         log.error("Unable to update reloadable SakaiComponent: " + sakaiComponentName, e);
      }
   }
   log.info("Added component proxy from webapp to component manager: " + sakaiComponentName);
}
 
Example #6
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testExistingProxyChangesTarget() throws Throwable {
	TestBean tb1 = new TestBean();
	tb1.setAge(33);

	TestBean tb2 = new TestBean();
	tb2.setAge(26);
	tb2.setName("Juergen");
	TestBean tb3 = new TestBean();
	tb3.setAge(37);
	ProxyFactory pc = new ProxyFactory(tb1);
	NopInterceptor nop = new NopInterceptor();
	pc.addAdvice(nop);
	ITestBean proxy = (ITestBean) createProxy(pc);
	assertEquals(nop.getCount(), 0);
	assertEquals(tb1.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 1);
	// Change to a new static target
	pc.setTarget(tb2);
	assertEquals(tb2.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 2);

	// Change to a new dynamic target
	HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3);
	pc.setTargetSource(hts);
	assertEquals(tb3.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 3);
	hts.swap(tb1);
	assertEquals(tb1.getAge(), proxy.getAge());
	tb1.setName("Colin");
	assertEquals(tb1.getName(), proxy.getName());
	assertEquals(nop.getCount(), 5);

	// Change back, relying on casting to Advised
	Advised advised = (Advised) proxy;
	assertSame(hts, advised.getTargetSource());
	SingletonTargetSource sts = new SingletonTargetSource(tb2);
	advised.setTargetSource(sts);
	assertEquals(tb2.getName(), proxy.getName());
	assertSame(sts, advised.getTargetSource());
	assertEquals(tb2.getAge(), proxy.getAge());
}
 
Example #7
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testExistingProxyChangesTarget() throws Throwable {
	TestBean tb1 = new TestBean();
	tb1.setAge(33);

	TestBean tb2 = new TestBean();
	tb2.setAge(26);
	tb2.setName("Juergen");
	TestBean tb3 = new TestBean();
	tb3.setAge(37);
	ProxyFactory pc = new ProxyFactory(tb1);
	NopInterceptor nop = new NopInterceptor();
	pc.addAdvice(nop);
	ITestBean proxy = (ITestBean) createProxy(pc);
	assertEquals(nop.getCount(), 0);
	assertEquals(tb1.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 1);
	// Change to a new static target
	pc.setTarget(tb2);
	assertEquals(tb2.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 2);

	// Change to a new dynamic target
	HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3);
	pc.setTargetSource(hts);
	assertEquals(tb3.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 3);
	hts.swap(tb1);
	assertEquals(tb1.getAge(), proxy.getAge());
	tb1.setName("Colin");
	assertEquals(tb1.getName(), proxy.getName());
	assertEquals(nop.getCount(), 5);

	// Change back, relying on casting to Advised
	Advised advised = (Advised) proxy;
	assertSame(hts, advised.getTargetSource());
	SingletonTargetSource sts = new SingletonTargetSource(tb2);
	advised.setTargetSource(sts);
	assertEquals(tb2.getName(), proxy.getName());
	assertSame(sts, advised.getTargetSource());
	assertEquals(tb2.getAge(), proxy.getAge());
}
 
Example #8
Source File: ToStringUtil.java    From learnjavabug with MIT License 4 votes vote down vote up
public static Object makeSpringAOPToStringTrigger ( Object o ) throws Exception {
    return makeToStringTrigger(o, x -> {
        return new HotSwappableTargetSource(x);
    });
}
 
Example #9
Source File: ToStringUtil.java    From marshalsec with MIT License 4 votes vote down vote up
public static Object makeSpringAOPToStringTrigger ( Object o ) throws Exception {
    return makeToStringTrigger(o, x -> {
        return new HotSwappableTargetSource(x);
    });
}
 
Example #10
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testExistingProxyChangesTarget() throws Throwable {
	TestBean tb1 = new TestBean();
	tb1.setAge(33);

	TestBean tb2 = new TestBean();
	tb2.setAge(26);
	tb2.setName("Juergen");
	TestBean tb3 = new TestBean();
	tb3.setAge(37);
	ProxyFactory pc = new ProxyFactory(tb1);
	NopInterceptor nop = new NopInterceptor();
	pc.addAdvice(nop);
	ITestBean proxy = (ITestBean) createProxy(pc);
	assertEquals(nop.getCount(), 0);
	assertEquals(tb1.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 1);
	// Change to a new static target
	pc.setTarget(tb2);
	assertEquals(tb2.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 2);

	// Change to a new dynamic target
	HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3);
	pc.setTargetSource(hts);
	assertEquals(tb3.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 3);
	hts.swap(tb1);
	assertEquals(tb1.getAge(), proxy.getAge());
	tb1.setName("Colin");
	assertEquals(tb1.getName(), proxy.getName());
	assertEquals(nop.getCount(), 5);

	// Change back, relying on casting to Advised
	Advised advised = (Advised) proxy;
	assertSame(hts, advised.getTargetSource());
	SingletonTargetSource sts = new SingletonTargetSource(tb2);
	advised.setTargetSource(sts);
	assertEquals(tb2.getName(), proxy.getName());
	assertSame(sts, advised.getTargetSource());
	assertEquals(tb2.getAge(), proxy.getAge());
}
 
Example #11
Source File: PassiveEventHotSwappableAdvice.java    From cobarclient with Apache License 2.0 4 votes vote down vote up
public HotSwappableTargetSource getTargetSource() {
    return targetSource;
}
 
Example #12
Source File: PassiveEventHotSwappableAdvice.java    From cobarclient with Apache License 2.0 4 votes vote down vote up
public void setTargetSource(HotSwappableTargetSource targetSource) {
    this.targetSource = targetSource;
}
 
Example #13
Source File: FailoverHotSwapDataSourceCreator.java    From cobarclient with Apache License 2.0 4 votes vote down vote up
public DataSource createHADataSource(CobarDataSourceDescriptor descriptor) throws Exception {
    DataSource activeDataSource = descriptor.getTargetDataSource();
    DataSource standbyDataSource = descriptor.getStandbyDataSource();
    if (activeDataSource == null && standbyDataSource == null) {
        throw new IllegalArgumentException("must have at least one data source active.");
    }
    if (activeDataSource == null || standbyDataSource == null) {
        logger.warn("only one data source is available for use, so no HA support.");
        if (activeDataSource == null) {
            return standbyDataSource;
        }
        return activeDataSource;
    }

    HotSwappableTargetSource targetSource = new HotSwappableTargetSource(activeDataSource);
    ProxyFactory pf = new ProxyFactory();
    pf.setInterfaces(new Class[] { DataSource.class });
    pf.setTargetSource(targetSource);
    
    
    if (isPositiveFailoverEnable()) {
        DataSource targetDetectorDataSource = descriptor.getTargetDetectorDataSource();
        DataSource standbyDetectorDataSource = descriptor.getStandbyDetectorDataSource();
        if (targetDetectorDataSource == null || standbyDetectorDataSource == null) {
            throw new IllegalArgumentException(
                    "targetDetectorDataSource or standbyDetectorDataSource can't be null if positive failover is enabled.");
        }
        // 1. create active monitoring job for failover event
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        ExecutorService jobExecutor = Executors.newFixedThreadPool(1);
        jobExecutorRegistry.add(jobExecutor);
        FailoverMonitorJob job = new FailoverMonitorJob(jobExecutor);
        //    1.1  inject dependencies
        job.setHotSwapTargetSource(targetSource);
        job.setMasterDataSource(activeDataSource);
        job.setStandbyDataSource(standbyDataSource);
        job.setMasterDetectorDataSource(targetDetectorDataSource);
        job.setStandbyDetectorDataSource(standbyDetectorDataSource);
        job.setCurrentDetectorDataSource(targetDetectorDataSource);
        job.setDetectingRequestTimeout(getDetectingTimeoutThreshold());
        job.setDetectingSQL(getDetectingSql());
        job.setRecheckInterval(recheckInterval);
        job.setRecheckTimes(recheckTimes);
        //    1.2  start scheduling and keep reference for canceling and shutdown
        ScheduledFuture<?> future = scheduler.scheduleWithFixedDelay(job, initialDelay,
                monitorPeriod, TimeUnit.MILLISECONDS);
        schedulerFutures.put(future, scheduler);
    }

    if (isPassiveFailoverEnable()) {
        // 2. create data source proxy with passive event advice
        PassiveEventHotSwappableAdvice advice = new PassiveEventHotSwappableAdvice();
        advice.setRetryInterval(recheckInterval);
        advice.setRetryTimes(recheckTimes);
        advice.setDetectingSql(detectingSql);
        advice.setTargetSource(targetSource);
        advice.setMainDataSource(activeDataSource);
        advice.setStandbyDataSource(standbyDataSource);
        pf.addAdvice(advice);
    }

    return (DataSource) pf.getProxy();
}
 
Example #14
Source File: FailoverMonitorJob.java    From cobarclient with Apache License 2.0 4 votes vote down vote up
public HotSwappableTargetSource getHotSwapTargetSource() {
    return hotSwapTargetSource;
}
 
Example #15
Source File: FailoverMonitorJob.java    From cobarclient with Apache License 2.0 4 votes vote down vote up
public void setHotSwapTargetSource(HotSwappableTargetSource hotSwapTargetSource) {
    this.hotSwapTargetSource = hotSwapTargetSource;
}