Tuesday, 15 February 2011

java - Run-time lookup for EJBs in MDB consuming immediately after deploy -



java - Run-time lookup for EJBs in MDB consuming immediately after deploy -

i have java ee 5 project using jboss 5.1 , problem this. have kind of run-time lookup ejbs in mdbs using string results message content. it's kind of service locator pattern used in mdbs. now, since mdbs start consuming after deploy, have lot namenotfoundexception since implicit deployment order doesn't work here (run-time lookup). think it? possible using ejb 3.0? it's acceptable me utilize vendor-specific stuff (jboss 5.1) if resolves problem.

some code snippet visualize situation:

@messagedriven(mappedname="jms/queue") public class messagebean implements messagelistener { @resource private messagedrivencontext mdc; public void onmessage(message msg) { final string beanname = // extract somehow bean's name 'msg' final context ctx = new initialcontext(); final object obj = ctx.lookup(beanname); // namenotfoundexception // 'obj' } }

use 1 of these 4 different approaches.

declare ejb dependencies (ejb references) using "@ejb" annotation (don't utilize jndi lookup). entity bean references, must refer entity bean home interface. container must ensure dependencies injected before methods/message-listeners processed:

messagedriven(mappedname="jms/queue") public class messagebean implements messagelistener {

@ejb private entitybeanhomea entitybeanhomea; @ejb private entitybeanhomeb entitybeanhomeb; @ejb private entitybeanhomec entitybeanhomec; @ejb private sessionbeand sessionbeand; @resource private messagedrivencontext mdc; public void onmessage(message msg) { final string beanname = // extract somehow bean's name 'msg' final object obj = getdependentejb(beanname); // 'obj' } private object getdependentejb(string beanname) { object result = null; if ("entitybeanhomea".equals(beanname)) { result = entitybeanhomea; else if ("entitybeanhomeb".equals(beanname)) { result = entitybeanhomeb; else ("entitybeanhomec".equals(beanname)) { result = entitybeanhomec; else ("sessionbeand".equals(beanname)) { result = sessionbeand; } homecoming result; }

}

use jndi lookup, declare ejb dependencies via ejb deployment descriptors. again, container must ensure ensure dependencies setup before methods/messages processed:

@messagedriven(mappedname="jms/queue") public class messagebean implements messagelistener {

// given in original question...

}

deployment descriptor:

<enterprise-beans> <message-driven> ... <ejb-name>messagebean</ejb-name> <ejb-class>com.company.pkg.messagebean</ejb-class> <messaging-type>javax.jms.messagelistener</messaging-type> <message-destination-type>javax.jms.queue</message-destination-type> <message-destination-link>expenseprocessingqueue</message-destination-link> <ejb-ref> <description> reference ejb 2.1 entity bean encapsulates access employee records. </description> <ejb-ref-name>ejb/emplrecord</ejb-ref-name> <ejb-ref-type>entity</ejb-ref-type> <home>com.wombat.empl.employeerecordhome</home> <remote>com.wombat.empl.employeerecord</remote> <ejb-link>employeerecord</ejb-link> <-- if in same ejb jar --> <-- ../emp/emp.jar#employeerecord if in diff ejb jar --> </ejb-ref> <ejb-local-ref> <description> reference local business interface of ejb 3.0 session bean provides payroll service. </description> <ejb-ref-name>ejb/payroll</ejb-ref-name> <local>com.aardvark.payroll.payroll</local> <ejb-link>payroll</ejb-link> </ejb-local-ref> <ejb-local-ref> <description> reference local business interface of ejb 3.0 session bean provides pension plan service. </description> <ejb-ref-name>ejb/pensionplan</ejb-ref-name> <local>com.wombat.empl.pensionplan</local> <ejb-link>pensionplan</ejb-link> <-- if in same ejb jar --> </ejb-local-ref> ... </message-driven> ... </enterprise-beans>

use jndi lookup not declare dependencies using either @ejb annotations or ejb deployment - handle exclusively own logic, without container helping. utilize delays/error handling.

use jboss proprietary configuration command deployment order:

http://texnoblog.wordpress.com/2010/09/16/depends-in-jboss/

how order deployment of ejbs , jms queue config in jboss 5?

java java-ee jboss jboss5.x

No comments:

Post a Comment