EJB学习之五---MDB(消息驱动EJB)

前面已经介绍过EJB的Session Bean,这篇文章主要介绍EJb的消息驱动bean---MDB.
Key
消息驱动Bean定义
发布消息方式
实现MDB
Sample

一 消息驱动Bean定义

二 发布消息方式
   1. P2P(Point to Point)

   2. Pub-Sub(Publish/Subcrib)

三 实现MDB

四 Sample:
   1. P2P的MDB
   1) 消息驱动EJB:QueueMdbBean
 
 import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * @author Jamson Huang
 *
 */
@MessageDriven(
	activationConfig={
			@ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Queue"),
			@ActivationConfigProperty(propertyName="destination",propertyValue="queue/QueueMdb")
	}
)
/*P2P*/
public class QueueMdbBean implements MessageListener {

	/* (non-Javadoc)
	 * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
	 */
	public void onMessage(Message msg) {
	   try{
		   TextMessage txtMsg = (TextMessage)msg;
		   System.out.println("Queue Text:"+ txtMsg.getText());
	   }catch(Exception ex){
		   ex.printStackTrace();
	   }

	}

}

    2)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;

/**
 * @author Jamson Huang
 *
 */
public class QueueMdbEjbBeanClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		InitialContext context = new InitialContext();
		
		QueueConnectionFactory factory = (QueueConnectionFactory)context.lookup("ConnectionFactory");
		
		QueueConnection conn = factory.createQueueConnection();
		
		QueueSession session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
		
		Queue queue = (Queue)context.lookup("queue/QueueMdb");
		
		TextMessage txtMsg = session.createTextMessage("Hello world, Jamson");
		QueueSender sender = session.createSender(queue);
		
		sender.send(txtMsg);
		System.out.println(txtMsg.getText());
		
		session.close();
		conn.close();
		System.out.println("queue has been sent!");
	}

}
}


   2. PUB/SUB的MDB
   1) 消息驱动EJB:TopicMdbBean
 
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * @author Jamson Huang
 *
 */
@MessageDriven(
		activationConfig={
				@ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Topic"),
				@ActivationConfigProperty(propertyName="destination",propertyValue="topic/TopicMdb")
		}
	)
public class TopicMdbBean implements MessageListener {

	/* (non-Javadoc)
	 * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
	 */
	public void onMessage(Message msg) {
		// TODO Auto-generated method stub
	   try{
		   TextMessage txtMsg = (TextMessage)msg;
		   System.out.println("Queue Text:"+ txtMsg.getText());
	   }catch(Exception ex){
		   ex.printStackTrace();
	   }

	}

}

2)MDB client:
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;

/**
 * @author Jamson Huang
 *
 */
public class TopicMdbEjbBeanClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		InitialContext context = new InitialContext();
		
		TopicConnectionFactory factory = (TopicConnectionFactory)context.lookup("ConnectionFactory");
		
		TopicConnection conn = factory.createTopicConnection();
		
		TopicSession session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
		
		Topic topic = (Topic)context.lookup("topic/TopicMdb");
		
		TextMessage txtMsg = session.createTextMessage("Hello world, Jamson");
		TopicPublisher publisher = session.createPublisher(topic);
		
		publisher.send(txtMsg);
		System.out.println(txtMsg.getText());
		
		session.close();
		conn.close();
		System.out.println("Topic has been sent!");
	}

}

你可能感兴趣的:(bean,jms,ejb)