title: Chat application using jms spring and activemq queue
body: <p>So here is my problem... big problem</p>
  
  <p>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</p>
  
  <p>Now, I need to run the Startup two times(not only two more than two actually) and perform chat operation </p>
  
  <p>My question is can I achieve this functionality using queue or should switch to topic</p>
  
  <p>Other thing I have partially achieved it but the problem is when I send message I cant show it in the right receiver</p>
  
  <p>Code is below</p>
  
  <p>Startup</p>
  
  <pre><code>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");
  }
  }
  </code></pre>
  
  <p>CClient</p>
  
  <pre><code>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);
     }
    }
  
  }
  </code></pre>
  
  <p>Chat Listener:</p>
  
  <pre><code>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;
  }
  }
  </code></pre>
  
  <p>SpringContext:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
  
  &lt;bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
      &lt;property name="brokerURL"&gt;
          &lt;!--&lt;value&gt;vm://localhost&lt;/value&gt;--&gt;
          &lt;value&gt;tcp://localhost:61616&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"&gt;
      &lt;constructor-arg value="jmsExample" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"&gt;
      &lt;property name="connectionFactory" ref="jmsConnectionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;util:constant id="constructorarg" static-field="com.communicator.Startup.ARGS"/&gt;
  
  &lt;bean id="simpleClient" class="com.communicator.CClient" init-method="init"&gt;
      &lt;constructor-arg&gt;&lt;value&gt;#{constructorarg}&lt;/value&gt;&lt;/constructor-arg&gt;
      &lt;property name="template" ref="jmsTemplate"/&gt;
      &lt;property name="destination" ref="destination" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="messageListener" class="com.communicator.ChatListener"&gt;
      &lt;property name="cClient" ref="simpleClient"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"&gt;
      &lt;property name="connectionFactory" ref="jmsConnectionFactory"/&gt;
      &lt;property name="destination" ref="destination"/&gt;
      &lt;property name="messageListener" ref="messageListener" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>Any help is greatly appreciated, Thank you</p>