JMS 连接未连接到远程 JBoss 但连接到本地实例
JMS connection not connecting to remote JBoss but connects local instance
我开发了一个简单的 JMS 连接测试器应用程序来测试远程 JMS 队列的连接性。它是一个 jar 文件。
在我的电脑上本地执行连接到同一台电脑(即本地主机)上的 JBoss 实例 运行ning 时,它工作得很好。但是当我将相同的 jar 文件复制到 windows 测试服务器(Windows Server 2008 R2 Standard)并从那里 运行 时,它会出现以下异常。本例中的 JBoss 实例 (jboss-eap-7.0) 在另一台 Linux 服务器上 运行ning。
下面是我的代码。我从包含中省略了一些敏感值(那些字符串是大写的)。此外,所有值都是从属性文件中动态读取的,我在这里通过直接在代码中对它们进行硬编码来简化它们。
这是我 运行 cmd 调用 jar 的代码
java -Dcom.javtech.appia.javatoolkit.middleware.LogPath=./logs -Dcom.javtech.appia.javatoolkit.middleware.LogKeep=0 -Dlog4j.configuration=file:/E:/component/log.properties -cp .\component.jar com.gmt.helper.JMSTester
我不知道为什么这在我的电脑上有效,但在服务器上却无效。我认为没有任何防火墙问题。因为我之前确实连接过队列。也没有连接问题。通过 cmd ping Linux 服务器 JBoss 运行ning 是成功的。请帮忙。
public class JMSTester implements MessageListener, AutoCloseable {
private static final String JNDI_FACTORY = "org.wildfly.naming.client.WildFlyInitialContextFactory";
private static final String JMS_FACTORY = "jms/RemoteConnectionFactory";
private InitialContext context;
private QueueConnection queueConnection;
private QueueSession queueSession;
private QueueReceiver queueReceiver;
private QueueSender queueSender;
@SneakyThrows
public JMSTester(String queue) {
if (createSession() && createQueue(queue)) queueConnection.start();
}
public static void run(String fullQueueName) {
new Thread(() -> {
try {
JMSTester tester = new JMSTester(fullQueueName);
tester.post();
while (!tester.success) {
}
String queueName = tester.queueSender.getQueue().getQueueName();
tester.close();
System.out.printf("connection to %s is closed\n", queueName);
} catch (JMSException exception) {
exception.printStackTrace();
}
}).start();
}
@SneakyThrows
private boolean createSession() {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
// i don't want to include the ip address, user name, password here
env.put(Context.PROVIDER_URL, "http-remoting://IP_ADDRESS_OF_SERVER:8080");
env.put(Context.SECURITY_PRINCIPAL, "USER_NAME");
env.put(Context.SECURITY_CREDENTIALS, "PASSWORD");
context = new InitialContext(env);
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) context.lookup(JMS_FACTORY);
queueConnection = queueConnectionFactory.createQueueConnection("USER_NAME", "PASSWORD");
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
return true;
}
private boolean createQueue(String queueName) throws NamingException, JMSException {
Queue receiverQueue = (Queue) context.lookup(queueName);
queueReceiver = queueSession.createReceiver(receiverQueue);
queueReceiver.setMessageListener(this);
Queue senderQueue = (Queue) context.lookup(queueName);
queueSender = queueSession.createSender(senderQueue);
return true;
}
public static void main(String[] args) {
JMSTester.run("jms/queue/QUEUE_ONE");
JMSTester.run("jms/queue/QUEUE_TWO");
}
}
我还添加了我的 pom 文件。我所做的是创建一个包含所有依赖项的 uber-jar,这样我就可以在服务器上进行测试而不会出现很多问题。
<?xml version="1.0" encoding="UTF-8"?>
<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>com.gmt</groupId>
<artifactId>component</artifactId>
<packaging>jar</packaging>
<version>0.2.4-SNAPSHOT</version>
<name>BRIDGE_COMPONENT</name>
<properties>
<JDK_VERSION>1.8</JDK_VERSION>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>${JDK_VERSION}</maven.compiler.source>
<maven.compiler.target>${JDK_VERSION}</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>github.release.repo</id>
<name>Mulesoft</name>
<url>https://raw.github.com/bulldog2011/bulldog-repo/master/repo/releases/</url>
</repository>
<repository>
<id>Redhat-GA</id>
<url>https://maven.repository.redhat.com/ga/</url>
</repository>
</repositories>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.leansoft/bigqueue -->
<dependency>
<groupId>com.leansoft</groupId>
<artifactId>bigqueue</artifactId>
<version>0.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.eap/wildfly-jms-client-bom -->
<dependency>
<groupId>org.jboss.eap</groupId>
<artifactId>wildfly-jms-client-bom</artifactId>
<version>7.3.3.GA</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.javatech</groupId>
<artifactId>appia</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>${name}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${JDK_VERSION}</source>
<target>${JDK_VERSION}</target>
<excludes>
<exclude>**/other/*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.gmt.component.Component</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
鉴于您收到 java.lang.NullPointerException
表明您的应用程序存在问题。也许它是由您的环境差异触发的,但在我看来您正在将 null
传递到队列的 JNDI 查找中。
问题是我的 maven 有缓存问题。有时它工作正常,有时它不工作。我删除了本地存储库并重新下载了所有依赖项。然后就正常了。
我开发了一个简单的 JMS 连接测试器应用程序来测试远程 JMS 队列的连接性。它是一个 jar 文件。
在我的电脑上本地执行连接到同一台电脑(即本地主机)上的 JBoss 实例 运行ning 时,它工作得很好。但是当我将相同的 jar 文件复制到 windows 测试服务器(Windows Server 2008 R2 Standard)并从那里 运行 时,它会出现以下异常。本例中的 JBoss 实例 (jboss-eap-7.0) 在另一台 Linux 服务器上 运行ning。
下面是我的代码。我从包含中省略了一些敏感值(那些字符串是大写的)。此外,所有值都是从属性文件中动态读取的,我在这里通过直接在代码中对它们进行硬编码来简化它们。
这是我 运行 cmd 调用 jar 的代码
java -Dcom.javtech.appia.javatoolkit.middleware.LogPath=./logs -Dcom.javtech.appia.javatoolkit.middleware.LogKeep=0 -Dlog4j.configuration=file:/E:/component/log.properties -cp .\component.jar com.gmt.helper.JMSTester
我不知道为什么这在我的电脑上有效,但在服务器上却无效。我认为没有任何防火墙问题。因为我之前确实连接过队列。也没有连接问题。通过 cmd ping Linux 服务器 JBoss 运行ning 是成功的。请帮忙。
public class JMSTester implements MessageListener, AutoCloseable {
private static final String JNDI_FACTORY = "org.wildfly.naming.client.WildFlyInitialContextFactory";
private static final String JMS_FACTORY = "jms/RemoteConnectionFactory";
private InitialContext context;
private QueueConnection queueConnection;
private QueueSession queueSession;
private QueueReceiver queueReceiver;
private QueueSender queueSender;
@SneakyThrows
public JMSTester(String queue) {
if (createSession() && createQueue(queue)) queueConnection.start();
}
public static void run(String fullQueueName) {
new Thread(() -> {
try {
JMSTester tester = new JMSTester(fullQueueName);
tester.post();
while (!tester.success) {
}
String queueName = tester.queueSender.getQueue().getQueueName();
tester.close();
System.out.printf("connection to %s is closed\n", queueName);
} catch (JMSException exception) {
exception.printStackTrace();
}
}).start();
}
@SneakyThrows
private boolean createSession() {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
// i don't want to include the ip address, user name, password here
env.put(Context.PROVIDER_URL, "http-remoting://IP_ADDRESS_OF_SERVER:8080");
env.put(Context.SECURITY_PRINCIPAL, "USER_NAME");
env.put(Context.SECURITY_CREDENTIALS, "PASSWORD");
context = new InitialContext(env);
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) context.lookup(JMS_FACTORY);
queueConnection = queueConnectionFactory.createQueueConnection("USER_NAME", "PASSWORD");
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
return true;
}
private boolean createQueue(String queueName) throws NamingException, JMSException {
Queue receiverQueue = (Queue) context.lookup(queueName);
queueReceiver = queueSession.createReceiver(receiverQueue);
queueReceiver.setMessageListener(this);
Queue senderQueue = (Queue) context.lookup(queueName);
queueSender = queueSession.createSender(senderQueue);
return true;
}
public static void main(String[] args) {
JMSTester.run("jms/queue/QUEUE_ONE");
JMSTester.run("jms/queue/QUEUE_TWO");
}
}
我还添加了我的 pom 文件。我所做的是创建一个包含所有依赖项的 uber-jar,这样我就可以在服务器上进行测试而不会出现很多问题。
<?xml version="1.0" encoding="UTF-8"?>
<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>com.gmt</groupId>
<artifactId>component</artifactId>
<packaging>jar</packaging>
<version>0.2.4-SNAPSHOT</version>
<name>BRIDGE_COMPONENT</name>
<properties>
<JDK_VERSION>1.8</JDK_VERSION>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>${JDK_VERSION}</maven.compiler.source>
<maven.compiler.target>${JDK_VERSION}</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>github.release.repo</id>
<name>Mulesoft</name>
<url>https://raw.github.com/bulldog2011/bulldog-repo/master/repo/releases/</url>
</repository>
<repository>
<id>Redhat-GA</id>
<url>https://maven.repository.redhat.com/ga/</url>
</repository>
</repositories>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.leansoft/bigqueue -->
<dependency>
<groupId>com.leansoft</groupId>
<artifactId>bigqueue</artifactId>
<version>0.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.eap/wildfly-jms-client-bom -->
<dependency>
<groupId>org.jboss.eap</groupId>
<artifactId>wildfly-jms-client-bom</artifactId>
<version>7.3.3.GA</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.javatech</groupId>
<artifactId>appia</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>${name}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${JDK_VERSION}</source>
<target>${JDK_VERSION}</target>
<excludes>
<exclude>**/other/*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.gmt.component.Component</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
鉴于您收到 java.lang.NullPointerException
表明您的应用程序存在问题。也许它是由您的环境差异触发的,但在我看来您正在将 null
传递到队列的 JNDI 查找中。
问题是我的 maven 有缓存问题。有时它工作正常,有时它不工作。我删除了本地存储库并重新下载了所有依赖项。然后就正常了。