如何用Java代码列出JBoss AS 7数据源属性? [英] How to list JBoss AS 7 datasource properties in Java code?

查看:120
本文介绍了如何用Java代码列出JBoss AS 7数据源属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行JBoss AS 7.1.0.CR1b。我在standalone.xml中定义了几个数据源,例如

I'm running JBoss AS 7.1.0.CR1b. I've got several datasources defined in my standalone.xml e.g.

        <subsystem xmlns="urn:jboss:domain:datasources:1.0">
            <datasources>
                <datasource jndi-name="java:/MyDS" pool-name="MyDS_Pool" enabled="true" use-java-context="true" use-ccm="true">
                    <connection-url>some-url</connection-url>
                    <driver>the-driver</driver>
                    [etc]

一切正常。

我正在尝试访问代码中包含的信息-特别是连接URL 驱动程序属性。

I'm trying to access the information contained here within my code - specifically the connection-url and driver properties.

我尝试像往常一样从JNDI获取数据源,但是它似乎无法提供对这些属性的访问:

I've tried getting the Datasource from JNDI, as normal, but it doesn't appear to provide access to these properties:

// catches removed
InitialContext context;
DataSource dataSource = null;
context = new InitialContext();
dataSource = (DataSource) context.lookup(jndi);

来自此数据源的Connection对象的ClientInfo和DatabaseMetadata也不包含这些粒度的JBoss属性

ClientInfo and DatabaseMetadata from a Connection object from this Datasource also don't contain these granular, JBoss properties either.

我的代码将在指定数据源的容器内运行,因此所有代码都应可用。我看了IronJacamar接口 org.jboss.jca.common.api.metadata.ds.DataSource 及其实现类,它们似乎具有可访问的钩子我需要的信息,但是我找不到有关如何使用容器中已部署的资源创建此类对象的任何信息(仅impl的构造函数涉及手动输入所有属性)。

My code will be running inside the container with the datasource specfied, so all should be available. I've looked at the IronJacamar interface org.jboss.jca.common.api.metadata.ds.DataSource, and its implementing class, and these seem to have accessible hooks to the information I require, but I can't find any information on how to create such objects with these already deployed resources within the container (only constructor on impl involves inputting all properties manually).

JBoss AS 7的命令行界面允许您浏览和列出数据源作为目录系统。 http://www.paykin.info/java/add- datasource-programaticaly-cli-jboss-7 / 提供了一篇很棒的文章,介绍了如何使用我认为是Java Management API与子系统进行交互的方法,但这似乎涉及到连接到目标JBoss服务器。我的代码已经在该服务器上运行 ,因此肯定有一种更简单的方法可以做到这一点?

JBoss AS 7's Command-Line Interface allows you to navigate and list the datasources as a directory system. http://www.paykin.info/java/add-datasource-programaticaly-cli-jboss-7/ provides an excellent post on how to use what I believe is the Java Management API to interact with the subsystem, but this appears to involve connecting to the target JBoss server. My code is already running within that server, so surely there must be an easier way to do this?

希望有人可以提供帮助。非常感谢。

Hope somebody can help. Many thanks.

推荐答案

您真正想做的是一项管理操作。最好的方法是使用可用的管理API。

What you're really trying to do is a management action. The best way to is to use the management API's that are available.

以下是一个简单的独立示例:

Here is a simple standalone example:

public class Main {

    public static void main(final String[] args) throws Exception {
        final List<ModelNode> dataSources = getDataSources();
        for (ModelNode dataSource : dataSources) {
            System.out.printf("Datasource: %s%n", dataSource.asString());
        }
    }

    public static List<ModelNode> getDataSources() throws IOException {
        final ModelNode request = new ModelNode();
        request.get(ClientConstants.OP).set("read-resource");
        request.get("recursive").set(true);
        request.get(ClientConstants.OP_ADDR).add("subsystem", "datasources");
        ModelControllerClient client = null;
        try {
            client = ModelControllerClient.Factory.create(InetAddress.getByName("127.0.0.1"), 9999);
            final ModelNode response = client.execute(new OperationBuilder(request).build());
            reportFailure(response);
            return response.get(ClientConstants.RESULT).get("data-source").asList();
        } finally {
            safeClose(client);
        }
    }

    public static void safeClose(final Closeable closeable) {
        if (closeable != null) try {
            closeable.close();
        } catch (Exception e) {
            // no-op
        }
    }


    private static void reportFailure(final ModelNode node) {
        if (!node.get(ClientConstants.OUTCOME).asString().equals(ClientConstants.SUCCESS)) {
            final String msg;
            if (node.hasDefined(ClientConstants.FAILURE_DESCRIPTION)) {
                if (node.hasDefined(ClientConstants.OP)) {
                    msg = String.format("Operation '%s' at address '%s' failed: %s", node.get(ClientConstants.OP), node.get(ClientConstants.OP_ADDR), node.get(ClientConstants.FAILURE_DESCRIPTION));
                } else {
                    msg = String.format("Operation failed: %s", node.get(ClientConstants.FAILURE_DESCRIPTION));
                }
            } else {
                msg = String.format("Operation failed: %s", node);
            }
            throw new RuntimeException(msg);
        }
    }
}

想到的是添加依赖于服务器内部的模块。可以做到,但是我可能会先使用管理API。

The only other way I can think of is to add module that relies on servers internals. It could be done, but I would probably use the management API first.

这篇关于如何用Java代码列出JBoss AS 7数据源属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆