JBoss6.0+EJB3中Message-DrivenBean的"HelloWorld"实现

        JBoss6.0 + EJB3 中Message-Driven Bean 的"HelloWorld" 实现。

        目的:了解 EJB3 中Message-Driven Bean的开发过程及其工作原理。

        开发步骤:

                1)开发服务端。编写Message-Driven Bean类。

                2)配置并发布Message-Driven Bean到JBoss服务器。

                3)开发消息客户端。

        项目结构预览:

                1)服务端项目结构:

                        JBoss6.0+EJB3中Message-DrivenBean的"HelloWorld"实现

                2)客户端项目结构:

                             JBoss6.0+EJB3中Message-DrivenBean的"HelloWorld"实现

        开发过程及详细代码:

                1)DMBQueueBean


package com.ejb;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(
		activationConfig = {
			@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
			@ActivationConfigProperty(propertyName="destination",propertyValue="queue/myqueue")
		})
public class MDBQueueBean implements MessageListener {

	@Override
	public void onMessage(Message message) {
		try {
			TextMessage textMessage = (TextMessage)message;
			System.out.println("MDBQueueBean被调用=====收到消息: "+textMessage.getText());
			} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}


                2)DMBTopicBean


package com.ejb;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(
		activationConfig = {
				@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
				@ActivationConfigProperty(propertyName="destination",propertyValue="topic/mytopic")
		})
public class DMBTopicBean implements MessageListener {
	
	@Override
	public void onMessage(Message message) {
		try {
			TextMessage textMessage = (TextMessage)message;
			System.out.println("MDBTopicBean被调用=====收到主题: "+textMessage.getText());
			} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}


                3)在JBoss6.0安装目录下(如:D:\jboss\server\default\deploy\hornetq)找到hornetq-jms.xml文件,并把如下内容添加到该配置文件中。


<topic name="MyTopic">

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

</topic>


<queue name="MyQueue">

<entry name="/queue/myqueue"/>

</queue>


                4)DMBQueueBeanTest

                

package com.ejb.messagedriven;

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 DMBQueueBeanTest {

	public static void main(String[] args) throws Exception {
		InitialContext ctx = new InitialContext();

		// 获取ConnectionFactory对象
		QueueConnectionFactory factory = (QueueConnectionFactory) ctx
				.lookup("ConnectionFactory");

		// 创建QueueConnection对象
		QueueConnection connection = factory.createQueueConnection();

		// 创建QueueSession对象,第一个参数表示事务自动提交,第二个参数标识一旦消息被正确送达,将自动发回响应
		QueueSession session = connection.createQueueSession(false,
				QueueSession.AUTO_ACKNOWLEDGE);

		// 获得Destination对象
		Queue queue = (Queue) ctx.lookup("queue/myqueue");

		// 创建文本消息
		TextMessage msg = session.createTextMessage("你好,这是一个队列消息! ");

		// 创建发送者
		QueueSender sender = session.createSender(queue);

		// 发送消息
		sender.send(msg);

		// 关闭会话
		session.close();

		connection.close();

		System.out.println("消息已发送!");
	}

}

                5)DMBTopicBeanTest



package com.ejb.messagedriven;

import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.InitialContext;

public class DMBTopicBeanTest {

	public static void main(String[] args) throws Exception {
		InitialContext ctx = new InitialContext();

		// 获取ConnectionFactory对象
		TopicConnectionFactory factory = (TopicConnectionFactory) ctx
				.lookup("ConnectionFactory");

		// 创建TopicConnection对象
		TopicConnection connection = factory.createTopicConnection();

		// 创建TopicSession对象,第一个参数表示事务自动提交,第二个参数标识一旦消息被正确送达,将自动发回响应
		TopicSession session = connection.createTopicSession(false,
				TopicSession.AUTO_ACKNOWLEDGE);

		// 获得Destination对象
		Topic topic = (Topic) ctx.lookup("topic/mytopic");

		// 创建文本消息
		TextMessage msg = session.createTextMessage("你好,这是一个主题消息! ");

		// 创建发布者
		TopicPublisher publisher = session.createPublisher(topic);

		// 发送消息
		publisher.publish(msg);

		// 关闭会话
		session.close();
		
		connection.close();

		System.out.println("消息已发送!");
	}

}


                6)jndi.properties

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost

              

              7)测试:

                        1.测试DMBQueueBean

                                控制台中客户端输出:

2012-10-30 18:56:03  INFO  createSocket, hostAddr: localhost/127.0.0.1, port: 1099, localAddr: null, localPort: 0, timeout: 0
消息已发送!

                                控制台中服务端输出:

18:55:50,487 INFO  [org.jboss.ejb3.EJBContainer] STARTED EJB: com.ejb.MDBQueueBean ejbName: MDBQueueBean
18:55:50,487 WARN  [org.jboss.ejb3.TimerServiceContainer] EJBTHREE-2193: using deprecated TimerServiceFactory for restoring timers
18:56:05,231 INFO  [STDOUT] MDBQueueBean被调用=====收到消息: 你好,这是一个队列消息!

                         2.测试DMBTopicBean

                                控制台中客户端输出:

2012-10-30 18:57:14  INFO  createSocket, hostAddr: localhost/127.0.0.1, port: 1099, localAddr: null, localPort: 0, timeout: 0
消息已发送!

                                控制台中服务端输出:

18:57:54,235 INFO  [STDOUT] MDBTopicBean被调用=====收到主题: 你好,这是一个主题消息!










你可能感兴趣的:(bean,Queue,EJB3,topic,Message-Driven,JBoss6.0)