java.net.MalformedURLException:不支持的协议:remoting-jmx

时间:2015-12-14 11:08:06

标签: jboss7.x jmx jboss-eap-6

我想在JBoss 6.4.0 EAP中使用Java客户端。我测试了这段代码:

public void testConnection() throws Exception
    {

        System.out.println("Starting lookup ...");

        ObjectName mBeanName = new ObjectName("java.lang:type=Runtime");
        String attributeName = "StartTime";

        String host = "104.233.103.41";
        int port = 9999;  // management-native port

        String urlString = System.getProperty("jmx.service.url", "service:jmx:remoting-jmx://" + host + ":" + port);
        JMXServiceURL serviceURL = new JMXServiceURL(urlString);
        JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceURL, null);
        MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();

        Object attrVal = connection.getAttribute(mBeanName, attributeName);
        System.out.println("Value via JMX: " + new Date((Long) attrVal));

    }

我的项目是基于Maven的,所以我添加了这些依赖项:

   <dependencies>
        <dependency>
            <groupId>org.jboss.as</groupId>
            <artifactId>jboss-as-ejb-client-bom</artifactId>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.jboss.as</groupId>
            <artifactId>jboss-as-jms-client-bom</artifactId>
            <type>pom</type>
        </dependency>
    </dependencies>

但是当我运行代码时出现此错误:

java.net.MalformedURLException: Unsupported protocol: remoting-jmx
    at javax.management.remote.JMXConnectorFactory.newJMXConnector(JMXConnectorFactory.java:359)
    at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:269)

进入EAP-6.4.0 / bin / client我发现了这个描述:

jboss-client.jar是JBoss EAP 6的组合客户端jar,用于非maven环境。应该使用这个罐子 仅部署到客户端,而不是部署到JBoss EAP 6实例的部署。

此jar包含远程JMS和EJB使用所需的类,并包含以下着色工件:

org.jboss.spec.javax.jms:jboss-jms-api_1.1_spec
org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec
org.jboss.spec.javax.ejb:jboss-ejb-api_3.1_spec

org.jboss:jboss-ejb-client
org.jboss:jboss-remote-naming
org.jboss.logging:jboss-logging
org.jboss.marshalling:jboss-marshalling
org.jboss.marshalling:jboss-marshalling-river
org.jboss.remoting3:jboss-remoting
org.jboss.remoting3:remoting-jmx
org.jboss.sasl:jboss-sasl
org.jboss.xnio:xnio-api
org.jboss.xnio:xnio-nio
org.jboss.netty:netty
org.hornetq:hornetq-core-client
org.hornetq:hornetq-jms-client

Maven用户不应该使用此jar,而应使用以下BOM依赖项

<dependencies>
    <dependency>
        <groupId>org.jboss.as</groupId>
        <artifactId>jboss-as-ejb-client-bom</artifactId>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.jboss.as</groupId>
        <artifactId>jboss-as-jms-client-bom</artifactId>
        <type>pom</type>
    </dependency>
</dependencies>

这是因为使用带有阴影jar的maven很有可能导致类版本冲突,这就是原因所在 我们不会将此jar发布到maven存储库。

但我又得到了这个错误。你能提出一些解决方案吗?

1 个答案:

答案 0 :(得分:5)

为remoting-jmx协议添加依赖项,即org.jboss.remoting3:remoting-jmxhttps://developer.jboss.org/thread/199914?tstart=0

<dependency>
    <groupId>org.jboss.remoting3</groupId>
    <artifactId>remoting-jmx</artifactId>
    <version>1.0.1.Final</version>
</dependency>

完整且有效的POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jboss.as</groupId>
            <artifactId>jboss-as-ejb-client-bom</artifactId>
            <type>pom</type>
            <version>7.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.as</groupId>
            <artifactId>jboss-as-jms-client-bom</artifactId>
            <type>pom</type>
            <version>7.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.remoting3</groupId>
            <artifactId>remoting-jmx</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

使用在本地主机上运行的JBoss服务器运行maven构建:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running test.ATest
Starting lookup ...
Dec 16, 2015 6:25:50 PM org.xnio.Xnio <clinit>
INFO: XNIO Version 3.0.7.GA
Dec 16, 2015 6:25:50 PM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.0.7.GA
Dec 16, 2015 6:25:50 PM org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 3.2.14.GA
Value via JMX: Wed Dec 16 18:14:05 CST 2015
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.89 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0