gpt4 book ai didi

java - 我该如何修复 "Message in read-only mode"

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:24 25 4
gpt4 key购买 nike

我有 2 个项目,其中包含相同的“Chatter.java”文件。
我想在节目之间聊天。
但是只有其中一个可以向另一个发送消息。
这是它的样子 no description
Tim 可以写信给 Xerox,但如果 Xerox 尝试发送,他会得到一个

javax.jms.MessageNotWriteableException: [C4008]: Message in read-only mode.

我正在使用具有以下设置的 GlassFish 服务器: enter image description here enter image description here

所以,我的代码是

package aero;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Connection;
import javax.jms.Queue;
import javax.annotation.Resource;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Chatter extends JFrame implements Runnable{
@Resource(mappedName = "aeroPool")
private static ConnectionFactory connectionFactory;
@Resource(mappedName = "aeroJNDI")
private static Queue queue;

private JTextArea msgArea = new JTextArea();
private JTextField input = new JTextField("Message");
private JButton button = new JButton("Send");
private JPanel lowerPanel = new JPanel(new GridLayout(2, 1));

MessageConsumer consumer = null;
Message message = null;
MapMessage mapMessage = null;
Connection queueConnection = null;
Session session = null;
MessageProducer producer = null;

Thread th = null;

String nickname = "Xerox";

public Chatter(){
connect();
initGUI();
th = new Thread(this);
th.start();
}

public void initGUI(){
this.setLayout(new BorderLayout());
this.setTitle("Chat - " + nickname);
this.setSize(300, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(msgArea, BorderLayout.CENTER);
lowerPanel.add(input);
lowerPanel.add(button);
this.add(lowerPanel, BorderLayout.SOUTH);

button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try {
sendMessage();
} catch (JMSException ex) {
Logger.getLogger(Chatter.this.getName()).log(Level.SEVERE, null, ex);
}
}
});
}

public void connect(){
try {
queueConnection = connectionFactory.createConnection();
session = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(queue);
mapMessage = session.createMapMessage();
consumer = session.createConsumer(queue);
queueConnection.start();
} catch (JMSException ex) {
Logger.getLogger(Chatter.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void sendMessage() throws JMSException{
Date date = new Date(mapMessage.getJMSTimestamp());
String time = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
String textMessage = input.getText();
mapMessage.setString("Time", time);
mapMessage.setString("Nickname", nickname);
mapMessage.setString("Message", textMessage);
producer.send(mapMessage);
msgArea.append(System.lineSeparator() + nickname + " @ " + time + " - " + textMessage);
}

public void run(){
while(true){
try {
th.sleep(500);
getMessage();
} catch (InterruptedException ex) {
Logger.getLogger(this.getName()).log(Level.SEVERE, null, ex);
} catch (JMSException ex) {
Logger.getLogger(this.getName()).log(Level.SEVERE, null, ex);
}
}
}

public void getMessage() throws JMSException{
do{
message = consumer.receive(10000);
if(message != null){
if(message instanceof MapMessage){
mapMessage = (MapMessage) message;
if(!mapMessage.getString("Nickname").equals(nickname)){
String msg = mapMessage.getString("Nickname");
msg += " @ ";
msg += mapMessage.getString("Time");
msg += " - ";
msg += mapMessage.getString("Message");
msgArea.append(System.lineSeparator() + msg);
}
}
}
}while(message != null);
}

public static void main(String[] args) {
new Chatter().setVisible(true);
}
}

最佳答案

您的sendMessage() 方法使用了一个名为mapMessage 的字段。问题是 mapMessage 也由 getMessage() 写入。在大多数 JMS 实现中,无法修改接收到的消息。

您应该一起删除 mapMessage 字段,并在每个 sendMessage()getMessage() 中用局部变量替换它。每次要发送消息时,通过 session.createMapMessage() 创建一条新消息。同样,当您收到一条消息时,您应该阅读消息内容,然后将其丢弃。

关于java - 我该如何修复 "Message in read-only mode",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28619172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com