ClassNotFoundException org.hornetq.jms.client.HornetQTopic

ClassNotFoundException org.hornetq.jms.client.HornetQTopic

我正在尝试通过远程 wildfly 9 从 Java 客户端使用 http-remoting。这总是导致 ClassNotFoundException。

这是我的pom.xml

<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>iz.alerting</groupId>
    <artifactId>AlertingClientTest</artifactId>
    <version>1.0.6-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.wildfly</groupId>
                <artifactId>wildfly-jms-client-bom</artifactId>
                <version>9.0.2.Final</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.spec.javax.jms</groupId>
            <artifactId>jboss-jms-api_2.0_spec</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hornetq</groupId>
            <artifactId>hornetq-jms-client</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss</groupId>
            <artifactId>jboss-remote-naming</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.xnio</groupId>
            <artifactId>xnio-nio</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

这是一个非常简单的 Java Main class 说明了问题。

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Main {

    public static void main(String[] args) {
        Logger log = Logger.getLogger("");
        try {
            Class.forName("org.hornetq.jms.client.HornetQTopic");
        } catch (ClassNotFoundException e1) {
            log.log(Level.SEVERE,"Could not find org.hornetq.jms.client.HornetQTopic",e1);
        }

        Properties env= new Properties();
        String url="http-remoting://servername:12080";
        log.log(Level.INFO,"Connecting to server "+url);
        env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        env.put(Context.PROVIDER_URL, url);
        env.put(Context.SECURITY_PRINCIPAL, "xxx");
        env.put(Context.SECURITY_CREDENTIALS, "xxx");
        InitialContext remoteContext=null;
        try {
            remoteContext = new InitialContext(env);
        } catch (NamingException e) {
            log.log(Level.SEVERE,"Could not create initialcontext", e);
        }
        Topic topic=null;
        String eventname = "jms/izalerting/alertresult";
        if(remoteContext!=null){
            try {
                topic = (Topic) remoteContext.lookup(eventname);
                if(topic == null) {
                    log.log(Level.SEVERE,"Could not find topic" + eventname+ ". It was null");
                }
            } catch (NamingException e) {
                log.log(Level.SEVERE,"Could not find topic" + eventname, e);
            }finally {
                try {
                    remoteContext.close();
                } catch (NamingException e) {
                    log.log(Level.SEVERE,"Could not close remoteContext", e);
                }
            }
        }
    }
}

在我的 Wildfly standalone.xml 中,我有关于 JMS 主题的定义 jms/izalerting/alertresult

<jms-destinations>
    <jms-topic name="izalertAlertresult">
        <entry name="jms/izalerting/alertresult"/>
        <entry name="java:jboss/exported/jms/izalerting/alertresult"/>
    </jms-topic>
</jms-destinations>

此程序总是在行 topic = (Topic) remoteContext.lookup(eventname); 上产生 ClassNotFoundException。下面我添加了程序的完整输出。

我在程序的开头添加了 Class.forName("org.hornetq.jms.client.HornetQTopic"); 因为我不明白为什么找不到它。它应该由 Maven 依赖项 hornetq-jms-client.

提供

有人知道如何解决这个问题吗?我需要额外的 Maven 依赖项吗?

我相当确定与服务器的连接成功,因为当我删除主体和凭据行时,它说它无法进行身份验证。

mrt 20, 2019 1:32:24 PM java.util.logging.LogManager$RootLogger log
SEVERE: Could not find org.hornetq.jms.client.HornetQTopic
java.lang.ClassNotFoundException: org.hornetq.jms.client.HornetQTopic
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at Main.main(Main.java:15)

mrt 20, 2019 1:32:24 PM java.util.logging.LogManager$RootLogger log
INFO: Connecting to server http-remoting://servername:12080
mrt 20, 2019 1:32:24 PM org.xnio.Xnio <clinit>
INFO: XNIO version 3.3.1.Final
mrt 20, 2019 1:32:25 PM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.3.1.Final
mrt 20, 2019 1:32:25 PM org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 4.0.9.Final
mrt 20, 2019 1:32:26 PM java.util.logging.LogManager$RootLogger log
SEVERE: Could not find topicjms/izalerting/alertresult
org.jboss.naming.remote.protocol.NamingIOException: Failed to lookup [Root exception is java.io.IOException: java.lang.ClassNotFoundException: org.hornetq.jms.client.HornetQTopic]
    at org.jboss.naming.remote.client.ClientUtil.namingException(ClientUtil.java:49)
    at org.jboss.naming.remote.protocol.v1.Protocol.execute(Protocol.java:104)
    at org.jboss.naming.remote.protocol.v1.RemoteNamingStoreV1.lookup(RemoteNamingStoreV1.java:95)
    at org.jboss.naming.remote.client.HaRemoteNamingStore.operation(HaRemoteNamingStore.java:276)
    at org.jboss.naming.remote.client.HaRemoteNamingStore.namingOperation(HaRemoteNamingStore.java:137)
    at org.jboss.naming.remote.client.HaRemoteNamingStore.lookup(HaRemoteNamingStore.java:272)
    at org.jboss.naming.remote.client.RemoteContext.lookupInternal(RemoteContext.java:104)
    at org.jboss.naming.remote.client.RemoteContext.lookup(RemoteContext.java:93)
    at org.jboss.naming.remote.client.RemoteContext.lookup(RemoteContext.java:146)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at Main.main(Main.java:43)
Caused by: java.io.IOException: java.lang.ClassNotFoundException: org.hornetq.jms.client.HornetQTopic
    at org.jboss.naming.remote.protocol.v1.Protocol.read(Protocol.java:159)
    at org.jboss.naming.remote.protocol.v1.Protocol.read(Protocol.java:149)
    at org.jboss.naming.remote.protocol.v1.BaseProtocolCommand.readResult(BaseProtocolCommand.java:59)
    at org.jboss.naming.remote.protocol.v1.Protocol.handleClientMessage(Protocol.java:149)
    at org.jboss.naming.remote.protocol.v1.RemoteNamingStoreV1$MessageReceiver.run(RemoteNamingStoreV1.java:232)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: org.hornetq.jms.client.HornetQTopic
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.jboss.marshalling.AbstractClassResolver.loadClass(AbstractClassResolver.java:131)
    at org.jboss.marshalling.AbstractClassResolver.resolveClass(AbstractClassResolver.java:112)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadClassDescriptor(RiverUnmarshaller.java:948)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadNewObject(RiverUnmarshaller.java:1255)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:276)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:209)
    at org.jboss.marshalling.AbstractObjectInput.readObject(AbstractObjectInput.java:41)
    at org.jboss.naming.remote.protocol.v1.Protocol.read(Protocol.java:156)
    ... 7 more

问题已解决。 ClassNotFoundException 实际上是由 java.util.zip.ZipException: invalid LOC header (bad signature) 引起的。显然是从 maven 存储库下载了一个错误的 jar。

问题已通过删除 .m2 目录并重新下载 maven 依赖关系得到解决。