3 Replies Latest reply on Sep 9, 2008 9:15 AM by mark spencer

    Best architecture practice for jboss messaging.

    Adrien Adrien Apprentice

      Hi

      I want to write a chat but not a chat with a topic but point to point between users : (un = 1 user)
      u1<--1:1-->u2
      u3<--1:1-->u2
      u3<--1:1-->u1

      Each user can connect on the chat at anytime

      I'm newbie on JBossMessaging.

      Actually my orientation choices (certainly not all good...)are :
      -IHM client : applet
      -use QUEUE because this is a point to point, a conversation between 2 users
      -For 1 converstation between 2 users i need 2 QUEUEs : each user can send or receive message
      -Each user can connect / disconnect himself at any time : for this it is need to create a queue when a user want to be connected and destroy the queue when the client disctonnect it'self : for this each applet can will create/destroy new queue on the server via JMX on jboss.messaging:service=ServerPeer

      If some experimented people can give me it s remarques..

      Thanks a log.


        • 1. Re: Best architecture practice for jboss messaging.
          Tim Fox Master

          I would use a topic.

          Each user can create a subscription when they join.

          If you want users to see chat messages sent while they were not connected, they can use a durable subscription.

          • 3. Re: Best architecture practice for jboss messaging.
            mark spencer Newbie

            This is a barebone JMS client sampe.

            import java.util.Properties;
            
            import javax.jms.JMSException;
            import javax.jms.Message;
            import javax.jms.MessageListener;
            import javax.jms.Topic;
            import javax.jms.TopicConnection;
            import javax.jms.TopicConnectionFactory;
            import javax.jms.TopicPublisher;
            import javax.jms.TopicSubscriber;
            import javax.jms.TopicSession;
            import javax.jms.TextMessage;
            import javax.naming.InitialContext;
            import javax.naming.NamingException;
            
            import EDU.oswego.cs.dl.util.concurrent.CountDown;
            
            public class ChatClient
            {
            static CountDown done = new CountDown(1);
            TopicConnection conn = null;
            TopicSession session = null;
            Topic topic = null;
            
            public static class ExListener implements MessageListener
            {
            public void onMessage(Message msg)
            {
            done.release();
            TextMessage tm = (TextMessage) msg;
            try {
            System.out.println("onMessage, recv text=" + tm.getText());
            } catch(Throwable t) {
            t.printStackTrace();
            }
            }
            }
            
            public void setupPubSub()
            throws JMSException, NamingException
            {
            Properties props = new Properties();
            props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
            props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
            props.setProperty("java.naming.provider.url", "jnp://localhost:1099");
            
            InitialContext iniCtx = new InitialContext(props);
            
            Object tmp = iniCtx.lookup("ConnectionFactory");
            TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
            conn = tcf.createTopicConnection();
            topic = (Topic) iniCtx.lookup("topic/testTopic");
            session = conn.createTopicSession(false,
            TopicSession.AUTO_ACKNOWLEDGE);
            conn.start();
            }
            
            public void sendRecvAsync(String text)
            throws JMSException, NamingException
            {
            System.out.println("Begin sendRecvAsync");
            // Setup the PubSub connection, session
            setupPubSub();
            // Set the async listener
            
            TopicSubscriber recv = session.createSubscriber(topic);
            recv.setMessageListener(new ExListener());
            // Send a text msg
            TopicPublisher send = session.createPublisher(topic);
            TextMessage tm = session.createTextMessage(text);
            send.publish(tm);
            System.out.println("sendRecvAsync, sent text=" + tm.getText());
            send.close();
            System.out.println("End sendRecvAsync");
            }
            
            public void stop() throws JMSException
            {
            conn.stop();
            session.close();
            conn.close();
            }
            
            public static void main(String args[]) throws Exception
            {
            System.out.println("Begin TopicSendRecvClient, now=" +
            System.currentTimeMillis());
            ChatClient client = new ChatClient ();
            client.sendRecvAsync("A text msg, now="+System.currentTimeMillis());
            client.done.acquire();
            client.stop();
            System.out.println("End TopicSendRecvClient");
            System.exit(0);
            }
            
            }


            You could expand it adding Selectors which allow you to filter messages.

            // Set the Message Selector
            message.setStringProperty("CHATROOM",chatRoom);
            topicPublisher.publish(message);


            good chatting
            regards