为什么 JMS 的 "createTextMessage" 会抛出带有字符串变量参数的异常,而不是在乱码字符串上?

Why does JMS' "createTextMessage" throw Exception with a string variable parameter but not on a litteral string?

我目前正在使用 JMS 创建一个聊天演示应用程序,在创建消息发布器时我遇到了一个奇怪的错误。这是我的代码:

public class MessageEmission implements Runnable {

    private String queueName;
    private String message;

    public MessageEmission(String queueName, String message) {
        queueName = this.queueName;
        message = this.message;
    }

    public void run() {
        try {
            Connection connection = CreateConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            TextMessage message = session.createTextMessage(message);
            Destination destination = session.createQueue(queueName);
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            producer.send(message);

            session.close();
            connection.close();
        }
        catch (Exception e) {
            System.out.println("Caught: " + e);
            e.printStackTrace();
        }
    }

    private Connection CreateConnection() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(Configuration.getActiveMqServerHost());
        Connection connection = null;

        try
        {
            connection = connectionFactory.createConnection();
            connection.start();
        }
        catch(Exception e)
        {
            System.out.println("Caught: " + e);
            e.printStackTrace();
        }

        return connection;
    }
}

我在 VS Code 中高亮显示的错误在这里结束

TextMessage message = session.createTextMessage(message);

错误如下:

The method createTextMessage(String) in the type Session is not applicable for the arguments

每当我用像这样的乱码字符串替换参数消息时,错误就会消失。

TextMessage message = session.createTextMessage("This is fine! :fire:");

如果我使用消息字符串变量作为函数的参数,我没有得到编译错误,但是当我 运行 它时应用程序崩溃了......我不确定我在做什么错了,谁能解释一下这是怎么回事?

在我的主应用程序 class 中,我将以下内容用于 运行 Runnable:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) throws Exception {
        
        thread(new MessageEmission("MESSAGE", "This"), false);
        thread(new MessageEmission("MESSAGE", "is"), false);
        thread(new MessageEmission("MESSAGE", "a"), false);
        thread(new MessageEmission("MESSAGE", "test"), false);
        thread(new MessageEmission("MESSAGE", "and"), false);
        thread(new MessageEmission("MESSAGE", "it"), false);
        thread(new MessageEmission("MESSAGE", "is"), false);
        thread(new MessageEmission("MESSAGE", "working"), false);
        thread(new MessageEmission("MESSAGE", "as"), false);
        thread(new MessageEmission("MESSAGE", "intended"), false);
        
        //SpringApplication.run(DemoApplication.class, args);
    }

    public static void thread(Runnable runnable, boolean daemon) {
        Thread brokerThread = new Thread(runnable);
        brokerThread.setDaemon(daemon);
        brokerThread.start();
    }
}

您在构造函数中的赋值顺序错误:

public MessageEmission(String queueName, String message) {
    this.queueName = queueName;   // assign the value of argument to field variable
    this.message = message;
}

您可能还想重命名 TextMessage 变量以避免与字段 message:

发生冲突
TextMessage textMessage = session.createTextMessage(message);  // here, message is equivalent to this.message

我还注意到您使用了 ConnectionSession 接口,它们都实现了 AutoCloseable,您也可以在 try-with-resources 语句中使用该对象:

    // this ensures connection and session are closed at the end of the try block
    try (Connection connection = createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) {
        
        TextMessage message = session.createTextMessage(message);
        Destination destination = session.createQueue(queueName);
        MessageProducer producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        producer.send(message);
    }
    catch (Exception e) {
        System.out.println("Caught: " + e);
        e.printStackTrace();
    }