Using the Kodo EJB in the Java EE environment

Source: Internet
Author: User
Tags bind commit contains interface mysql version xmlns mysql database

Kodo EJB is a framework that supports object/relationship mapping, and Kodo EJB, in addition to supporting the lightweight persistence layer framework in common Java applications, supports the need to meet heavyweight enterprise applications in Java EE containers, as required by the EJB3 specification, taking full advantage of Java The superior features provided in the EE container, such as container management transactions, remote access.

Applications based on Kodo EJB Development support the use of EJB or JCA standard access to the Java EE environment:

    1. Jca

      The Kodo EJB supports the JCA1.0 standard, so applications based on Kodo EJB development can be easily published to the Java EE Application Server as well as other JCA resources.

    2. Jndi

      Another way is to bind an instance of Kodo.persistence.EntityManagerFactoryImpl to Jndi and then use the Kodo EJB application by looking for Jndi. While this approach requires developers to write code based on different Java EE containers, this approach can maintain the maximum portability of Java EE containers and create the possibility of using Kodo EJBs in Java EE containers that do not support JCA standards.


In this article, we will use a simple example to simply explain and demonstrate how to access the Kodo EJB application on the WEBLOGIC9 via a jndi approach.

   Environmental Preparedness

Since Kodo is a framework based on annotation mechanisms, we must use JDK5.0 to complete the development effort. So before downloading, installing Kodo, make sure you have downloaded and installed JDK5.0.

To demonstrate the need, we choose the MySQL database as a persistent target database, please go to www.mysql.com download the latest MySQL database installation.

   Install Kodo

Kodo's latest version is Kodo 4.0.0 Early Access 4, now you can go to http://www.solarmetric.com/to download the trial version, download need to register, you will get 30 days of license.

Extract the downloaded compressed file into the C:/kodo4 directory (followed by using%kodo_home% to refer to this directory) and open%kodo_home%/bin/ Kodocmd.cmd file, set the Kododir to your Kodo installation directory, and set Jdkhome to the Java installation directory.

   Install WEBLOGIC9

Kodo EJB requires EJB containers to support the EJB3 standard when running in Java EE environments, and Bea's newest WEBLOGIC9 server supports the EJB3 standard, so we chose WEBLOGIC9 as the target server to use for the demo. You can download Weblogic9 to http://www.bea.com and then install WEBLOGIC9 on your own machine.

   [note]The example in this article is the implementation of a remotely accessible EJB instance, so you can install WEBLOGIC9 on other machines, although the client calls to add some environment variables, please refer to the following instructions.
Developing Kodo EJB Applications

Due to the length of the relationship, we directly use"Kodo EJB: A persistence layer framework conforming to the EJB3 specification "A good example has been created in the article, including environmental preparation, persistence class creation, database creation, and so on, as far as possible in the following chapters"Kodo EJB: A persistence layer framework conforming to the EJB3 specification "One of the steps already mentioned in the article, but focuses on the work that needs to be done in extra detail.

In EJB development, we usually use the session Fa?ade design pattern, so the following example uses this design pattern to encapsulate Kodo EJB applications.

We use a stateless session bean to encapsulate all operations on the book class, because the Kodo EJB application requires developers to complete Kodo.persistence.EntityManagerFactoryImpl instances to Jndi binding, we have this part of the work in the session Bean completed, the following is the session Bean's interface and all the code for implementation, note the added annotations in the code that will help you understand how the Kodo EJB works.

   Bookbean Class

The following is the source code for the Bookbean class, please pay special attention to the Setsessioncontext method in the Bookbean class, where the code will
The Kodo.persistence.EntityManagerFactoryImpl instance is bound to Jndi.

Package org.vivianj.kodo.examples.ejb.stateless; Import java.rmi.RemoteException; Import java.util.Collection; Import javax.ejb.EJBException; Import Javax.ejb.SessionBean; Import Javax.ejb.SessionContext; Import Javax.naming.InitialContext; Import javax.naming.NamingException; Import Javax.persistence.EntityManager; Import Javax.persistence.EntityManagerFactory; Import Javax.persistence.PersistenceContextType; Import Javax.persistence.Query; Import kodo.persistence.KodoPersistence; Import Org.vivianj.kodo.examples.beans.Book;  /** * Bookbean provides the implementation class of the session bean/public class Bookbean implements Sessionbean {protected sessioncontext;  Private Entitymanagerfactory EMF; /** * Getbookbyid to find, return a qualified book Object * * @param ID * of the book object based on the id attribute of the book object * @return the book objects with ID number * @thro  WS remoteexception/public book Getbookbyid (int id) throws RemoteException {/* Gets an EJB entity manager/Entitymanager em =   EMF. Createentitymanager (persistencecontexttype.extended); try {    /* Start transaction/em.gettransaction (). Begin ();    /* Processing business/book book = Em.find (Book.class, id);    /* End Transaction/em.gettransaction (). commit ();   return book;   Finally {/* Shut down the EJB entity Manager/Em.close (); }/** * Updatebook update Book Object info * @param book * Update book Object * @throws remoteexception/public VO   ID updatebook (book book) throws RemoteException {/* entity manager for EJB/Entitymanager em = Emf.getentitymanager ();    try {/* Start transaction/em.gettransaction (). Begin ();    /* Processing Business * * Em.merge (book);   /* End Transaction/em.gettransaction (). commit ();   Finally {/* Shut down the EJB entity Manager/Em.close (); The/** * Createbook method is used to persist the new book Object * @param book * The persisted book Object * @throws remoteexception * * * publi   c void Createbook (book book) throws RemoteException {/* entity manager for EJB/Entitymanager em = Emf.getentitymanager ();    try {/* Start transaction/em.gettransaction (). Begin ();    /* Processing Business * * em.persist (book); /* END transaction */Em.gettransactioN (). commit ();   Finally {/* Shut down the EJB entity Manager/Em.close (); Throws RemoteException {/* The Entity manager for EJB Deletebook/entitymanager em = Emf.getentityman   Ager ();    try {/* Start transaction/em.gettransaction (). Begin ();    /* Processing business/Query q = em.createquery ("Delete from book C where c.id =: id");    Q.setparameter ("id", book.id);    Q.executeupdate ();   /* End Transaction/em.gettransaction (). commit ();   Finally {/* Shut down the EJB entity Manager/Em.close (); } public Collection Getbooks (String querystring) throws RemoteException {/* The Entity manager for the EJB/entitymanager em = EMF   . Getentitymanager ();    try {/* Start transaction/em.gettransaction (). Begin ();    /* Processing business/List Allbooks = Em.createquery (querystring). Getresultlist ();    /* End Transaction/em.gettransaction (). commit ();   return allbooks;   Finally {/* Shut down the EJB entity Manager/Em.close ();   } public void Setsessioncontext (Sessioncontext ctx) {this.ctx = CTX; try {* * get Java EE container context environment EJbcontext = new InitialContext ();        Object o = null;    try{/* Find an instance of Kodo.persistence.EntityManagerFactoryImpl based on Jndi */o = ejbcontext.lookup ("ejb/kodo/emf"); }catch (Exception e) {/* If not found, attempt to bind an instance of Kodo.persistence.EntityManagerFactoryIm pl to Jndi * * Entitymanagerfactor     Y EMFFOREJB = persistence.createentitymanagerfactory (null);    Ejbcontext.bind ("ejb/kodo/emf", EMFFOREJB);    } if (o = = null) {/* If not found, try to find the * emf = (entitymanagerfactory) ejbcontext.lookup ("ejb/kodo/emf") again;    else {emf = (entitymanagerfactory) o;   The catch (Namingexception e) {throw new RuntimeException (e); The public void Ejbcreate () throws RemoteException {} is public void Ejbactivate () throws Ejbexception, RemoteException {} public void Ejbpassivate () throws Ejbexception, remoteexception {} public void Ejbremove () throws Ejbexception, R  emoteexception {} public void Unsetsessioncontext () {CTX = null;  } }

Bookhome interface

Package org.vivianj.kodo.examples.ejb.stateless; Import java.rmi.RemoteException; Import javax.ejb.CreateException; Import Javax.ejb.EJBHome;  the/** * Bookhome class provides the home interface for the session bean/public  interface Bookhome extends EJBHome {  

Bookremote interface

Package org.vivianj.kodo.examples.ejb.stateless; Import java.rmi.RemoteException; Import java.util.Collection; Import Javax.ejb.EJBObject; Import Org.vivianj.kodo.examples.beans.Book; /**  * Bookremote class provides remote interface for session bean/Public  interface Bookremote extends Ejbobject {public book  Getb Ookbyid (int id) throws remoteexception;  public void Updatebook (book book) throws RemoteException;  public void Createbook (book book) throws RemoteException;  public void Deletebook (book book) throws RemoteException;  

  writing an EJB descriptor file

In order to publish the session bean to WEBLOGIC9, We also need to provide two profiles: Ejb-jar.xml and Weblogic-ejb-jar.xml, and in the demo example provided by the author, the two files are as follows, and the developer can adjust according to their actual circumstances.

Ejb-jar.xml

<?xml version= "1.0" encoding= "UTF-8"?> <ejb-jar xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "http://" Www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd "version=" 2.1 "> <display-name>sample Kodo ejb</ display-name> <enterprise-beans> <session> <ejb-name>BookEJB</ejb-name>

Weblogic-ejb-jar.xml

<?xml version= "1.0"?> <weblogic-ejb-jar xmlns= "http://www.bea.com/ns/weblogic/90"   Http://java.sun.com/xml/ns/j2ee "  xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "  xsi: schemalocation= "Http://www.bea.com/ns/weblogic/90  http://www.bea.com/ns/weblogic/90/weblogic-ejb-jar.xsd" >  <weblogic-enterprise-bean>   <ejb-name>BookEJB</ejb-name>   <jndi-name> Ejb/kodo/book</jndi-name>  

Packaged deployment

EJB packaging and common EJB packaging based on Kodo EJB is nothing special, the deployment process is nothing special, the simpler way is to use the Configuration tool to create a new domain, and then copy the packaged EJB jar files directly to the Autodeploy directory under the domain directory.

Complete package deployment process No more details here, if you are not very familiar, please refer to WebLogic server's online Help documentation http://edocs.bea.com/wls/docs91/index.html.

Here's how to configure the two parts of Kodo after you create a WEBLOGIC9 domain:

    1. Install Kodo

      After you create the WEBLOGIC9 domain under the Windows platform, you can find the Setdomainenv.cmd file in the bin directory under the directory that contains the domain. Open the file to find the set pre_classpath= line, where the jar file will be added to the WEBLOGIC9 server's startup CLASSPATH, so we add all the jar files in the%kodo_home%/lib directory to the Pre_ In Classpath. The following is the author's settings (not fully completed, please complete the developer according to the actual situation).

      Set pre_classpath= F:/opensource/kodo-4.0.0ea4/lib/kodo.jar; F:/opensource/kodo-4.0.0ea4/lib/jta-spec1_0_1.jar; F:/opensource/kodo-4.0.0ea4/lib/jca1.0.jar; F:/opensource/kodo-4.0.0ea4/lib/jdo-2.0.jar;

      Optionally, you can put the database server's drive jar and other application-required jar files under this variable.
    1. Provide Kodo of license

      Kodo need to provide license files when used, in the Kodo EJB: A persistence layer framework conforming to the EJB3 specification, We know that Kodo's license information is stored in the Kodo.xml file in the application's Meta-inf directory, and is used in conjunction with the application, but in the Java EE environment, We are binding the instance of Kodo.persistence.EntityManagerFactoryImpl to the Jndi service, the process is done by the server, placing license in the EJB package does not allow the server to obtain license information, view Kodo help documentation, nor See the corresponding Implementation guide, but after testing the author found that the following method can do this part of the work.
      1. Prepare a License.jar file
        The License.jar contains a meta-inf directory containing kodo.xml files that provide Kodo license. The structure of the License.jar file is as follows:



      2. Add the jar file to the classpath in the WebLogic domain

        Refer to the "Install Kodo" step of the previous step to add the License.jar file to the classpath of the WebLogic domain.

Test

Now, start the server, write a simple test code to see if the EJB can start working properly, and the following code can be used to test the lookup of the required book object from the EJB and print its Name property.

/* Provide WebLogic Server information */hashtable<string,string> H = new hashtable<string,string> ();  H.put (Context.initial_context_factory,      "weblogic.jndi.WLInitialContextFactory");  H.put (Context.provider_url, "t3://localhost:7001"); /* Get the context path of the specified server/*  


When executing the client, place the jar files in the%kodo_home%/lib directory and the Server/lib/weblogic.jar files under the WebLogic Server installation directory in Classpath.

  Summarize

In the EJB3 standard, the EJB3 application can be used both in the Java SE Environment and also in the Java EE environment. The Kodo EJB provides two different ways to support use in Java EE environments: JCA and Jndi, where the JCA approach is simpler and the JNDI approach provides greater flexibility.

In this article, the author presents a simple example of how to bind an instance of Kodo.persistence.EntityManagerFactoryImpl in a Kodo EJB to a jndi in Weblogic9, and access the Kodo through Jndi The persistence class in an EJB application.

  Reference Resources:

EJB3 specification:http://jcp.org/aboutJava/communityprocess/pfd/jsr220/index.html
JDO2 specification:http://jcp.org/aboutJava/communityprocess/pfd/jsr243/index.html
Kodo online Documentation:http://solarmetric.com/kodo/Documentation/4.0.0EA4/docs/full/html/index.html
WEBLOGIC9 online Documentation:http://edocs.bea.com/wls/docs91/index.html

  Download Resources :Kodoejb-javaee.jar



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.