问题
I developed a simple JMS connection tester application to test connectivity of remote JMS queue. It is a jar file.
It works perfectly when executing locally on my pc connecting to JBoss instance running on same pc (i.e. localhost). But when I copy same jar file onto the windows test server (Windows Server 2008 R2 Standard) and run it from there, it gives following exception. The JBoss instance (jboss-eap-7.0) in this case is running on another Linux server.
Here is my code below. I have omitted some sensitive values from inclusion (those strings are CAPITALIZED). Moreover all the values are read dynamically from a properties file which I simplified here by hard-coding them directly in the code.
this is the code I run cmd to invoke the 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
I don't know why this works on my pc but not on the server. I don't think there are any firewall issues. Because I did connect to the queue on a previous occasion. Also there are no connectivity problems either. Pinging the Linux server where JBoss is running through cmd was a success. Please help.
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");
}
}
I have also added my pom file. What I do is I create an uber-jar of all dependencies so I could test on the server without many problems.
<?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>
回答1:
Given that you getting a java.lang.NullPointerException
that indicates to me a problem with your application. Maybe it's being triggered by the difference in your environment, but it looks to me like you're passing null
into the JNDI lookup for the queue.
回答2:
The problem is my maven had caching issues. sometimes it works alright and sometimes it doesn't. I removed my local repo and redownloaded all dependencies. Then it worked okay.
来源:https://stackoverflow.com/questions/65274510/jms-connection-not-connecting-to-remote-jboss-but-connects-local-instance