簡體   English   中英

JMS 連接未連接到遠程 JBoss 但連接本地實例

[英]JMS connection not connecting to remote JBoss but connects local instance

我開發了一個簡單的 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

我不知道為什么這在我的電腦上有效,但在服務器上無效。 我不認為有任何防火牆問題。 因為我之前確實連接到了隊列。 也沒有連接問題。 ping Linux 服務器,其中 JBoss 通過 cmd 運行是成功的。 請幫忙。

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 有緩存問題。 有時它可以正常工作,有時卻不行。 我刪除了我的本地倉庫並重新下載了所有依賴項。 然后它工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM