jboss EAP 6.2+ 通过代码控制JNDI数据源

通过Jboss提供的API,可以操控JBoss,效果跟在管理控制台手动操作完全一样,下面是示例代码:

一、pom.xml添加依赖项

<dependency>
    <groupId>org.jboss.as</groupId>
    <artifactId>jboss-as-controller-client</artifactId>
    <version>7.2.0.Final</version>
</dependency>

二、创建数据源示例

/**
 * 创建jboss数据源示例
 * @throws IOException
 */
@Test
public void createJbossDataSource() throws IOException{
    String dataSourceName = "myDS";//数据源名称
    ModelNode request = new ModelNode();
    request.get(ClientConstants.OP).set(ClientConstants.ADD);
    request.get(ClientConstants.OP_ADDR).add("subsystem",
            "datasources");
    request.get(ClientConstants.OP_ADDR).add("data-source",dataSourceName);

    request.get("jndi-name").set("java:/"+dataSourceName);
    request.get("use-java-context").set(Boolean.TRUE);
    request.get("connection-url").set("jdbc:oracle:thin:@172.21.X.X:1521:orcl");
    request.get("driver-name").set("oracle");//注意:jboss上要事先添加好名为oracle的ojdbc数据驱动jar包
    request.get("driver-class").set("oracle.jdbc.driver.OracleDriver");
    request.get("min-pool-size").set(1);
    request.get("max-pool-size").set(15);
    request.get("user-name").set("你的用户名");
    request.get("password").set("你的密码");

    ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName("127.0.0.1"), 9999);
    client.execute(new OperationBuilder(request).build());
    client.close();

    System.out.println(dataSourceName + "创建完成!");
}

三、删除数据源

    /**
     * 删除指定数据源示例
     * @throws IOException
     */
    @Test
    public void deleteJbossDataSource() throws IOException {
        String datasourceName = "myDS";
        ModelNode request = new ModelNode();
        request.get(ClientConstants.OP).set("remove");
        request.get(ClientConstants.OP_ADDR).add("subsystem","datasources");
        request.get(ClientConstants.OP_ADDR).add("data-source",datasourceName);
        ModelControllerClient client = ModelControllerClient.Factory.create(
                InetAddress.getByName("127.0.0.1"), 9999);
        client.execute(new OperationBuilder(request).build());
        client.close();
        System.out.println("数据源" + datasourceName + "删除完成!");
    }

四、启用/禁用指定数据源

/**
 * 启用指定数据源
 * @throws IOException
 */
@Test
public void enableJbossDataSource() throws IOException{
    String datasourceName = "myDS";
    ModelNode request = new ModelNode();
    request.get(ClientConstants.OP).set("enable");//禁用的话,换成disable
    request.get(ClientConstants.OP_ADDR).add("subsystem","datasources");
    request.get(ClientConstants.OP_ADDR).add("data-source",datasourceName);

    ModelControllerClient client = ModelControllerClient.Factory.create(
            InetAddress.getByName("127.0.0.1"), 9999);
    client.execute(new OperationBuilder(request).build());
    client.close();
    System.out.println("数据源" + datasourceName + "启用完成!");
}

五、获取jboss已创建的jndi数据源列表

    /**
     * 获取jboss上已创建的所有数据源
     * @throws IOException
     */
    @Test
    public void getJbossDataSource() throws IOException {
        List<ModelNode> dsList = getDataSources();
        for (ModelNode n : dsList) {
            System.out.println(n.asString());
        }
    }

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

            return response.get(ClientConstants.RESULT).get("data-source").asList();
        } finally {
            safeClose(client);
        }
    }

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

github上还有一个开源项目jboss-controller-operation-executor,把一些常用操作做了封装,推荐大家也看一看。

  

  

  

上一篇:Teradata QueryGrid整合最佳分析技术 拓展客户选择空间


下一篇:Android Handler、Loop 的简单使用