快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

Jboss Ejb MDB



 


对jms不是很认识,弄了几天终于有了点思路。这里有两个程序、增加了个queue-example-service.xml配置文件很简朴,复杂点的在以后在说。




 



 


package org.jboss.tutorial.mdb.bean;




 


import javax.ejb.MessageDriven;



import javax.ejb.ActivationConfigProperty;



import javax.jms.Message;



import javax.jms.MessageListener;




 


@MessageDriven(activateConfig =



        {



        @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),



        @ActivationConfigProperty(propertyName="destination", propertyValue="queue/tutorial/example")



        })



        //A destination is the object on the JBossMQ server that clients



        //use to send and receive messages. There are two types of



        //destination objects, Queues and Topics.



public class ExampleMDB implements MessageListener



{



   public void onMessage(Message recvMsg)



   {



      System.out.println("----------------");



      System.out.println("Received message");



      System.out.println("----------------");



   }



}




 


英文注释是我在The JBoss 4 Application Server Guide找的,可以知道destination是在jboss 服务器里负责收发消息 ( message ) 的地方。Destination根据消息发布方法的不同分两种:Queues 和 Topics .



topic发布答应一对多,或多对多通讯通道,消息的产生者被叫做publisher, 消息接受者叫做subscriber,故称为 发布/订阅(publish/Subscribe)。



queue 是另外一种方法,仅仅答应一个消息传送给一个客户。一个发送者将消息放在消息队列中,接受者从队列中抽取并得到消息,消息就会在队列中消失。第一个接受者抽取并得到消息后,其他人就不能在得到它。又称为 点对点(point to point) .




 


关于activateConfig跟queue-example-service.xml有关,这里只要知道destination就行了。从程序可以看到其接口为MessageListener可猜出这个程序负责监听消息,收到消息后打印。就是这样 :)




 



 


Client.java



package org.jboss.tutorial.mdb.client;




 


import javax.jms.Queue;



import javax.jms.QueueConnection;



import javax.jms.QueueConnectionFactory;



import javax.jms.QueueSender;



import javax.jms.QueueSession;



import javax.jms.TextMessage;



import javax.naming.InitialContext;




 


public class Client



{



   public static void main(String[] args) throws Exception



   {



      QueueConnection cnn = null;



      QueueSender sender = null;



      QueueSession session = null;



      InitialContext ctx = new InitialContext();



      Queue queue = (Queue) ctx.lookup("queue/tutorial/example");



         //这里lookup的内容在queue-example-service.xml有定义jndi



      QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");



      cnn = factory.createQueueConnection();



      session = cnn.createQueueSession(false,//不需要事务



              QueueSession.AUTO_ACKNOWLEDGE);//自动接收消息




 


      TextMessage msg = session.createTextMessage("Hello World");




 


      sender = session.createSender(queue);



      sender.send(msg);



      System.out.println("Message sent successfully to remote queue.");



   }



}




 


Client在这里的执行顺序是 QueueConnectionFactoryà QueueConnection à QueueSession àQueueSender



假如这样说还是不清晰的话就要先补习下jms咯。呵呵,我也是这样的。



Client的任务呢就是发送个消息,然后由服务器接收。




 



 



 


queue-example-service.xml



<?xml version="1.0" encoding="UTF-8"?>



<server>



   <mbean code="org.jboss.mq.server.jmx.Queue"



      name="jboss.mq.destination:service=Queue,name=tutorial">



      <attribute name="JNDIName">queue/tutorial/example</attribute>



      <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>



   </mbean>



</server>




 


首先给我的感觉就是这个xml跟jmx有关,mbean代表manage bean ,这个问题不大。



这个xml的作用就是instance个queue,名字为tutorial ( 可以自己改过 ) ,然后由jmx-console这个控制台统一治理,运行完这个程序可以在http://localhost:8080/jmx-console/HtmlAdaptor?action=inspectMBean&name=jboss.mq.destination%3Aservice%3DQueue%2Cname%3Dtutorial (希望你还没来的及更改上面的配置,我找不到能让你更快找到这个queue的地址了,太多了。)  看到这个配置文件上的queue ,jboss里已经有了几个queue了。



然后就是定义个jndi,Client.java就可以lookup了。




 



 


这里附上log4j.properties 在jboss-EJB-3.0_Preview_5.zip 里面没有这个老是显示缺少appender。有了这个将在该目录下生成个record.log日志文件。




 


log4j.properties



log4j.appender.R=org.apache.log4j.RollingFileAppender



log4j.appender.R.File=record.log



log4j.appender.R.layout=org.apache.log4j.PatternLayout



log4j.appender.R.layout.ConversionPattern=%p  %d{hh:mm:ss} %t %c{1} -%m%n



log4j.appender.R.MaxBackupIndex=1



log4j.appender.R.MaxFileSize=100KB



log4j.appender.stdout.layout=org.apache.log4j.PatternLayout



log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) -%m%n



log4j.appender.stdout=org.apache.log4j.ConsoleAppender



log4j.rootLogger=stdout,R




 



 



 


运行:参考installing.html



Windows下



打开命令提示符cmd,到  jboss_home/bin



 Run.bat –c all



用ant



先build后run 就行了。



看看jboss窗口可以看到



01:01:20,828 INFO  [STDOUT] ----------------



01:01:20,828 INFO  [STDOUT] Received message



01:01:20,828 INFO  [STDOUT] ----------------




 



 



 


讨论:



虽然就两个程序,但是由于我以前没怎么了解jms 就花了些时间。查找相关的资料对于理解以上问题是很重要。



http://www.cn-java.com/target/news.php?news_id=2730



http://blog.blogchina.com/category.225381.html



The JBoss 4 Application Server Guide




 



 





返回类别: 教程
上一教程: 编写自己的登录与访问控制模块
下一教程: 问一个低级输入流的问题。。。

您可以阅读与"Jboss Ejb MDB"相关的教程:
· Jboss Ejb3.0 Entity Bean
· Jboss Ejb3.0 Interceptor
· Jboss Ejb3.0 Statefull Bean
· Jboss EJB3.0 security
· Jboss EJB 3.0--Stateless Beans
    微笑服务 优质保证 索取样品