有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JMS连接未连接到远程JBoss,但连接本地实例

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

当在我的pc上本地执行并连接到在同一台pc上运行的JBoss实例(即localhost)时,它可以完美地工作。但是,当我将同一个jar文件复制到windows测试服务器(windows server 2008 R2标准)并从那里运行它时,会出现以下异常。本例中的JBoss实例(JBoss-eap-7.0)正在另一台Linux服务器上运行

Error message

下面是我的代码。我从包含中省略了一些敏感值(这些字符串是大写的)。此外,所有值都是从属性文件中动态读取的,我在这里通过直接在代码中硬编码简化了属性文件

这是我运行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

我不知道为什么这在我的pc上有效,但在服务器上无效。我认为没有任何防火墙问题。因为我在前一次连接到队列。此外,也没有连接问题。通过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>

共 (2) 个答案

  1. # 1 楼答案

    问题是我的maven有缓存问题。有时可以正常工作,有时不行。我删除了本地回购协议,并重新下载了所有依赖项。然后就好了

  2. # 2 楼答案

    考虑到你得到的java.lang.NullPointerException表明你的应用程序有问题。也许它是由您环境中的差异触发的,但在我看来,您正在将null传递到队列的JNDI查找中