Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9266527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:20:24+00:00 2026-06-18T14:20:24+00:00

So here is my problem… big problem I have class called Startup which contains

  • 0

So here is my problem… big problem

I have class called Startup which contains my main method which calls client class, and Client class creates a window, ChatListener for listening to the messages

Now, I need to run the Startup two times(not only two more than two actually) and perform chat operation

My question is can I achieve this functionality using queue or should switch to topic

Other thing I have partially achieved it but the problem is when I send message I cant show it in the right receiver

Code is below

Startup

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

}

Chat Listener:

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>

Any help is greatly appreciated, Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:20:25+00:00Added an answer on June 18, 2026 at 2:20 pm

    The semantics of a queue is that every message is consumed once, and only once.

    I am not sure to understand what you are trying to do:

    • when you say that you need to call Startup several times, are you saying that you are effectively starting several clients on the same machine?)
    • you don’t describe the intended message flow: are you trying to broadcast every message to every connected client? Or are you trying to send messages from one client to the other? The answer to this question will allow you to choose between topics, respectively queues (and probably a combination of both, since most chat systems combine broadcast and unicast exchanges).
    • one thing that you could use are JMS selectors, allowing you to filter the messages consumed by a particular client (you could use a destination attribute or something like that).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem here is, i have a Modal PopUp Extender inside a User Control, which
I have a problem here im trying to upload a file first time it
I have a problem here. My Zend_Forms do not render in view script. Via
I have a problem here I can't solve. I have a database of houses
enter code here My problem is this: in this database the junction table contains
Here the problem. I have my site hosted on a shared hosting for asp.net
Small problem here I want to make a small fb app which show different
hi all i have implemented code as shown in the below here problem is
Here's my problem, say, for example, I have a link on my page, it
Here is my JSFIDDLE CLICK HERE My problem is that I Have classes that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.