本文主要是介绍从头到尾讲解EJB MDB(消息驱动bean)四——示例代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、Queue:
客户端:
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:
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
客户端:
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:
@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:
@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。
这篇关于从头到尾讲解EJB MDB(消息驱动bean)四——示例代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!