MyEclipse+Message Driven Bean+ Weblogic8.1(Spring)

最近学习Spring对mdb(消息驱动Bean)的支持,折腾了两个星期,总算搞定了。现将配置总结如下:
一.ejb部分。
mdb类:

package com.mdb.ejb;

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;

/***/ /**
*XDoclet-basedMessageDrivenentitybean.
*
*TogenerateEJBrelatedclassesusingXDoclet:-AddStandardEJBmoduleto
*XDocletprojectproperties-CustomizeXDocletconfiguration-RunXDoclet
*
*Belowarethexdoclet-relatedtagsneededforthisEJB.
*
*@ejb.beanname="Hello"display-name="NameforHello"description="Description
*forHello"destination-type="javax.jms.Queue"
*acknowledge-mode="Auto-acknowledge"
*@weblogic.poolmax-beans-in-free-pool="10"initial-beans-in-free-pool="2"
*@weblogic.message-drivendestination-jndi-name="ejb/Hello"
*initial-context-factory="weblogic.jndi.WLInitialContextFactory"
*connection-factory-jndi-name="helloConnectionFactory"
*/

public class HelloBean implements MessageDrivenBean,MessageListener ... {
privatestaticfinallongserialVersionUID=1L;

/***//**TheMessageDrivenContext*/
MessageDrivenContextcontext;

publicHelloBean()...{
super();
//TODOAuto-generatedconstructorstub
}


/***//**
*Settheassociatedcontext.Thecontainercallsthismethodafterthe
*instancecreation.<br>
*
*Theenterprisebeaninstanceshouldstorethereferencetothecontext
*objectinaninstancevariable.<br>
*
*Thismethodiscalledwithnotransactioncontext.
*
*
@paramnewContext
*AMessageDrivenContextinterfacefortheinstance.
*
*
@throwsEJBException
*Thrownbythemethodtoindicateafailurecausedbya
*system-levelerror.
*/

publicvoidsetMessageDrivenContext(MessageDrivenContextnewContext)
throwsEJBException...{
context
=newContext;
}


publicvoidejbRemove()throwsEJBException...{
//TODOAuto-generatedmethodstub

}


publicvoidonMessage(MessagerecvMsg)...{
System.out.println(
"HelloBean接收到的消息:");
try...{
TextMessagemessage
=(TextMessage)recvMsg;
System.out.println(message.getText());
}
catch(JMSExceptione)...{
e.printStackTrace();
}

}


/***//**
*AnejbCreatemethodasrequiredbytheEJBspecification.
*
*Thecontainercallstheinstance?s<code>ejbCreate</code>method
*immediatelyafterinstantiation.
*
*@ejb.create-method
*/

publicvoidejbCreate()...{
}


}

ejb-jar.xml:

<? xmlversion="1.0"encoding="UTF-8" ?>

<! DOCTYPEejb-jarPUBLIC"-//SunMicrosystems,Inc.//DTDEnterpriseJavaBeans2.0//EN""http://java.sun.com/dtd/ejb-jar_2_0.dtd" >

< ejb-jar >

< description > <![CDATA[ NoDescription. ]]> </ description >
< display-name > GeneratedbyXDoclet </ display-name >

< enterprise-beans >
<!--
Toaddsessionbeansthatyouhavedeploymentdescriptorinfofor,add
afiletoyourXDocletmergedirectorycalledsession-beans.xmlthatcontains
the<session></session>markupforthosebeans.
-->

<!-- EntityBeans -->
<!--
Toaddentitybeansthatyouhavedeploymentdescriptorinfofor,add
afiletoyourXDocletmergedirectorycalledentity-beans.xmlthatcontains
the<entity></entity>markupforthosebeans.
-->

<!-- MessageDrivenBeans -->
< message-driven >
< description > <![CDATA[ DescriptionforHello ]]> </ description >
< display-name > NameforHello </ display-name >

< ejb-name > Hello </ ejb-name >

< ejb-class > com.mdb.ejb.HelloBean </ ejb-class >

< transaction-type > Container </ transaction-type >
< acknowledge-mode > Auto-acknowledge </ acknowledge-mode >
< message-driven-destination >
< destination-type > javax.jms.Queue </ destination-type >
</ message-driven-destination >

</ message-driven >

<!--
Toaddmessagedrivenbeansthatyouhavedeploymentdescriptorinfofor,add
afiletoyourXDocletmergedirectorycalledmessage-driven-beans.xmlthatcontains
the<message-driven></message-driven>markupforthosebeans.
-->

</ enterprise-beans >

<!-- Relationships -->

<!-- AssemblyDescriptor -->
<!--
Tospecifyyourownassemblydescriptorinfohere,addafiletoyour
XDocletmergedirectorycalledassembly-descriptor.xmlthatcontains
the<assembly-descriptor></assembly-descriptor>markup.
-->

< assembly-descriptor >
<!--
Tospecifyadditionalsecurity-roleelements,addafileinthemerge
directorycalledejb-security-roles.xmlthatcontainsthem.
-->

<!-- methodpermissions -->
<!--
Tospecifyadditionalmethod-permissionelements,addafileinthemerge
directorycalledejb-method-permissions.entthatcontainsthem.
-->

<!-- transactions -->
<!--
Tospecifyadditionalcontainer-transactionelements,addafileinthemerge
directorycalledejb-container-transactions.entthatcontainsthem.
-->

<!-- findertransactions -->

<!-- messagedestinations -->
<!--
Tospecifyadditionalmessage-destinationelements,addafileinthemerge
directorycalledejb-message-destinations.entthatcontainsthem.
-->

<!-- excludelist -->
<!--
Tospecifyanexclude-listelement,addafileinthemergedirectory
calledejb-exclude-list.xmlthatcontainsit.
-->
</ assembly-descriptor >

</ ejb-jar >

weblogic-ejb-jar.xml:

<? xmlversion="1.0"encoding="UTF-8" ?>

<! DOCTYPEweblogic-ejb-jarPUBLIC"-//BEASystems,Inc.//DTDWebLogic8.1.0EJB//EN""http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd" >

< weblogic-ejb-jar >
< description > <![CDATA[ GeneratedbyXDoclet ]]> </ description >
< weblogic-enterprise-bean >
< ejb-name > Hello </ 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 > ejb/Hello </ destination-jndi-name >
< initial-context-factory > weblogic.jndi.WLInitialContextFactory </ initial-context-factory >
< connection-factory-jndi-name > helloConnectionFactory </ connection-factory-jndi-name >
</ message-driven-descriptor >
< reference-descriptor >
</ reference-descriptor >

</ weblogic-enterprise-bean >
<!--
Toaddenterprisebeansthatyouhavedeploymentdescriptorinfofor,add
afiletoyourXDocletmergedirectorycalledweblogic-enterprise-beans.xmlthatcontains
the<weblogic-enterprise-bean></weblogic-enterprise-bean>markupforthosebeans.
-->

<!--
Toaddasecurity-role-assignmentsection,add
afiletoyourXDocletmergedirectorycalledweblogic-security-role-assignment.xmlthatcontains
the<security-role-assignment></security-role-assignment>markup.
-->

<!--
Toaddarun-as-role-assignmentsection,add
afiletoyourXDocletmergedirectorycalledweblogic-run-as-role-assignment.xmlthatcontains
the<run-as-role-assignment></run-as-role-assignment>markup.
-->

</ weblogic-ejb-jar >

打包ejb jar并发布。配置文件放在META-INF下。ejb jar 的结构如下:

测试代码:

public class EJBtest ... {
privatefinalstaticStringNAMING_FACTORY="weblogic.jndi.WLInitialContextFactory";
//QueueConnectionFactory'sfactoryclass,valuealwaysisjavax.jms.QueueConnectionFactory
privatefinalstaticStringQCON_FACTORY="javax.jms.QueueConnectionFactory";
//MessageDrivenBean'sJNDI-Name
privatefinalstaticStringMESSAGE_QUEUE="ejb/Message";

publicstaticvoidmain(String[]args)...{
QueueConnectionqCon
=null;
QueueSenderqSender
=null;
QueueSessionqSession
=null;

try...{

//CreateaJNDIinitialcontext
Propertiesprops=newProperties();
props.put(Context.INITIAL_CONTEXT_FACTORY,NAMING_FACTORY);
props.put(Context.PROVIDER_URL,
"t3://localhost:7001");
InitialContextic
=newInitialContext(props);

//LookupaConnectionFactoryinJNDI
QueueConnectionFactoryqCF=(QueueConnectionFactory)ic
.lookup(QCON_FACTORY);

//CreateaConnection
qCon=qCF.createQueueConnection();

//CreateaSession
qSession=qCon.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);

//LookuptheQueue
Queuequeue=(Queue)ic.lookup(MESSAGE_QUEUE);

//CreateaQueueSenderboundtotheQueue
qSender=qSession.createSender(queue);

//CreateaTextMessage
TextMessagemes=qSession.createTextMessage("HelloWorld!");

//StarttheQueue,sendthemessage
qCon.start();
for(inti=0;i<5;i++)...{
qSender.send(mes);
System.out.println(
"SendingHelloWorld-"+i
+"totheJMSQueue");
}

}
catch(NamingExceptione)...{
System.out.println(e);
}
catch(JMSExceptione)...{
e.printStackTrace();
}
finally...{
//ReleaseallJMSresources
try...{
qCon.close();
qSender.close();
qSession.close();
}
catch(JMSExceptionje)...{
System.out.println(je);
}

}


}


}


二。搭建spring web 项目环境,并配置applicationContext.xml如下:

<? xmlversion="1.0"encoding="UTF-8" ?>
<! DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd"
>
< beans >

< bean id ="connectionFactory"
class
="org.springframework.jndi.JndiObjectFactoryBean" >
< property name ="jndiName" >
< value > helloConnectionFactory </ value >
</ property >
< property name ="jndiTemplate" >
< ref local ="jndiTemplate" ></ ref >
</ property >
</ bean >

< bean id ="destination"
class
="org.springframework.jndi.JndiObjectFactoryBean" >
< property name ="jndiName" >
< value > ejb/Hello </ value >
</ property >
< property name ="jndiTemplate" >
< ref local ="jndiTemplate" ></ ref >
</ property >
</ bean >

< bean id ="jmsTemplate"
class
="org.springframework.jms.core.JmsTemplate" >
< property name ="connectionFactory" >
< ref local ="connectionFactory" />
</ property >
< property name ="defaultDestination" >
< ref local ="destination" />
</ property >
</ bean >

< bean id ="jndiTemplate"
class
="org.springframework.jndi.JndiTemplate" >
< property name ="environment" >
< props >
< prop key ="java.naming.factory.initial" >
weblogic.jndi.WLInitialContextFactory
</ prop >
< prop key ="java.naming.provider.url" >
t3://localhost:7001
</ prop >
</ props >
</ property >
</ bean >

</ beans >

web.xml:

<? xmlversion="1.0"encoding="UTF-8" ?>
<! DOCTYPEweb-appPUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" >
< web-app id ="WebApp_ID" >
< display-name > p_spring </ display-name >
< context-param >
< param-name > contextConfigLocation </ param-name >
< param-value > /WEB-INF/applicationContext.xml </ param-value >
</ context-param >
< listener >
< listener-class >
org.springframework.web.context.ContextLoaderListener
</ listener-class >
</ listener >
< welcome-file-list >
< welcome-file > index.html </ welcome-file >
< welcome-file > index.htm </ welcome-file >
< welcome-file > index.jsp </ welcome-file >
< welcome-file > default.html </ welcome-file >
< welcome-file > default.htm </ welcome-file >
< welcome-file > default.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >

weblogic.xml:

<? xmlversion="1.0"encoding="UTF-8" ?>
<! DOCTYPEweblogic-web-app
PUBLIC"-//BEASystems,Inc.//DTDWebApplication8.1//EN"
"http://www.bea.com/servers/wls810/dtd/weblogic810-web-jar.dtd"
>
< weblogic-web-app >
< jsp-descriptor >
< jsp-param >
< param-name > compileFlags </ param-name >
< param-value > -g </ param-value >
</ jsp-param >
< jsp-param >
< param-name > keepgenerated </ param-name >
< param-value > true </ param-value >
</ jsp-param >
</ jsp-descriptor >
< context-root > p_spring </ context-root >
</ weblogic-web-app >

编写spring调用ejb的测试类:

package com.mdb.ejb;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

/***/ /**
*HelloBeanClient,用于发送消息。
*/

public class HelloBeanClient ... {
protectedstaticfinalLoglog=LogFactory.getLog(HelloBeanClient.class);

publicstaticvoidmain(String[]args)...{
Resourceresource
=newClassPathResource("applicationContext.xml");
BeanFactoryfactory
=newXmlBeanFactory(resource);
JmsTemplatejt
=(JmsTemplate)factory.getBean("jmsTemplate");
try...{
for(inti=0;i<5;++i)...{
jt.send(
newMessageCreator()...{
publicMessagecreateMessage(Sessionsession)
throwsJMSException...{
returnsession
.createTextMessage(
"HelloWorld(MDB),ejb/Hello!,"
+System.currentTimeMillis());
}

}
);
}

}
catch(Exceptione)...{
e.printStackTrace();
}

log.info(
"成功发送消息!");
}

}


至此编码工作全部完成,整个项目的结构如下图:

三。在weblogic控制台

配置JMS 文件存储:



配置JMS 服务器:



配置JMS 队列:



配置JMS 连接工厂:



四。运行测试代码,运行结果如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值