如何在只读模式下修复"消息"

时间:2015-02-19 23:40:51

标签: java glassfish jms

我有2个项目,其中包含相同的'Chatter.java'文件 我想聊聊节目。
但只有其中一人可以向其他人发送信息 它的外观如下no description
蒂姆可以写信给施乐,但如果施乐试图发送他就得到了

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);
    }
}

1 个答案:

答案 0 :(得分:2)

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

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