【问题标题】:Chat application using jms spring and activemq queue使用 jms spring 和 activemq 队列的聊天应用程序
【发布时间】:2013-01-17 07:16:29
【问题描述】:

所以这是我的问题...大问题

我有一个名为 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;
}
}

SpringContext:

<?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>

非常感谢您的帮助,谢谢

【问题讨论】:

  • 发布/订阅拓扑通常是建模聊天网络的方式。您可能也可以使用队列,但是您需要以某种方式为不同的客户端动态创建不同的队列。

标签: java spring queue jms activemq


【解决方案1】:

队列的语义是每条消息被消费一次,而且只被消费一次

我不确定你要做什么:

  • 当您说需要多次调用 Startup 时,您是说您实际上是在同一台机器上启动多个客户端?)
  • 您没有描述预期的消息流:您是否尝试将每条消息广播到每个连接的客户端?或者您是否尝试从一个客户端向另一个客户端发送消息?这个问题的答案将允许您在主题和队列之间进行选择(也可能是两者的组合,因为大多数聊天系统都结合了广播和单播交换)。
  • 可以使用的一件事是 JMS 选择器,允许您过滤特定客户端使用的消息(您可以使用 destination 属性或类似的东西) .

【讨论】:

  • 我已经用提到的第三个项目符号解决了这个问题,谢谢
猜你喜欢
  • 2016-04-29
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
  • 2015-02-19
  • 2018-07-16
  • 2021-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多