我该如何修复 "Message in read-only mode"
How can I fix "Message in read-only mode"
我有 2 个项目具有相同的 'Chatter.java' 文件。
我想在节目之间聊天。
但是只有其中一个可以向另一个发送消息。
这是它的样子
Tim 可以写信给 Xerox,但如果 Xerox 尝试发送,他会收到
javax.jms.MessageNotWriteableException: [C4008]: Message in read-only mode.
我正在使用 GlassFish 服务器,设置如下:
所以,我的代码是
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()
创建一条新消息。同样,当您收到一条消息时,您应该阅读消息内容,然后将其丢弃。
我有 2 个项目具有相同的 'Chatter.java' 文件。
我想在节目之间聊天。
但是只有其中一个可以向另一个发送消息。
这是它的样子
Tim 可以写信给 Xerox,但如果 Xerox 尝试发送,他会收到
javax.jms.MessageNotWriteableException: [C4008]: Message in read-only mode.
我正在使用 GlassFish 服务器,设置如下:
所以,我的代码是
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()
创建一条新消息。同样,当您收到一条消息时,您应该阅读消息内容,然后将其丢弃。