Example usage for org.springframework.aop.target HotSwappableTargetSource HotSwappableTargetSource

List of usage examples for org.springframework.aop.target HotSwappableTargetSource HotSwappableTargetSource

Introduction

In this page you can find the example usage for org.springframework.aop.target HotSwappableTargetSource HotSwappableTargetSource.

Prototype

public HotSwappableTargetSource(Object initialTarget) 

Source Link

Document

Create a new HotSwappableTargetSource with the given initial target object.

Usage

From source file:marshalsec.gadgets.ToStringUtil.java

public static Object makeSpringAOPToStringTrigger(Object o) throws Exception {
    return makeToStringTrigger(o, x -> {
        return new HotSwappableTargetSource(x);
    });/*from  w  w  w  .  j  a  v a 2 s . c  o  m*/
}

From source file:cn.vko.cache.dao.ha.FailoverHotSwapDataSourceCreator.java

@Override
public DataSource createHADataSource(DataSourceDescriptor descriptor) throws Exception {
    DataSource activeDataSource = descriptor.getTarget();
    DataSource standbyDataSource = descriptor.getStandby();
    if (activeDataSource == null && standbyDataSource == null) {
        throw new IllegalArgumentException("must have at least one data source active.");
    }/*from  w  w  w.  ja va 2 s .co  m*/
    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.getTarget();
        DataSource standbyDetectorDataSource = descriptor.getStandby();
        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();
}

From source file:com.alibaba.cobar.client.datasources.ha.FailoverHotSwapDataSourceCreator.java

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.");
    }// w  w w  . j a  va 2  s.c  om
    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();
}

From source file:org.springframework.osgi.service.OsgiServiceProxyFactoryBean.java

/**
 * We proxy the actual service so that we can listen to service events
 * and rebind transparently if the service goes down and comes back up
 * for example//  w w w .j a  v  a2 s  .c om
 */
private Object getServiceProxyFor(Object target, String lookupFilter) {
    ProxyFactory pf = new ProxyFactory();
    if (getServiceType().isInterface()) {
        pf.setInterfaces(new Class[] { getServiceType() });
    }
    HotSwappableTargetSource targetSource = new HotSwappableTargetSource(target);
    pf.setTargetSource(targetSource);
    OsgiServiceInterceptor interceptor = new OsgiServiceInterceptor(this.bundleContext, this.serviceReference,
            targetSource, getServiceType(), lookupFilter);
    interceptor.setMaxRetries(this.retryOnUnregisteredService ? this.maxRetries : 0);
    interceptor.setRetryIntervalMillis(this.millisBetweenRetries);
    pf.addAdvice(interceptor);
    ClassLoader classLoader = ((DefaultResourceLoader) this.applicationContext).getClassLoader();
    return pf.getProxy(classLoader);
}