Example usage for com.google.common.collect MapEvictionListener MapEvictionListener

List of usage examples for com.google.common.collect MapEvictionListener MapEvictionListener

Introduction

In this page you can find the example usage for com.google.common.collect MapEvictionListener MapEvictionListener.

Prototype

MapEvictionListener

Source Link

Usage

From source file:com.alibaba.otter.node.etl.common.db.dialect.DbDialectFactory.java

public DbDialectFactory() {
    // map//w w w.  j a v a 2 s  . c om
    GenericMapMaker mapMaker = null;
    mapMaker = new MapMaker().softValues()
            .evictionListener(new MapEvictionListener<Long, Map<DbMediaSource, DbDialect>>() {

                public void onEviction(Long pipelineId, Map<DbMediaSource, DbDialect> dialect) {
                    if (dialect == null) {
                        return;
                    }

                    for (DbDialect dbDialect : dialect.values()) {
                        dbDialect.destory();
                    }
                }
            });

    dialects = mapMaker.makeComputingMap(new Function<Long, Map<DbMediaSource, DbDialect>>() {

        public Map<DbMediaSource, DbDialect> apply(final Long pipelineId) {
            // map
            return new MapMaker().makeComputingMap(new Function<DbMediaSource, DbDialect>() {

                public DbDialect apply(final DbMediaSource source) {
                    DataSource dataSource = dataSourceService.getDataSource(pipelineId, source);
                    final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
                    return (DbDialect) jdbcTemplate.execute(new ConnectionCallback() {

                        public Object doInConnection(Connection c) throws SQLException, DataAccessException {
                            DatabaseMetaData meta = c.getMetaData();
                            String databaseName = meta.getDatabaseProductName();
                            int databaseMajorVersion = meta.getDatabaseMajorVersion();
                            int databaseMinorVersion = meta.getDatabaseMinorVersion();
                            DbDialect dialect = dbDialectGenerator.generate(jdbcTemplate, databaseName,
                                    databaseMajorVersion, databaseMinorVersion, source.getType());
                            if (dialect == null) {
                                throw new UnsupportedOperationException("no dialect for" + databaseName);
                            }

                            if (logger.isInfoEnabled()) {
                                logger.info(String.format("--- DATABASE: %s, SCHEMA: %s ---", databaseName,
                                        (dialect.getDefaultSchema() == null) ? dialect.getDefaultCatalog()
                                                : dialect.getDefaultSchema()));
                            }

                            return dialect;
                        }
                    });

                }
            });
        }
    });

}

From source file:com.alibaba.otter.common.push.datasource.media.MediaPushDataSourceHandler.java

public MediaPushDataSourceHandler() {
    // soft/*  w w  w .  j av a 2s .  co m*/
    GenericMapMaker mapMaker = new MapMaker().softValues();
    mapMaker = ((MapMaker) mapMaker)
            .evictionListener(new MapEvictionListener<Long, Map<DbMediaSource, DataSource>>() {

                public void onEviction(Long pipelineId, Map<DbMediaSource, DataSource> dataSources) {
                    if (dataSources == null) {
                        return;
                    }

                    for (DataSource dataSource : dataSources.values()) {
                        try {
                            MediaPushDataSource mediaPushDataSource = (MediaPushDataSource) dataSource;
                            mediaPushDataSource.destory();
                        } catch (SQLException e) {
                            log.error("ERROR ## close the datasource has an error", e);
                        }
                    }
                }
            });

    // map
    dataSources = new MapMaker().makeComputingMap(new Function<Long, Map<DbMediaSource, DataSource>>() {

        public Map<DbMediaSource, DataSource> apply(Long pipelineId) {
            // map
            return new MapMaker().makeComputingMap(new Function<DbMediaSource, DataSource>() {

                public DataSource apply(DbMediaSource dbMediaSource) {
                    return createDataSource(dbMediaSource.getUrl(), dbMediaSource.getUsername(),
                            dbMediaSource.getPassword(), dbMediaSource.getDriver(), dbMediaSource.getType(),
                            dbMediaSource.getEncode());
                }

            });
        }
    });
}

From source file:com.alibaba.otter.manager.biz.remote.impl.NodeMBeanServiceImpl.java

public NodeMBeanServiceImpl() {
    try {//  w w w  . j a v  a2 s  .com
        objectName = new ObjectName(MBEAN_NAME);
    } catch (Exception e) {
        throw new ManagerException(e);
    }

    GenericMapMaker mapMaker = null;
    mapMaker = new MapMaker().expireAfterAccess(5, TimeUnit.MINUTES).softValues()
            .evictionListener(new MapEvictionListener<Long, MBeanServerConnection>() {

                public void onEviction(Long nid, MBeanServerConnection mbeanServer) {
                    // do nothing
                }
            });

    mbeanServers = mapMaker.makeComputingMap(new Function<Long, MBeanServerConnection>() {

        public MBeanServerConnection apply(Long nid) {
            Node node = nodeService.findById(nid);
            String ip = node.getIp();
            if (node.getParameters().getUseExternalIp()) {
                ip = node.getParameters().getExternalIp();
            }

            int port = node.getPort().intValue() + 1;
            Integer mbeanPort = node.getParameters().getMbeanPort();
            if (mbeanPort != null && mbeanPort != 0) {// ??<=4.2.2mbeanPort
                port = mbeanPort;
            }

            try {
                JMXServiceURL serviceURL = new JMXServiceURL(
                        MessageFormat.format(SERVICE_URL, ip, String.valueOf(port)));
                JMXConnector cntor = JMXConnectorFactory.connect(serviceURL, null);
                MBeanServerConnection mbsc = cntor.getMBeanServerConnection();
                return mbsc;
            } catch (Exception e) {
                throw new ManagerException(e);
            }
        }

    });
}

From source file:com.alibaba.otter.node.etl.common.datasource.impl.DBDataSourceService.java

public DBDataSourceService() {
    // soft//from   ww w  .  j av a2s .  c  o m
    GenericMapMaker mapMaker = new MapMaker().softValues();
    mapMaker = ((MapMaker) mapMaker)
            .evictionListener(new MapEvictionListener<Long, Map<DbMediaSource, DataSource>>() {

                public void onEviction(Long pipelineId, Map<DbMediaSource, DataSource> dataSources) {
                    if (dataSources == null) {
                        return;
                    }

                    for (DataSource dataSource : dataSources.values()) {
                        // for filter to destroy custom datasource
                        if (letHandlerDestroyIfSupport(pipelineId, dataSource)) {
                            continue;
                        }
                        BasicDataSource basicDataSource = (BasicDataSource) dataSource;
                        try {
                            basicDataSource.close();
                        } catch (SQLException e) {
                            logger.error("ERROR ## close the datasource has an error", e);
                        }
                    }
                }
            });

    // map
    dataSources = new MapMaker().makeComputingMap(new Function<Long, Map<DbMediaSource, DataSource>>() {

        public Map<DbMediaSource, DataSource> apply(final Long pipelineId) {
            // map
            return new MapMaker().makeComputingMap(new Function<DbMediaSource, DataSource>() {

                public DataSource apply(DbMediaSource dbMediaSource) {

                    // ,? dataSource
                    DataSource customDataSource = preCreate(pipelineId, dbMediaSource);
                    if (customDataSource != null) {
                        return customDataSource;
                    }

                    return createDataSource(dbMediaSource.getUrl(), dbMediaSource.getUsername(),
                            dbMediaSource.getPassword(), dbMediaSource.getDriver(), dbMediaSource.getType(),
                            dbMediaSource.getEncode());
                }

            });
        }
    });

}

From source file:com.alibaba.otter.node.etl.common.db.dialect.AbstractDbDialect.java

private void initTables(final JdbcTemplate jdbcTemplate) {
    // soft??/* w ww.  j a  v  a2 s.  c  om*/
    GenericMapMaker mapMaker = null;
    mapMaker = new MapMaker().softValues().evictionListener(new MapEvictionListener<List<String>, Table>() {

        public void onEviction(List<String> names, Table table) {
            logger.warn("Eviction For Table:" + table);
        }
    });

    this.tables = mapMaker.makeComputingMap(new Function<List<String>, Table>() {

        public Table apply(List<String> names) {
            Assert.isTrue(names.size() == 2);
            try {
                beforeFindTable(jdbcTemplate, names.get(0), names.get(0), names.get(1));
                DdlUtilsFilter filter = getDdlUtilsFilter(jdbcTemplate, names.get(0), names.get(0),
                        names.get(1));
                Table table = DdlUtils.findTable(jdbcTemplate, names.get(0), names.get(0), names.get(1),
                        filter);
                afterFindTable(table, jdbcTemplate, names.get(0), names.get(0), names.get(1));
                if (table == null) {
                    throw new NestableRuntimeException(
                            "no found table [" + names.get(0) + "." + names.get(1) + "] , pls check");
                } else {
                    return table;
                }
            } catch (Exception e) {
                throw new NestableRuntimeException(
                        "find table [" + names.get(0) + "." + names.get(1) + "] error", e);
            }
        }
    });
}