CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2EE / EJB / JMS

在创建一个非常简单的JMS测试程序时出现了如下问题,请高手指教!

楼主gen2(踏雪观月)2005-04-02 14:36:56 在 Java / J2EE / EJB / JMS 提问

初学Java,请指教  
  1.J2EE   版本:   1.4  
  2.J2EE   Default   server   has   started  
  3.在J2EE   admin   console中创建了  
  JNDI   Name:   jms/MyQueueConnectionFactory    
  Type:             javax.jms.QueueConnectionFactory    
  Status:         enabled  
   
  JNDI   Name:   jms/MyTestQueue    
  Type:             javax.jms.Queue  
  Status:         enabled  
  然后保存  
  3.编写下面的类  
  import   javax.naming.InitialContext;  
  import   javax.naming.NamingException;  
  import   javax.jms.*;  
   
  public   class   TestQueue   {  
   
          protected   static   String   sendUsage   =  
                  "TestQueue   send   queue-name   [strings...]";  
          protected   static   String   recvUsage   =  
                  "TestQueue   recv   queue-name";  
   
          //   Change   this   variable's   value   to   change   the   connection   factory   name  
          protected   static   String   qfactoryName   =  
                  "jms/MyQueueConnectionFactory";  
   
          protected   static   void   Usage(String   message)   {  
                  if   (message   ==   null)   {  
                          message   =   sendUsage   +   "\n\t"   +   recvUsage;  
                  }  
                  System.err.println("Usage:   "   +   message);  
                  System.err.print("Read   Enterprise   Java   Tech   Tips,   March   2003   ");  
                  System.err.println("for   instructions");  
                  System.exit(1);  
          }  
           
          //   This   is   the   main   program  
          public   static   void   main(String[]   args)   {  
                  if   (args.length   ==   0)   {  
                          Usage(null);  
                  }  
   
                  String   command   =   args[0];  
   
                  try   {  
                          if   (command.equalsIgnoreCase("send"))   {  
                                  send(args);  
                          }   else   if   (command.equalsIgnoreCase("recv"))   {  
                                  recv(args);  
                          }   else   {  
                                  Usage(null);  
                          }  
                  }   catch   (NamingException   nex)   {  
                          System.err.println("Naming   exception   :   "   +   nex.getMessage());  
                          nex.printStackTrace(System.err);  
                  }   catch   (JMSException   jex)   {  
                          System.err.println("JMS   exception   :   "   +   jex.getMessage());  
                          jex.printStackTrace(System.err);  
                          System.err.println("Be   sure   j2ee   server   is   running");  
                  }  
          }  
   
          //   Send   a   JMS   message   to   a   queue.  
          //   Arguments:  
          //   args[0]   is   the   word   "send"  
          //   args[1]   is   the   queue   name  
          //   args[2..end]   are   the   strings   to   send   as   TextMessages  
   
          protected   static   void   send(String[]   args)    
              throws   JMSException,   NamingException   {  
   
                  String   queueName;  
   
                  if   (args.length   <=   1)   {  
                          Usage("sendUsage");  
                  }  
   
                  queueName   =   args[1];  
                  QueueConnection   qc   =   null;  
   
                  try   {  
                          //   Get   JNDI   context  
                          InitialContext   ctx   =   new   InitialContext();  
   
                          //   Get   connection   factory    
                          QueueConnectionFactory   qcf   =  
                                  (QueueConnectionFactory)ctx.lookup(qfactoryName);  
                           
                          //   Get   a   connection   to   the   queue  
                          qc   =   qcf.createQueueConnection();  
   
                          //   Get   a   session   from   the   connection  
                          QueueSession   qs   =  
                                  qc.createQueueSession(false,   Session.AUTO_ACKNOWLEDGE);  
   
                          //   Get   a   queue  
                          Queue   q   =   (Queue)ctx.lookup(queueName);  
   
                          //   Use   the   session   to   create   a   QueueSender   and   a   TextMessage.  
                          QueueSender   qsnd   =   qs.createSender(q);  
                          TextMessage   tm   =   qs.createTextMessage();  
   
                          //   For   each   argument   (after   the   first),   send   the   argument  
                          //   string   as   a   text   message.  
                          for   (int   i   =   2;   i   <   args.length;   i++)   {  
                                  System.out.println("Sent:   '"   +   args[i]   +   "'");  
                                  tm.setText(args[i]);  
                                  qsnd.send(tm);  
                          }  
                  }   finally   {  
                          if   (qc   !=   null)   {  
                                  qc.close();  
                          }  
                  }  
          }  
   
   
          //   Receive   and   print   JMS   messages   from   a   queue  
          //   Arguments:  
          //   args[0]   is   the   word   "recv"  
          //   args[1]   is   the   queue   name  
   
          protected   static   void   recv(String[]   args)    
              throws   JMSException,   NamingException   {  
                  String   queueName;  
   
                  if   (args.length   <=   1)   {  
                          Usage(recvUsage);  
                  }  
   
                  queueName   =   args[1];  
   
                  QueueConnection   qc   =   null;  
   
                  try   {  
   
                          InitialContext   ctx   =   new   InitialContext();  
   
                          QueueConnectionFactory   qcf   =  
                                  (QueueConnectionFactory)ctx.lookup(qfactoryName);  
                          qc   =   qcf.createQueueConnection();  
                          QueueSession   qs   =  
                                  qc.createQueueSession(false,   Session.AUTO_ACKNOWLEDGE);  
   
                          Queue   q   =   (Queue)ctx.lookup(queueName);  
                          QueueReceiver   qrcv   =   qs.createReceiver(q);  
                           
                          qc.start();  
                           
                          Message   m   =   qrcv.receive(10000);  
                          while   (m   !=   null)   {  
                                  if   (m   instanceof   TextMessage)   {  
                                          TextMessage   tm   =   (TextMessage)m;  
                                          System.out.println("Received   text:   '"   +  
                                                                                tm.getText()   +   "'");  
                                  }   else   {  
                                          System.out.println("Received   a   "   +  
                                                                                m.getClass().getName());  
                                  }  
                                  m   =   qrcv.receive(100);  
                          }  
                  }   finally   {  
                          if   (qc   !=   null)   {  
                                  qc.close();  
                          }  
                  }  
          }  
  }  
  编译ok!  
   
  4.执行如下命令  
  java   TestQueue   send   jms/queue/MyTestQueue   a   b   c   d  
  出现如下错误  
  Naming   exception   :   Need   to   specify   class   name   in   environment   or   system   property,  
    or   as   an   applet   parameter,   or   in   an   application   resource   file:     java.naming.fac  
  tory.initial  
  请问这是为什么?  
  我对JMS是这么理解的:  
  1.消息生产者   发消息到消息队列    
  2.消息队列对接收的消息持久化  
  3.消息消费者进程   从消息队列提取消息  
  是不是j2EE的admin   console创建的connection   Factory和Queue有问题?  
  //java   TestQueue   recv   jms/queue/MyTestQueue  
  问题点数:50、回复次数:1Top

1 楼zhutouzip(醒了的鸟)回复于 2005-04-02 14:56:20 得分 50

写一个jndi.properties文件,加入下面的内容:  
  java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory  
  java.naming.provider.url=localhost:1099  
  java.naming.factory.url.pkgs=com.sun.enterprise.naming  
  =后面的内容根据不同的服务器适当改变,具体的参考这些服务气的文档!Top

相关问题

  • 创建的 asp.net Web Application程序.运行后出现如下错误提示。说我访问失败.
  • 求助!用.net2003创建智能设备程序系统提示 "在如下位置找不到Default.js或default.vbs" 后面跟一大串目录
  • 怎么创建如下的触发器
  • 请看一下如下的程序。
  • 低手有如下程序编译
  • 程序编写如下,错在哪?
  • 在线用户程序如下:
  • 一段简单的C#程序,请指定;程序如下:
  • 谁能简化如下程序:《C程序语言》中的一个习题。
  • 程序挑错,源程序如下,有关读Excel表,写入数据库的。

关键词

  • j2ee
  • jms
  • testqueue
  • javax
  • protected static
  • message
  • import

得分解答快速导航

  • 帖主:gen2
  • zhutouzip

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo