細心!用心!耐心!

          吾非文人,乃市井一俗人也,讀百卷書,跨江河千里,故申城一游; 一兩滴辛酸,三四年學業,五六點粗墨,七八筆買賣,九十道人情。

          BlogJava 聯系 聚合 管理
            1 Posts :: 196 Stories :: 10 Comments :: 0 Trackbacks

          JMS編程模式

          消息形式:

          點對點為兩個客戶之間建立消息隊列,使兩個客戶端之間通過隊列實現點對點的消息傳遞

          注:類似數據結構里的隊列,先進先出

          主題消息,在消息中間件上建立一個主題,沒個客戶端都可已向這個主題發送消息,接受消息

          注:類似廣播的方式

          開發流程:

           


          使用JMS步驟

          1.創建一個JNDI上下文

          Context  init initCtx =new InitialContext(env);

          2.查找創建JMS連接使用的工廠類(Connnect Factory)

          對于主題消息:

          Object tmp=initCtx.lookup(“Connnect Factory”);

          TopicConnectionFactory tcf=( TopicConnectionFactory) tmp;

          對于點對點消息:

          Object tmp=initCtx.lookup(“Connnect Factory”);

          QueueConnectionFactory tcf=( QueueConnectionFactory) tmp;

          3.查找JMS的目標對象(Destination)

          對于主題消息:

          Topic topic=(Topic)iniCtx.lookup(“topic/testTopic”);

          對于點對點消息:

          Queue queue (Queue)iniCtx.lookup(“queue /A”);

          4.創建JMS連接(Connection)

          對于主題消息:

          TopicConnection conn=tcp.createTopicConnection();

          對于點對點消息:

          QueueConnection conn=tcp.createQueueConnection ();

          5.創建JMS會話(session)

          對于主題消息:

          TopicSession session=conn.createTopicSession(fasle,Session.AUTO_ACKNOWLEDGE);

          對于點對點消息:

          QueueSession session= conn.createQueueSession(true,0);

          6.創建消息的生產和消費者

          生產者:

          對于主題消息:

          TopicPublisher publisher=session.createPublisher(topic);

          對于點對點消息:

          QueueSender sender = session.createSender (queue);

          消費者:

          對于主題消息:

          TopicSubscriber subscriber=session.createSubscriber (topic);

          對于點對點消息:

          QueueReceiver receiver= session.createReceiver (queue);

          7.注冊消息的監聽者

          對于主題消息:

          TextListener listener=new TextListener();

          subscriber.setMessageListener(listener);

          對于點對點消息:

          TextListener listener=new TextListener();

          receiver.setMessageListener(listener);

          8.開始JMS的連接

             conn.start();

          9.發送和接受消息

          發送消息:

          對于主題消息:

          publisher.publish(message);

          對于點對點消息:

          sender.send(message);

          接受消息:

          對于主題消息:

          Message msg= subscriber.receive();

          對于點對點消息:

          Message msg= receiver.receive(1000);

          10.停止和關閉JMS連接

          conn.stop();

          session.close();

          conn.close();

          posted on 2007-05-06 11:43 張金鵬 閱讀(98) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 栾川县| 马龙县| 广丰县| 泰顺县| 陆川县| 温宿县| 固安县| 隆尧县| 喀喇沁旗| 宜兴市| 惠州市| 新巴尔虎左旗| 斗六市| 札达县| 宁都县| 永年县| 许昌县| 土默特右旗| 清丰县| 边坝县| 通榆县| 浦县| 新安县| 手游| 巴林右旗| 巢湖市| 衡阳县| 黄大仙区| 北辰区| 额尔古纳市| 德州市| 广汉市| 花垣县| 蒙自县| 绥江县| 广安市| 新建县| 凉城县| 裕民县| 个旧市| 黎川县|