Java Code Examples for com.alibaba.dubbo.config.spring.ServiceBean#getSpringContext()

The following examples show how to use com.alibaba.dubbo.config.spring.ServiceBean#getSpringContext() . 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: DataSourceStatusChecker.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public Status check() {
    ApplicationContext context = ServiceBean.getSpringContext();
    if (context == null) {
        return new Status(Status.Level.UNKNOWN);
    }
    Map<String, DataSource> dataSources = context.getBeansOfType(DataSource.class, false, false);
    if (dataSources == null || dataSources.size() == 0) {
        return new Status(Status.Level.UNKNOWN);
    }
    Status.Level level = Status.Level.OK;
    StringBuilder buf = new StringBuilder();
    for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) {
        DataSource dataSource = entry.getValue();
        if (buf.length() > 0) {
            buf.append(", ");
        }
        buf.append(entry.getKey());
        try {
            try (Connection connection = dataSource.getConnection()) {
                DatabaseMetaData metaData = connection.getMetaData();
                try (ResultSet resultSet = metaData.getTypeInfo()) {
                    if (!resultSet.next()) {
                        level = Status.Level.ERROR;
                    }
                }
                buf.append(metaData.getURL());
                buf.append("(");
                buf.append(metaData.getDatabaseProductName());
                buf.append("-");
                buf.append(metaData.getDatabaseProductVersion());
                buf.append(")");
            }
        } catch (Throwable e) {
            logger.warn(e.getMessage(), e);
            return new Status(level, e.getMessage());
        }
    }
    return new Status(level, buf.toString());
}
 
Example 2
Source File: DataSourceStatusChecker.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Status check() {
    ApplicationContext context = ServiceBean.getSpringContext();
    if (context == null) {
        return new Status(Status.Level.UNKNOWN);
    }
    Map<String, DataSource> dataSources = context.getBeansOfType(DataSource.class, false, false);
    if (dataSources == null || dataSources.size() == 0) {
        return new Status(Status.Level.UNKNOWN);
    }
    Status.Level level = Status.Level.OK;
    StringBuilder buf = new StringBuilder();
    for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) {
        DataSource dataSource = entry.getValue();
        if (buf.length() > 0) {
            buf.append(", ");
        }
        buf.append(entry.getKey());
        try {
            Connection connection = dataSource.getConnection();
            try {
                DatabaseMetaData metaData = connection.getMetaData();
                ResultSet resultSet = metaData.getTypeInfo();
                try {
                    if (!resultSet.next()) {
                        level = Status.Level.ERROR;
                    }
                } finally {
                    resultSet.close();
                }
                buf.append(metaData.getURL());
                buf.append("(");
                buf.append(metaData.getDatabaseProductName());
                buf.append("-");
                buf.append(metaData.getDatabaseProductVersion());
                buf.append(")");
            } finally {
                connection.close();
            }
        } catch (Throwable e) {
            logger.warn(e.getMessage(), e);
            return new Status(level, e.getMessage());
        }
    }
    return new Status(level, buf.toString());
}
 
Example 3
Source File: DataSourceStatusChecker.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public Status check() {
    ApplicationContext context = ServiceBean.getSpringContext();
    if (context == null) {
        return new Status(Status.Level.UNKNOWN);
    }
    Map<String, DataSource> dataSources = context.getBeansOfType(DataSource.class, false, false);
    if (dataSources == null || dataSources.size() == 0) {
        return new Status(Status.Level.UNKNOWN);
    }
    Status.Level level = Status.Level.OK;
    StringBuilder buf = new StringBuilder();
    for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) {
        DataSource dataSource = entry.getValue();
        if (buf.length() > 0) {
            buf.append(", ");
        }
        buf.append(entry.getKey());
        try {
            Connection connection = dataSource.getConnection();
            try {
                DatabaseMetaData metaData = connection.getMetaData();
                ResultSet resultSet = metaData.getTypeInfo();
                try {
                    if (! resultSet.next()) {
                        level = Status.Level.ERROR;
                    }
                } finally {
                    resultSet.close();
                }
                buf.append(metaData.getURL());
                buf.append("(");
                buf.append(metaData.getDatabaseProductName());
                buf.append("-");
                buf.append(metaData.getDatabaseProductVersion());
                buf.append(")");
            } finally {
                connection.close();
            }
        } catch (Throwable e) {
            logger.warn(e.getMessage(), e);
            return new Status(level, e.getMessage());
        }
    }
    return new Status(level, buf.toString());
}
 
Example 4
Source File: DataSourceStatusChecker.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public Status check() {
    ApplicationContext context = ServiceBean.getSpringContext();
    if (context == null) {
        return new Status(Status.Level.UNKNOWN);
    }
    Map<String, DataSource> dataSources = context.getBeansOfType(DataSource.class, false, false);
    if (dataSources == null || dataSources.size() == 0) {
        return new Status(Status.Level.UNKNOWN);
    }
    Status.Level level = Status.Level.OK;
    StringBuilder buf = new StringBuilder();
    for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) {
        DataSource dataSource = entry.getValue();
        if (buf.length() > 0) {
            buf.append(", ");
        }
        buf.append(entry.getKey());
        try {
            Connection connection = dataSource.getConnection();
            try {
                DatabaseMetaData metaData = connection.getMetaData();
                ResultSet resultSet = metaData.getTypeInfo();
                try {
                    if (! resultSet.next()) {
                        level = Status.Level.ERROR;
                    }
                } finally {
                    resultSet.close();
                }
                buf.append(metaData.getURL());
                buf.append("(");
                buf.append(metaData.getDatabaseProductName());
                buf.append("-");
                buf.append(metaData.getDatabaseProductVersion());
                buf.append(")");
            } finally {
                connection.close();
            }
        } catch (Throwable e) {
            logger.warn(e.getMessage(), e);
            return new Status(level, e.getMessage());
        }
    }
    return new Status(level, buf.toString());
}
 
Example 5
Source File: DataSourceStatusChecker.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public Status check() {
    ApplicationContext context = ServiceBean.getSpringContext();
    if (context == null) {
        return new Status(Status.Level.UNKNOWN);
    }
    Map<String, DataSource> dataSources = context.getBeansOfType(DataSource.class, false, false);
    if (dataSources == null || dataSources.size() == 0) {
        return new Status(Status.Level.UNKNOWN);
    }
    Status.Level level = Status.Level.OK;
    StringBuilder buf = new StringBuilder();
    for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) {
        DataSource dataSource = entry.getValue();
        if (buf.length() > 0) {
            buf.append(", ");
        }
        buf.append(entry.getKey());
        try {
            Connection connection = dataSource.getConnection();
            try {
                DatabaseMetaData metaData = connection.getMetaData();
                ResultSet resultSet = metaData.getTypeInfo();
                try {
                    if (! resultSet.next()) {
                        level = Status.Level.ERROR;
                    }
                } finally {
                    resultSet.close();
                }
                buf.append(metaData.getURL());
                buf.append("(");
                buf.append(metaData.getDatabaseProductName());
                buf.append("-");
                buf.append(metaData.getDatabaseProductVersion());
                buf.append(")");
            } finally {
                connection.close();
            }
        } catch (Throwable e) {
            logger.warn(e.getMessage(), e);
            return new Status(level, e.getMessage());
        }
    }
    return new Status(level, buf.toString());
}
 
Example 6
Source File: DataSourceStatusChecker.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public Status check() {
    ApplicationContext context = ServiceBean.getSpringContext();
    if (context == null) {
        return new Status(Status.Level.UNKNOWN);
    }
    Map<String, DataSource> dataSources = context.getBeansOfType(DataSource.class, false, false);
    if (dataSources == null || dataSources.size() == 0) {
        return new Status(Status.Level.UNKNOWN);
    }
    Status.Level level = Status.Level.OK;
    StringBuilder buf = new StringBuilder();
    for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) {
        DataSource dataSource = entry.getValue();
        if (buf.length() > 0) {
            buf.append(", ");
        }
        buf.append(entry.getKey());
        try {
            Connection connection = dataSource.getConnection();
            try {
                DatabaseMetaData metaData = connection.getMetaData();
                ResultSet resultSet = metaData.getTypeInfo();
                try {
                    if (! resultSet.next()) {
                        level = Status.Level.ERROR;
                    }
                } finally {
                    resultSet.close();
                }
                buf.append(metaData.getURL());
                buf.append("(");
                buf.append(metaData.getDatabaseProductName());
                buf.append("-");
                buf.append(metaData.getDatabaseProductVersion());
                buf.append(")");
            } finally {
                connection.close();
            }
        } catch (Throwable e) {
            logger.warn(e.getMessage(), e);
            return new Status(level, e.getMessage());
        }
    }
    return new Status(level, buf.toString());
}