【问题标题】:JMS connection not connecting to remote JBoss but connects local instanceJMS 连接未连接到远程 JBoss 但连接本地实例
【发布时间】:2021-03-24 05:40:09
【问题描述】:

我开发了一个简单的 JMS 连接测试器应用程序来测试远程 JMS 队列的连接性。这是一个jar文件。

在我的电脑上本地执行连接到在同一台电脑上运行的 JBoss 实例(即本地主机)时,它可以完美运行。但是当我将相同的 jar 文件复制到 Windows 测试服务器(Windows Server 2008 R2 Standard)并从那里运行它时,它会给出以下异常。本例中的 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

我不知道为什么这在我的电脑上有效,但在服务器上无效。我不认为有任何防火墙问题。因为我之前确实连接到了队列。也没有连接问题。通过 cmd ping JBoss 正在运行的 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 文件。我所做的是创建一个包含所有依赖项的 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 jms jboss-eap-7


    【解决方案1】:

    鉴于您收到的 java.lang.NullPointerException 向我表明您的应用程序存在问题。也许它是由您的环境差异触发的,但在我看来,您将 null 传递到队列的 JNDI 查找中。

    【讨论】:

    • 但是你可以看到我的代码,它在任何地方都没有空参数。我发现问题出在这里:QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) context.lookup(JMS_FACTORY);,它说上下文为空。但事实并非如此。我该如何解决这个问题。
    • 也检查这个。我认为这些是相关的:stackoverflow.com/questions/65293636/…
    • 实际上,我无法判断代码中是否没有 null 参数,因为查找中使用的队列名称最终是从某处传入别的。它可能null。您粘贴的内容中没有任何内容可以证明并非如此。此外,根据堆栈跟踪,NullPointerException 是从 createQueue() 而不是 createSession() 抛出的,其中包含您引用的行。
    【解决方案2】:

    问题是我的 Maven 有缓存问题。有时它可以正常工作,有时却不行。我删除了我的本地仓库并重新下载了所有依赖项。然后就正常了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      相关资源
      最近更新 更多