从头到尾讲解EJB MDB(消息驱动bean)四――示例代码

、Queue:
客户端:
[java] view plaincopy
public class MyMDBBeanClient { 
    public static void main(String[] args) throws Exception { 
        InitialContext ict = new InitialContext(); 
        //获取connectionFactory对象 
        QueueConnectionFactory factory = (QueueConnectionFactory)ict.lookup("ConnectionFactory"); 
        //获取Connection对象 
        QueueConnection connection = factory.createQueueConnection(); 
        //获取Session对象,第一个参数表示事务自动提交,第二个参数表示一旦消息被正确发送,将自动发回响应 
        QueueSession session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);      
        //获取destination对象 
        Queue queue = (Queue)ict.lookup("queue/myqueue");    
        //设置消息 
        TextMessage msg = session.createTextMessage("hello world ");         
        //获取sender对象 
        QueueSender sender = session.createSender(queue);    
        //发送消息 
        sender.send(msg); 
         
        session.close(); 
         
        System.out.println("消息已经发送!"); 
    } 

MDB:
[java] view plaincopy
public class MyMDBBeanClient { 
    public static void main(String[] args) throws Exception { 
        InitialContext ict = new InitialContext(); 
        //获取connectionFactory对象 
        QueueConnectionFactory factory = (QueueConnectionFactory)ict.lookup("ConnectionFactory"); 
        //获取Connection对象 
        QueueConnection connection = factory.createQueueConnection(); 
        //获取Session对象,第一个参数表示事务自动提交,第二个参数表示一旦消息被正确发送,将自动发回响应 
        QueueSession session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);      
        //获取destination对象 
        Queue queue = (Queue)ict.lookup("queue/myqueue");    
        //设置消息 
        TextMessage msg = session.createTextMessage("hello world ");         
        //获取sender对象 
        QueueSender sender = session.createSender(queue);    
        //发送消息 
        sender.send(msg);        
        session.close();         
        System.out.println("消息已经发送!"); 
    } 

2.Topic
客户端:
[java] view plaincopy
public class MyTopicMDBBeanClient { 
    public static void main(String[] args) throws Exception { 
        InitialContext ict = new InitialContext(); 
        //获取connectionFactory对象 
        TopicConnectionFactory factory = (TopicConnectionFactory)ict.lookup("ConnectionFactory"); 
        //获取Connection对象 
        TopicConnection connection = factory.createTopicConnection(); 
        //获取Session对象,第一个参数表示事务自动提交,第二个参数表示一旦消息被正确发送,将自动发回响应 
        TopicSession session = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);      
        //获取destination对象 
        Topic topic = (Topic)ict.lookup("topic/mytopic");    
        //设置消息 
        TextMessage msg = session.createTextMessage("hello world ");         
        //获取sender对象 
        TopicPublisher publisher = session.createPublisher(topic);       
        //发送消息 
        publisher.publish(msg);      
        session.close();         
        System.out.println("消息已经发送!"); 
    } 

MDB1:
[java] view plaincopy
@MessageDriven ( 
        activationConfig = { 
                @ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Topic"), 
                @ActivationConfigProperty(propertyName="destination",propertyValue="topic/mytopic") 
        } 

public class MyTopicMDBBean1 implements MessageListener { 
    public void onMessage(Message msg) { 
        TextMessage message = (TextMessage)msg; 
        try { 
            System.out.println("MyTopicMDBBean1调用了:"+ message.getText()); 
        } catch (JMSException e) { 
            e.printStackTrace(); 
        } 
    } 

MDB2:
[java] view plaincopy
@MessageDriven ( 
        activationConfig = { 
                @ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Topic"), 
                @ActivationConfigProperty(propertyName="destination",propertyValue="topic/mytopic") 
        } 

public class MyTopicMDBBean2 implements MessageListener { 
    public void onMessage(Message msg) { 
        TextMessage message = (TextMessage)msg; 
        try { 
            System.out.println("MyTopicMDBBean2调用了:"+ message.getText()); 
        } catch (JMSException e) { 
            e.printStackTrace(); 
        } 
    } 

3、部署运行测试
将ejb mdb(queue、toppic)部署到jboss服务器上,运行客户端。
注意:如果是部署到jboss4.0版本上,不会有问题,但直接部署到jboss5.0版本上会报错:javax.naming.NameNotFoundException:myqueue not bound
解决方法:
在JBoss根目录\server\default\deploy 下的mail-service.xml文件中加入如下:
<mbeancode="org.jboss.mq.server.jmx.Queue"name="jboss.org.destination:server=Queue,name=myqueue" >
<attributename="JNDIName" >queue/myqueue</attribute>
<dependsoptional-attribute-name = "DestinationManager" >jboss.mq:service=DestinationManager </depends>
</mbean>
topic找不到
 <mbean code="org.jboss.mq.server.jmx.Topic"name="jboss.org.destination:server=Topic,name=mytopic" >
    <!-- JNDI名称 -->
    <attributename="JNDIName">topic/mytopic</attribute>
    <dependsoptional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
  </mbean>
重启JBoss问题即解决。
当然,如果有其他的MDB,还需要继续加入JNDIName。

你可能感兴趣的:(客户端,factory)