我开发了一个简单的jms连接测试程序来测试远程jms队列的连接。它是一个jar文件。
当在我的pc上连接到运行在同一台pc上的jboss示例(即localhost)本地执行时,它工作得非常好。但是当我将同一个jar文件复制到windows测试服务器(windowsserver2008r2标准)并从那里运行它时,它会给出以下异常。本例中的jboss示例(jboss-eap-7.0)正在另一台linux服务器上运行。
下面是我的代码。我从包含中省略了一些敏感值(那些字符串是大写的)。此外,所有的值都是从一个属性文件中动态读取的,我在这里通过直接在代码中硬编码简化了这个属性文件。
这是我运行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
我不知道为什么这在我的电脑上,但在服务器上不工作。我不认为有任何防火墙问题。因为我以前确实连接到队列。此外,也没有连接问题。ping jboss通过cmd运行的linux服务器是成功的。请帮忙。
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文件。我要做的是创建一个包含所有依赖项的uberjar,这样就可以在服务器上进行测试,而不会出现很多问题。
<?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>
2条答案
按热度按时间qlzsbp2j1#
考虑到你得到了
java.lang.NullPointerException
这说明你的申请有问题。也许是因为你所处环境的不同而引发的,但在我看来,你只是路过而已null
进入队列的jndi查找。mwg9r5ms2#
问题是我的maven有缓存问题。有时可以,有时不行。我删除了本地回购并重新下载了所有依赖项。然后就好了。