java - 使用 jms spring 和 activemq 队列的聊天应用程序

标签 java spring queue jms activemq

这就是我的问题...大问题

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

CClient

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>

非常感谢任何帮助,谢谢

最佳答案

队列的语义是每条消息被消耗一次,并且仅被消耗一次

我不确定您要做什么:

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

关于java - 使用 jms spring 和 activemq 队列的聊天应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14655408/

相关文章:

java.lang.ClassNotFoundException : net. corda.testing.core.DummyCommandData

java - Spring MVC安全: Error creating bean with name 'springSecurityFilterChain'

c++ - cpp中如何根据数据类型将数据推送到不同的队列?

java - 从 Clojure 中使用静态方法生成类

java - 将泛型列表分配给具体的ArrayList会导致编译时错误

java - 在 Spring Boot 中使用模板/包含

java - Spring中如何判断请求是否是静态资源?

C++ 队列实现错误

redis - 如何创建分布式 'debounce' 任务来排空 Redis 列表?

java - 在 Google Play 商店发布应用程序后,当应用程序依赖于 Google 登录时,如何测试我的应用程序?