我正在尝试使用远程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类,用于说明问题。
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/izalerting/alertresult
的JMS主题的定义。
<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");
in,因为我不明白为什么找不到它。它应该由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$1.execute(Protocol.java:104)
at org.jboss.naming.remote.protocol.v1.RemoteNamingStoreV1.lookup(RemoteNamingStoreV1.java:95)
at org.jboss.naming.remote.client.HaRemoteNamingStore$1.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$1$3.read(Protocol.java:159)
at org.jboss.naming.remote.protocol.v1.Protocol$1$3.read(Protocol.java:149)
at org.jboss.naming.remote.protocol.v1.BaseProtocolCommand.readResult(BaseProtocolCommand.java:59)
at org.jboss.naming.remote.protocol.v1.Protocol$1.handleClientMessage(Protocol.java:149)
at org.jboss.naming.remote.protocol.v1.RemoteNamingStoreV1$MessageReceiver$1.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$1$3.read(Protocol.java:156)
... 7 more
问题得到解决。 ClassNotFoundException
实际上是由java.util.zip.ZipException: invalid LOC header (bad signature)
引起的。显然从maven存储库下载了一个坏jar。
通过删除.m2目录重新下载maven依赖项来解决该问题。