[jboss-user] [JBoss Messaging] - Re: Best architecture practice for jboss messaging.

francis17101970 do-not-reply at jboss.com
Tue Sep 9 09:15:34 EDT 2008


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

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4175298#4175298

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4175298



More information about the jboss-user mailing list