Java EE 6???????GlassFish v3??EJB????????????????EJB 3.1??????????EJB 3.1?EJB 3.0?????????????????????????????EJB 3.1 ?????????????????????????????????????????

EJB 3.1???????????????????????????????????????????EJB? ???(Embeddable EJB Container)?JNDI???????(portable global JNDI name)?EOD(Ease Of Development)????????????????????????????????GlassF ish v3????Embeddable EJB Container??????????????????

Embeddable EJB Container????????????

EJB???3.0??POJO????????????????????????????????EJB ????????????????????????EJB?????JDBC?JMS?????????J NDI????????????????????JTA????????????????????EJB? ?????????????????????????????????????????????????? ?Java SE???????????????????????????????????????????JDBC? ??????Spring Framework?DriverManagerDataSource???????JPA?persit ence-unit???transaction-mode="JTA"??transaction-mode="RESOURCE_LOCAL"???????????UserTransaction???????????????????????? ?????????????????????????????????????????????????? ????????Embeddable EJB Container??EJB????????????????????EJB????????????? ????Java SE??????????????????

Embeddable EJB Container??????????Embedded JBoss?OpenEJB?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????EJB 3.1?Embeddable EJB Container????????????????API????EJB??????????????? ???????API???????????

Embeddable EJB Container????

Embeddable EJB Container????????????????????????javax.ejb.embeddable.EJBContainer????createEJBContainer()????????????????close()??? ???????????

// EJB??????????? EJBContainer container = EJBContainer.createEJBContainer(); // EJB??????????? container.close();

EJBContainer?????????????Java EE 6??????????????????????????????CLASSPATH?????????? ?????GlassFish v3?????glassfish-embedded-static-shell.jar?????glassfish-embedded-all.jar???????????????????GlassFish v3????????????????????????????

$AS_INSTALL/glassfish/lib/embedded/glassfish-embedded-static-shell.jar

????NetBeans 3.8????Java EE 6??????????????????????????GlassFish v3??????????????JAR???????????????????????????



Embeddable EJB Container????????JUnit???????????????

package com.example; import java.util.HashMap; import java.util.Map; import javax.ejb.embeddable.EJBContainer; import javax.naming.NamingException; import junit.framework.Assert; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class CalcTest { static Calc calc; // ??????EJB static EJBContainer container; // EJB???????? /** ?????????? */ @BeforeClass public static void setUpClass() throws Exception { // EJB??????????? container = EJBContainer.createEJBContainer(); // ?????EJB????? calc = (Calc)container.getContext().lookup("java:global/classes/Calc"); } /** ?????????? */ @AfterClass public static void tearDownClass() throws Exception { // EJB??????????? container.close(); } @Before public void setUp() { } @After public void tearDown() { } /** Calc#add()???????? */ @Test public void testadd() { int x = 10; int y = 20; int ret = calc.add(x, y); System.out.printf("%d = %d + %d", ret, x, y); Assert.assertEquals(30, ret); } }

EJB???????EJBContainer#getContext()??lookup()????? ??????EJB 3.1??EJB?JNDI?????????????EJB?JNDI????"java:global"?????????????????????????????????EJB???????JNDI??? ?"java:module"????????"java:app"??????????EJB???????????Embeddable EJB Container?????????"java:global"???????????EJB????????????????????[1]?

NetBeans 6.8?EJBContainer????????EJB?JNDI???????

?????Embeddable EJB Container??EJB???????????EJB???JNDI???"java:global"???????????????????????????JUnit?????????????EJB?? ?JNDI??"java:global/classes/Calc"????EJB 3.1???????"java:global"????????JNDI???

java:global[/]//[!]

?????????EJB????????"classes"??????????????Embeddable EJB Container?????EJB??????????????EJB JAR???????????????????EJB????????????????????????E JBContainer????????????EJB???????????????????JNDI? ???????????????[2]?EJB?????????????????????EJB?????????????????????? ???EJB?????????????NetBeans 6.8?EJB???????????????????????????????????????${ba sedir}/build/classes???????JUnit??????????????????????????????? ???????EJB?????????????????EJB???????"classes"????????????

calc-ejb/build calc-ejb/build/classes calc-ejb/build/classes/com calc-ejb/build/classes/com/example calc-ejb/build/classes/com/example/Calc.class

?????EJB?calc-ejb.jar????????????????????"calc-ejb"?????????????????????????????EJB???????JNDI????"java:global/calc-ejb/Calc"?????????????????????????????????????EJB?????????? ?????????????????????????????????????????

  • ??EJB??????????????????????????????src/conf/ejb-jar.xml????????????classes/???????????????????ejb-jar.xml???????????"calc-ejb"??????? calc-ejb NetBeans???????build.xml????????????????project.pr operties?build.classes.dir???????????????????????? ??EJB?????????????????? --- nbproject/project.properties.orig 2010-04-13 12:00:23.000000000 +0900 +++ nbproject/project.properties 2010-04-13 12:01:26.000000000 +0900 @@ -1,4 +1,4 @@ -build.classes.dir=${build.dir}/classes +build.classes.dir=${build.dir}/calc-ejb build.classes.excludes=**/*.java,**/*.form,**/.nbattrs build.dir=build build.ear.classes.dir=${build.dir}/classes


?????????????????????????????????????????JNDI????E JB?????????????

// ?????EJB????? calc = (Calc)container.getContext().lookup("java:global/calc-ejb/Calc");

???????????EJB????EJB??????EJB JAR????????????????????????JNDI????EJB??????EAR??? ?????????????????JNDI???"java:global///"??????????????????????JNDI?????????????EAR???????? ??????????????????EJBContainer.APP_NAME??????creat eEJBContainer(Map)????????????????????

Map env = new HashMap(); // ?"calc-app"?????????????????? env.put(EJBContainer.APP_NAME, "calc-app"); container = EJBContainer.createEJBContainer(env); // ="calc-app"????JNDI??EJB???????? calc = (Calc)container.getContext().lookup("java:global/calc-app/calc-ejb/Calc");

????JNDI?????EJBContainer.APP_NAME???????????????? ??????????CLASSPATH??????EJB?????????????????????? ??????????EAR?????????????????EJB????????????????? ???????????EJB3??????????CLASSPATH????????????[3]?

$ CLASSPATH=$AS_INSTALL/glassfish/lib/embedded/glassfish-embedded-static-shell.jar $ CLASSPATH=~/projects/calc-ejb/build/classes:~/projects/dummy-ejb/dist/dummy-ejb.jar:$CLASSPATH $ export CLASSPATH

NetBeans 3.8????????????????????????????????????????EJB3??? ????????????????



Embeddable EJB Container????????????????

?????EJB 3.1?????????????EJB??????????????????????????EJB 3.1????????EJB 3.1 Lite?????????????????EJB 3.1 Lite???????EJB 3.1????21.1??pp.596?Table 7??????????????????????EJB 3.1 Lite?????????JDBC DataSource?JTA??????????????????transaction-type="JTA"?JPA 2.0??????????????????????????????????????????????? ?

?????????????EJBContainer#getContext()???????????? ?????????????????EJBContainer??????????????Initial Context??lookup()????????????????JPA?????DAO?????? ??????????????????????????JTA????????JPA????DAO??? ????????????????

container = EJBContainer.createEJBContainer(); // ???????????????InitialContext??????????? InitialContext ctx = new InitialContext(); DataSource ds = ctx.lookup("jdbc/__default");

???GrassFish v3?Embeddable EJB Container?glassfish-embedded-static-shell.jar?????????EJB Timer Service?Felix OSGi?????????????????????????????JMS?MDB??????????????????????

[1] ??????EJB???????"java:module"?"java:app"???????????JNDI?lookup()??????

[2] EJBContainer???????????????EJB??????????, EJB 3.1????"JSR 318: Enterprise JavaBeansTM,Version 3.1"?22.2.1 EJBContainer, pp.608?????????????????
By default, the embeddable container searches the JVM classpath(the value of the Java System property java.class.path) to find the set of EJB modules for initialization. A classpath entry is considered a matching entry if it meets one of the following criteria:

  • It is an ejb-jar according to the standard module-type identification rules defined by the Java EE platform specification
  • It is a directory containing a META-INF/ejb-jar.xml file or at least one .class with an enterprise bean component-defining annotation
Each matching entry is considered an EJB module within the same application.[105] If an ejb-jar.xml is present the module-name element defines the module name. Otherwise, for ejb-jars the module name is the unqualified file name excluding the ?.jar? extension and for directories the mod- ule name is the unqualified name of the directory (the last name in the pathname?s name sequence). The embeddable container is not required to support more than one matching entry with the same module name.

[3] EAR???JNDI???????????????EJBContainer.APP_NAME???? ???????EJB 3.1????"JSR 318: Enterprise JavaBeansTM,Version 3.1"?22.2.2.3 javax.ejb.embeddable.appName, pp.610?????????????????
This property specifies an application name for the EJB modules executing within the embeddable con- tainer. If specified, the property value applies to the portion of the portable global JNDI name syntax. It is recommended that this property be set whenever an embeddable container is executed with more than one ejb module.

The property name is defined as EJBContainer.APP_NAME.

????????????????????????JSR 318?????????Kenneth Saks??????????EAR???JNDI??????????????????EJB????????? ??????????????????????????????????



Read More about [EJB 3.1 Embeddable EJB Container improved JUnit-testability...