java 获取jboss属性,如何用Java代码列出JBoss AS 7数据源属性?

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

some-url

the-driver

[etc]

Everything works fine.

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

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);

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

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'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.

解决方案

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 dataSources = getDataSources();

for (ModelNode dataSource : dataSources) {

System.out.printf("Datasource: %s%n", dataSource.asString());

}

}

public static List 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);

}

}

}

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值