Search in sources :

Example 1 with DatasourceChangeCallback

use of com.alibaba.otter.common.push.supplier.DatasourceChangeCallback in project otter by alibaba.

the class MediaPushDataSource method init.

public synchronized void init() {
    if (!dataMediaType.isMysql()) {
        throw new UnsupportedOperationException("currently only support mysql type");
    }
    if (delegate != null) {
        return;
    }
    if (dataSourceSupplier == null) {
        dataSourceSupplier = MediaDatasourceSupplier.newInstance(dbGroupKey);
        dataSourceSupplier.start();
        dataSourceSupplier.addSwtichCallback(new DatasourceChangeCallback() {

            public void masterChanged(DatasourceInfo newMaster) {
                String newUrl = buildMysqlUrl(newMaster.getAddress().getAddress().getHostAddress(), newMaster.getAddress().getPort());
                try {
                    ((BasicDataSource) delegate).close();
                    DataSource newDelegate = doCreateDataSource(newUrl);
                    delegate = newDelegate;
                } catch (SQLException e) {
                    logger.error("switch master error with url : " + originalUrl, e);
                }
            }
        });
    }
    DatasourceInfo datasourceInfo = dataSourceSupplier.fetchMaster();
    String url = buildMysqlUrl(datasourceInfo.getAddress().getAddress().getHostAddress(), datasourceInfo.getAddress().getPort());
    delegate = doCreateDataSource(url);
}
Also used : DatasourceInfo(com.alibaba.otter.common.push.supplier.DatasourceInfo) DatasourceChangeCallback(com.alibaba.otter.common.push.supplier.DatasourceChangeCallback) SQLException(java.sql.SQLException) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) CommonDataSource(javax.sql.CommonDataSource) DataSource(javax.sql.DataSource)

Example 2 with DatasourceChangeCallback

use of com.alibaba.otter.common.push.supplier.DatasourceChangeCallback in project otter by alibaba.

the class MediaHAController method start.

public void start() throws CanalHAException {
    super.start();
    if (this.supplier == null) {
        validate();
        this.supplier = MediaDatasourceSupplier.newInstance(group);
    }
    if (!this.supplier.isStart()) {
        this.supplier.start();
    }
    DatasourceInfo fetched = this.supplier.fetchMaster();
    AuthenticationInfo masterFetched = AuthenticationInfoUtils.createFrom(fetched);
    log.info(String.format("medialHAController started for  goup:[%s], and first auth info is : [%s]", this.group, masterFetched));
    this.availableAuthenticationInfo = customInfoIfNecessay(masterFetched);
    log.info(String.format("medialHAController customed for goup:[%s], and first auth info is : [%s]", this.group, this.availableAuthenticationInfo));
    this.supplier.addSwtichCallback(new DatasourceChangeCallback() {

        @Override
        public void masterChanged(DatasourceInfo newMaster) {
            AuthenticationInfo newAuthenticationInfo = AuthenticationInfoUtils.createFrom(newMaster);
            switchEventSource(newAuthenticationInfo);
        }
    });
}
Also used : DatasourceInfo(com.alibaba.otter.common.push.supplier.DatasourceInfo) DatasourceChangeCallback(com.alibaba.otter.common.push.supplier.DatasourceChangeCallback) AuthenticationInfo(com.alibaba.otter.canal.parse.support.AuthenticationInfo)

Aggregations

DatasourceChangeCallback (com.alibaba.otter.common.push.supplier.DatasourceChangeCallback)2 DatasourceInfo (com.alibaba.otter.common.push.supplier.DatasourceInfo)2 AuthenticationInfo (com.alibaba.otter.canal.parse.support.AuthenticationInfo)1 SQLException (java.sql.SQLException)1 CommonDataSource (javax.sql.CommonDataSource)1 DataSource (javax.sql.DataSource)1 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)1