一.WeblogicMDB类(Message Driven Bean)
import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
public class WeblogicMDB implements MessageDrivenBean, MessageListener {
private static final long serialVersionUID = 5582665474886073061L;
private MessageDrivenContext context;
private Context jndiContext;
public void setMessageDrivenContext(MessageDrivenContext context)
throws EJBException {
this.context = context;
try {
jndiContext = new InitialContext();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void ejbCreate() {
}
public void ejbRemove() {
}
public void onMessage(Message message) {
if (message instanceof TextMessage) {
//System.out.println("Yeah! I have received the TextMassage:");
TextMessage txtmsg = (TextMessage) message;
try {
System.out.println("I have received the TextMassage:");
System.out.println(txtmsg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
二.ejb-jar.xml
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_2_0.dtd">
<ejb-jar>
<enterprise-beans>
<message-driven>
<ejb-name>WeblogicMDBName</ejb-name>
<ejb-class>WeblogicMDB</ejb-class>
<transaction-type>Container</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.Queue</destination-type>
</message-driven-destination>
<env-entry>
<description>This is a bean listening on a queue.</description>
<env-entry-name>listen_type</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>queue</env-entry-value>
</env-entry>
</message-driven>
</enterprise-beans>
</ejb-jar>
三.weblogic-ejb-jar.xml
<?xml version="1.0"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN' 'http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd'>
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>WeblogicMDBName</ejb-name>
<message-driven-descriptor>
<pool>
<max-beans-in-free-pool>10</max-beans-in-free-pool>
<initial-beans-in-free-pool>
2
</initial-beans-in-free-pool>
</pool>
<destination-jndi-name>MyJMSQueue</destination-jndi-name>
<initial-context-factory>
weblogic.jndi.WLInitialContextFactory
</initial-context-factory>
<provider-url>t3://127.0.0.1:7001/</provider-url>
<connection-factory-jndi-name>
MyJMSConnectionFactory
</connection-factory-jndi-name>
</message-driven-descriptor>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
使用上一篇文章中的QueueSupplier发送消息,输出示例:
过程很简单,值得注意的是系统的JDK最好和Weblogic使用的保持一致,至少不能存在代差.例如
输出消息我曾经是这样写的:
System.out.println("I have received the TextMassage:"+txtmsg.getText());
系统中的JDK1.5使用StringBuilder来把两个字符串加在一起,而Weblogic自带的1.4不认识StringBuilder,就产生了异常.
而制定Weblogic8.1的JDK为系统中的JDK1.5又会导致错误. 所以,编程时建议JDK和Weblogic8.1的JDK保持一致,以避免发生莫明其妙的错误.
程序下载:
http://www.blogjava.net/Files/sitinspring/WeblogicMDB20070910131749.rar