手机版
你好,游客 登录 注册
背景:
阅读新闻

Jboss 6 JMS 部署

[日期:2011-12-01] 来源:Linux社区  作者:liu372267001 [字体:大 中 小]
今天学习MDB 遇到了一个问题,一直 topic not found ,在网上找到很多资料,都没有结果。因为网上的jboss版本都是4.x 而我的jboss 6 所以出错了。后来才知道是 

  在%JBOSS_HOME%\server\default\deploy\hornetq\hornetq-jms.xml中配置JMS 目标地址 如

  <topic name="topic1">
 <entry name="/topic/topic1"/>
   </topic>

发送消息

  1. public class TopicMsg extends Properties{  
  2.   
  3.       
  4.     /** 
  5.      *  
  6.      */  
  7.     private static final long serialVersionUID = 1L;  
  8.   
  9.     public TopicMsg(){  
  10.         InputStream in = Class.class.getResourceAsStream("jndi.properties");  
  11.         try {  
  12.             load(in);  
  13.         } catch (IOException e) {  
  14.             e.printStackTrace();  
  15.         }  
  16.     }  
  17.       
  18.     public static void main(String[] args) {  
  19.           
  20.         TopicSession tSession = null ; // session实例   
  21.         TopicConnection tConnection = null;// 连接实例   
  22.         Destination tDes = null;// 目标地址   
  23.         MessageProducer tProducer = null;//发布消息对象    
  24.         MapMessage mapMessage = null ;//消息的类型   
  25.           
  26.         try {  
  27.             InitialContext context = new InitialContext();  
  28.             TopicConnectionFactory fFactory = (TopicConnectionFactory)context.lookup("ConnectionFactory");  
  29.             tConnection = fFactory.createTopicConnection();  
  30.             // 第一个参数 是否在事物中 第二个参数 : 自动回复状态,已经收到消息   
  31.             tSession = tConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);  
  32.             tDes = (Destination)context.lookup("/topic/topic1");  
  33.             tProducer = tSession.createProducer(tDes);// 发布消息参数是发布的目标地址   
  34.             mapMessage = tSession.createMapMessage();  
  35.             mapMessage.setStringProperty("key1", "value1");  
  36.             mapMessage.setStringProperty("key2", "value2");  
  37.               
  38.             tProducer.send(mapMessage);  
  39.             System.out.println("Topic 消息发送成功!");  
  40.         } catch (NamingException e) {  
  41.             e.printStackTrace();  
  42.         } catch (JMSException ex){  
  43.             ex.printStackTrace();  
  44.         } finally{  
  45.             try{  
  46.                 tSession.close();  
  47.                 tConnection.close();  
  48.             }catch(JMSException jmsE){  
  49.                 jmsE.printStackTrace();  
  50.             }  
  51.         }  
  52.     }   
 
  1. 订阅者  
  2.   
  3. /** 
  4.  * 主题与订阅的消息 
  5.  *  
  6.  * @author liujx 
  7.  *  
  8.  */  
  9. @MessageDriven(activationConfig = {  
  10.   @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),// 指定消息类别   
  11.   @ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/topic1") })// 指定目标地址   
  12. public class TopicMsg implements MessageListener {  
  13.   
  14.  public void onMessage(Message arg0) {  
  15.   if(arg0 instanceof MapMessage){  
  16.    try {  
  17.     System.out.println("TopicMsg01 key1 = "+arg0.getStringProperty("key1"));  
  18.     System.out.println("TopicMsg01 key2 = "+arg0.getStringProperty("key2"));  
  19.    } catch (JMSException e) {  
  20.     e.printStackTrace();  
  21.    }  
  22.   }  
  23.  }  
  24.   
  25. }  

这样一个简单jms就完成了。

linux
相关资讯       Jboss 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款