所以这是我的问题......大问题
我有一个名为 Startup 的类,其中包含调用客户端类的主要方法,并且客户端类创建一个窗口 ChatListener 用于收听消息
现在,我需要运行两次启动(实际上不止两次)并执行聊天操作
我的问题是我可以使用队列实现此功能还是应该切换到主题
其他事情我已经部分实现了,但问题是当我发送消息时,我无法在正确的接收器中显示它
代码如下
启动
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
import java.sql.SQLException;
public class Startup {
static Long id = (long) 0;
public static String[] ARGS;
public static void main(String[] args) throws IOException, InterruptedException, SQLException {
Startup s = new Startup();
s.ARGS = args;
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("SpringContext.xml");
CClient client = (CClient) context.getBean("simpleClient");
}
}
客户端
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CClient extends JFrame implements ActionListener {
String identifier;
public CClient(String identifier) {
this.identifier = identifier;
}
public JTextArea taDisplay;
public void setTfInput(JTextField tfInput) {
this.tfInput = tfInput;
}
private JTextField tfInput;
private String msg;
public void setTemplate(JmsTemplate template) {
this.template = template;
}
public void setDestination(Destination destination) {
this.destination = destination;
}
public JmsTemplate template;
public Destination destination;
public void init() {
setLayout(new FlowLayout());
add(new JLabel("Enter Text: "));
tfInput = new JTextField(10);
add(tfInput);
JButton jSend = new JButton("Send");
add(jSend);
taDisplay = new JTextArea(6, 30);
JScrollPane scrollPane = new JScrollPane(taDisplay);
add(scrollPane);
jSend.addActionListener(this);
setTitle("Communicator " + identifier);
setSize(400, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
try {
if ("Send".equals(e.getActionCommand())) {
msg = tfInput.getText();
template.send(destination, new MessageCreator() {
public Message createMessage(Session session)
throws JMSException {
Message message = session.createTextMessage(msg);
message.setStringProperty("stringProperty", identifier);
return message;
}
});
}
} catch (Exception e1) {
e1.printStackTrace();
System.out.println("Error: " + e1);
}
}
}
聊天监听器:
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.regex.Pattern;
public class ChatListener implements MessageListener {
public CClient cClient;
public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
System.out.print("hai");
System.out.println("Received Message is " + ((TextMessage) message).getText());
String[] parts = Pattern.compile(":", Pattern.LITERAL).split(((TextMessage) message).getText());
System.out.println(parts[0]);
String frmWho = message.getStringProperty("stringProperty");
System.out.println("From Who " + frmWho);
cClient.taDisplay.append(((TextMessage) message).getText() + "\n");
} catch (JMSException ex) {
throw new RuntimeException(ex);
}
}
}
public void setcClient(CClient cClient) {
this.cClient = cClient;
}
}
弹簧上下文:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<!--<value>vm://localhost</value>-->
<value>tcp://localhost:61616</value>
</property>
</bean>
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="jmsExample" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory" />
</bean>
<util:constant id="constructorarg" static-field="com.communicator.Startup.ARGS"/>
<bean id="simpleClient" class="com.communicator.CClient" init-method="init">
<constructor-arg><value>#{constructorarg}</value></constructor-arg>
<property name="template" ref="jmsTemplate"/>
<property name="destination" ref="destination" />
</bean>
<bean id="messageListener" class="com.communicator.ChatListener">
<property name="cClient" ref="simpleClient"></property>
</bean>
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory"/>
<property name="destination" ref="destination"/>
<property name="messageListener" ref="messageListener" />
</bean>
非常感谢任何帮助,谢谢