Top Banner
Enterprise Java JNDI Enterprise Naming Conext (ENC) and Injection
25

Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

Nov 16, 2014

Download

Documents

Orfeo Morello

Example about jndi configuration in Jboss 4.x
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

EnterpriseJava

JNDI Enterprise Naming Conext

(ENC)and

Injection

Page 2: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 2

EnterpriseJava

Goals

• Configure applications using the JNDI Enterprise Naming Context (ENC)

Page 3: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 3

EnterpriseJavaObjectives

• JNDI ENC Overview• JNDI ENC Lookup• XML Population• Annotation Population• Dependency Injection

Page 4: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 4

EnterpriseJavaJNDI ENC Overview

• Component's individual address space• Holds references to values and objects in the

environment• Theoretical equivalent to Unix soft links

– ln -s <some physical file path> $HOME/<my logical path>• Object's registered

– EJB interfaces (local and remote)– JMS connection factories– JMS destinations– data sources– any JCA resource– primitive values

Page 5: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 5

EnterpriseJavaJNDI ENC and Injection

• Objects are placed in the component's JNDI ENC by the container

• Components may look these values up– using raw JNDI calls– using SessionContext convenience method– allows any nested component to also get the environment

• Components may have values injected into properties/fields.

– fewer lines of code dedicated to lookups– inversion of control

Page 6: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 6

EnterpriseJavaReading JNDI ENC

• JNDIInitialContext jndi = new InitialContext();object = jndi.lookup(“java:comp/env/encname);

• SessionContextSessionContext ctx;...object = ctx.lookup(“encname”);

• Note: “java:comp/env” does not work with JBoss 4.0.xGA EJB3

– use “java:comp.ejb3/env” or SessionContext in the short term

– works with 4.2.xGA

Page 7: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 7

EnterpriseJavaExample: Deployed JNDI Tree

• Local JNDI Namespace+- ejavaDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)

• Global JNDI Namespace– default names +- jndiDemoEAR (class: org.jnp.interfaces.NamingContext)

| +- Hospital (class: org.jnp.interfaces.NamingContext)

| | +- local (... ejava.examples.jndidemo.ejb.HospitalLocal ...)

| | +- remote (... ejava.examples.jndidemo.ejb.HospitalRemote ...)

– custom-configured JNDI names (jboss.xml) +- ejava (class: org.jnp.interfaces.NamingContext)

| +- examples (class: org.jnp.interfaces.NamingContext)

| | +- jndidemo (class: org.jnp.interfaces.NamingContext)

| | | +- AidScheduler (class: org.jnp.interfaces.NamingContext)

| | | | +- local (... ejava.examples.jndidemo.ejb.AidSchedulerLocal

| | | | +- remote (... ejava.examples.jndidemo.ejb.AidSchedulerRemote

Page 8: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 8

EnterpriseJavaXML Population: Example POJO Style

//@Local declared by ejb-jar.xml entrypublic interface HospitalLocal extends Scheduler {}

//@Remote declared by ejb-jar.xml entrypublic interface HospitalRemote extends Scheduler {}

//@Stateless(name=”Hospital”) declared by ejb-jar.xml entrypublic class HospitalEJB extends SchedulerBase implements HospitalLocal, HospitalRemote { public String getName() { return "HospitalEJB"; }

@Resource public void setSessionContext(SessionContext ctx) { this.ctx =

ctx; }}

//@Local declared by ejb-jar.xml entrypublic interface AidSchedulerLocal extends Scheduler {}

//@Remote declared by ejb-jar.xml entrypublic interface AidSchedulerRemote extends Scheduler {}

Page 9: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 9

EnterpriseJavaXML Population: Example POJO Style

//@Stateless(name=”AidScheduler”) declared by ejb-jar.xml entrypublic class AidSchedulerEJB extends SchedulerBase implements AidSchedulerLocal, AidSchedulerRemote { private EntityManager em; private DataSource ds; private String message; private HospitalLocal hospital; @Resource //in super - protected SessionContext ctx; public void setSessionContext(SessionContext ctx) {

this.ctx = ctx; } public void init() { log.info("************* AidScheduler Created ************"); log.debug("ctx=" + ctx); log.debug("ejb/hospital=" + ctx.lookup("ejb/hospital")); log.debug("message=" + message); log.debug("em=" + em); log.debug("ds=" + ds); log.debug("hospital=" + hospital); } public String getName() { return "AidScheduler"; } }

Page 10: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 10

EnterpriseJavaXML Population: Example Output

[AidSchedulerEJB] ******************* AidScheduler Created ******************

DEBUG [AidSchedulerEJB] ctx=org.jboss.ejb3.BaseSessionContext@5f6ac6

DEBUG [AidSchedulerEJB] ejb/hospital=Hospital

DEBUG [AidSchedulerEJB] message=Hello Helping World

DEBUG [AidSchedulerEJB] em=org.jboss.ejb3.entity.TransactionScopedEntityManager@16fa2a5

DEBUG [AidSchedulerEJB] ds=org.jboss.resource.adapter.jdbc.WrapperDataSource@ede19e

DEBUG [AidSchedulerEJB] hospital=Hospital

DEBUG [JNDIHelper] listing java:comp/env

...

listing java:comp/env

+jdbc :org.jnp.interfaces.NamingContext

----myds :javax.naming.LinkRef

+ejb :org.jnp.interfaces.NamingContext

----hospital :javax.naming.LinkRef

+persistence :org.jnp.interfaces.NamingContext

----jndidemo :org.jboss.ejb3.entity.TransactionScopedEntityManager

+vals :org.jnp.interfaces.NamingContext

----message :java.lang.String

Page 11: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 11

EnterpriseJavaXML Population: META-INF/ejb-jar.xml

<?xml version="1.0"?>

<ejb-jar

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"

version="3.0">

<enterprise-beans>

...

<session>

<ejb-name>Hospital</ejb-name>

<remote>ejava.examples.jndidemo.ejb.HospitalRemote</remote>

<local>ejava.examples.jndidemo.ejb.HospitalLocal</local>

<ejb-class>ejava.examples.jndidemo.ejb.HospitalEJB</ejb-class>

</session>

</enterprise-beans>

</ejb-jar>

Page 12: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 12

EnterpriseJavaXML Population: META-INF/ejb-jar.xml

<enterprise-beans>

<session>

<ejb-name>AidScheduler</ejb-name>

<remote>ejava.examples.jndidemo.ejb.AidSchedulerRemote</remote>

<local>ejava.examples.jndidemo.ejb.AidSchedulerLocal</local>

<ejb-class>ejava.examples.jndidemo.ejb.AidSchedulerEJB</ejb-class>

<!-- REMAINDER OF EXAMPLE GOES HERE -->

<post-construct>

<lifecycle-callback-method>

init

</lifecycle-callback-method>

</post-construct>

</session>

Page 13: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 13

EnterpriseJavaXML Population: env-entry

<env-entry> <env-entry-name>vals/message</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>Hello Helping World</env-entry-value> <injection-target> <injection-target-class> ejava.examples.jndischeduler.ejb.AidSchedulerEJB </injection-target-class> <injection-target-name>message</injection-target-name> </injection-target> </env-entry>

Page 14: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 14

EnterpriseJavaXML Population: ejb-local-ref

<ejb-local-ref> <ejb-ref-name>ejb/hospital</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <local>ejava.examples.jndischeduler.ejb.HospitalLocal</local> <ejb-link>Hospital</ejb-link> <injection-target> <injection-target-class> ejava.examples.jndischeduler.ejb.AidSchedulerEJB </injection-target-class> <injection-target-name>hospital</injection-target-name> </injection-target> </ejb-local-ref>

Page 15: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 15

EnterpriseJavaXML Population: DataSource

<resource-ref> <res-ref-name>jdbc/myds</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> <injection-target> <injection-target-class> ejava.examples.jndidemo.ejb.AidSchedulerEJB </injection-target-class> <injection-target-name>ds</injection-target-name> </injection-target> </resource-ref>

Page 16: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 16

EnterpriseJavaXML Population: EntityManager

<persistence-context-ref> <persistence-context-ref-name> persistence/jndischeduler </persistence-context-ref-name> <persistence-unit-name>jndischeduler</persistence-unit-name> <persistence-context-type>Transaction</persistence-context-type> <injection-target> <injection-target-class> ejava.examples.jndischeduler.ejb.AidSchedulerEJB </injection-target-class> <injection-target-name>em</injection-target-name> </injection-target> </persistence-context-ref>

Page 17: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 17

EnterpriseJavaXML JNDI mapping META-INF/jboss.xml

<?xml version="1.0" encoding="UTF-8"?><jboss xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://www.jboss.org/j2ee/schema/jboss_5_0.xsd" version="3.0"> <enterprise-beans> <session> <ejb-name>AidScheduler</ejb-name> <jndi-name>

ejava/examples/jndidemo/AidScheduler/remote</jndi-name>

<local-jndi-name>ejava/examples/jndidemo/AidScheduler/local

</local-jndi-name> <resource-ref> <res-ref-name>jdbc/myds</res-ref-name> <jndi-name>java:/ejavaDS</jndi-name> </resource-ref> </session> </enterprise-beans></jboss>

Page 18: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 18

EnterpriseJavaExample: Deployed JNDI Tree

• Local JNDI Namespace+- ejavaDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)

• Global JNDI Namespace– default names +- jndiDemoEAR (class: org.jnp.interfaces.NamingContext)

| +- CookEJB (class: org.jnp.interfaces.NamingContext)

| | +- local (.. ejava.examples.jndidemo.ejb.CookLocal ...)

| +- BakeScheduler (class: org.jnp.interfaces.NamingContext)

| | +- remote (class: java.lang.Object)

| | +- remoteStatefulProxyFactory (... org.jboss.ejb3.ProxyFactory)

Page 19: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 19

EnterpriseJavaAnnotation Population: Example Entities

@Localpublic interface CookLocal extends Scheduler {}

@Statelesspublic class CookEJB extends SchedulerBase implements CookLocal { public String getName() { return "CookEJB"; }

@Resource protected void setSessionContext(SessionContext ctx) { super.ctx = ctx; }}

@Remotepublic interface BakeSchedulerRemote extends Scheduler {}

Page 20: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 20

EnterpriseJavaAnnotation Population: Example Java Class

@Stateful(name="BakeScheduler")@EJBs({ @EJB(name="ejb/cook", beanInterface=CookLocal.class, beanName="CookEJB")})@PersistenceContext(unitName="jndischeduler", name="persistence/jndischeduler", type=PersistenceContextType.EXTENDED)public class BakeSchedulerEJB extends SchedulerBase implements BakeSchedulerRemote {

@Resource(name=”persistence/jndischeduler”) private EntityManager em; @Resource(mappedName=”java:/ejavaDS”) private DataSource ds; @Resource protected void setSessionContext(SessionContext ctx) { super.ctx = ctx; }

Page 21: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 21

EnterpriseJavaAnnotation Population: Example Java Class

@Resource(name=”ejb/cook”) protected CookLocal cook; @Resource(name="vals/message") String message;

@PostConstruct public void init() {

log.info("********* BakeScheduler Created ********");

log.debug("ctx=" + ctx);

log.debug("ejb/cook=" + ctx.lookup("ejb/cook"));

log.debug("em=" + em);

log.debug("ds=" + ds);

log.debug("persistence/jndischeduler=" +

ctx.lookup("persistence/jndischeduler"));

log.debug("message=" + message);

log.debug("cook=" + cook);

}

}

Page 22: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 22

EnterpriseJavaAnnotation Population: Example Output

BakeSchedulerEJB] ******************* BakeScheduler Created ******************

[BakeSchedulerEJB] ctx=org.jboss.ejb3.BaseSessionContext@a1a602

[BakeSchedulerEJB] ejb/cook=CookEJB

[BakeSchedulerEJB] em=org.jboss.ejb3.entity.ExtendedEntityManager@16b3939

[BakeSchedulerEJB] ds=org.jboss.resource.adapter.jdbc.WrapperDataSource@155bd22

[BakeSchedulerEJB] persistence/jndidemo=org.jboss.ejb3.entity.ExtendedEntityManager@16b3939

[BakeSchedulerEJB] message=null

[BakeSchedulerEJB] cook=null

[BakeSchedulerEJB] cook2=CookEJB

[JNDIHelper]

listing java:comp/env

+env :org.jnp.interfaces.NamingContext

+ejb :org.jnp.interfaces.NamingContext

---cook :javax.naming.LinkRef

+ejava.examples.jndidemo.ejb.BakeSchedulerEJB :org.jnp.interfaces.NamingContext

---ds :javax.naming.LinkRef

+persistence :org.jnp.interfaces.NamingContext

---jndidemo :org.jboss.ejb3.entity.ExtendedEntityManager

Page 23: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 23

EnterpriseJavaDependency Injection

• fields– simple, less verbose

• property setter– easier to plug into unit tests– no need for getter– inheritance supported

Page 24: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 24

EnterpriseJavaSummary

• Populate– XML– Annotations

• Lookup– JNDI– SessionContext

• Property Assignment– Lookup– Dependency Injection

Page 25: Ejb3 JNDI Enterprise Naming Conext (ENC) and Injection

v081029 25

EnterpriseJava

References

• “Enterprise JavaBeans 3.0, 5th Edition”; Burke & Monsen-Haefel; ISBN 0-596-00978-X; O'Reilly