开发的基本过程包括:
u 创建EJB工程
u 创建消息驱动Bean
u 创建客户端程序
u 运行测试
2、创建EJB工程
选择【File】à【New Project】,在弹出的界面上选择【Enterprise】à【EJB Module】,在弹出的界面中输入工程的名字“MDBTest”,完成工程的创建。
3、创建消息驱动Bean
在工程上面点击右键,选择【New】à【Message-Driven Bean…】,如下图所示。
<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 209.25pt; HEIGHT: 168pt" type="#_x0000_t75"><imagedata o:title="创建MDB" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.png"></imagedata></shape>
在弹出的界面上输入消息驱动Bean的名字,并为Bean类指定包。这里包名为“ch<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="20" unitname="”"><font face="Times New Roman">20</font><span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><span lang="EN-US">”</span></span></chmetcnv>,类名是“TextMDB”。
创建后的消息驱动Bean的代码如下:
/*
* TextMDB.java
*
* Created on <chsdate w:st="on" isrocdate="False" islunardate="False" day="14" month="5" year="2007"><font face="Times New Roman">2007</font><span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><span lang="EN-US">年</span></span><font face="Times New Roman">5</font><span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><span lang="EN-US">月</span></span><font face="Times New Roman">14</font><span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><span lang="EN-US">日</span></span></chsdate>, 下午7:28
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package ch20;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
/**
* Entity class TextMDB
*
* @author Administrator
*/
@MessageDriven(mappedName = "jms/TextMDB", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class TextMDB implements MessageListener {
/** Creates a new instance of TextMDB */
public TextMDB() {
}
public void onMessage(Message message) {
}
}
在类前面的代码:
@MessageDriven(mappedName = "jms/TextMDB", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
声明这个消息驱动Bean使用的目的是“jms/TextMDB”,默认的生成的连接工厂的名字是“jms/TextMDBFactory”。activationConfig声明消息的确认模式和目的的类型(队列)。
在onMessage方法中添加对消息处理的代码,这里假设接收到的消息是TextMessage,接收到消息之后把消息输出到控制台。修改onMessage方法如下:
public void onMessage(Message message) {
TextMessage msg = (TextMessage)message;
try{
System.out.println(msg.getText());
}catch(Exception e){
System.out.println(e.getMessage());
}
}
部署该EJB应用,在该工程上点击右键选择【Deploy project】,完成消息驱动Bean的部署。
4、创建客户端程序
选择【File】à【New Project】,在弹出的界面中选择【Enterprise】à【Enterprise Application Client】,输入客户端工程的名字“mdbClient”。
客户端的代码如下:
/*
* Main.java
*
* Created on <chsdate w:st="on" isrocdate="False" islunardate="False" day="14" month="5" year="2007"><font face="Times New Roman">2007</font><span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><span lang="EN-US">年</span></span><font face="Times New Roman">5</font><span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><span lang="EN-US">月</span></span><font face="Times New Roman">14</font><span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><span lang="EN-US">日</span></span></chsdate>, 下午7:51
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package mdbclient;
import javax.naming.*;
import javax.jms.*;
/**
*
* @author Administrator
*/
public class <place w:st="on">Main</place> {
/** Creates a new instance of <place w:st="on">Main</place> */
public <place w:st="on">Main</place>() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try{
// 创建上下文环境
Context ctx = new InitialContext();
// 查找连接工厂
QueueConnectionFactory qConFactory = (QueueConnectionFactory)
ctx.lookup("jms/TextMDBFactory");
// 查找队列
javax.jms.Queue messageQueue = (javax.jms.Queue) ctx.lookup("jms/TextMDB");
// 创建连接
QueueConnection qCon = qConFactory.createQueueConnection();
// 创建会话Bean
QueueSession session = qCon.createQueueSession(
false, /* not a transacted session */
Session.AUTO_ACKNOWLEDGE
);
// 创建消息发送者
QueueSender sender = session.createSender(messageQueue);
// 创建要发送的消息
TextMessage msg = session.createTextMessage();
// 为消息赋值
msg.setText("发送给消息驱动Bean的消息!");
System.out.println("开始发送消息!");
// 发送消息
sender.send(msg);
qCon.close();
System.out.println("消息发送成功!");
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
注意:这里发送消息的连接工厂和目的应该与消息驱动Bean的相同。
5、运行测试
在客户端程序所在的工程上点击右键选择【Run project】。可以在服务器端的控制台上看到消息驱动Bean的输出信息如下图所示:
<shape id="_x0000_i1026" style="WIDTH: 414.75pt; HEIGHT: 174.75pt" type="#_x0000_t75"><imagedata o:title="运行结果" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image003.png"></imagedata></shape>