java hosting


horsebig.gif (6904 bytes) drivehead.gif (2283 bytes)

JavaRanch Cattle Drive - EJB

Many companies need server side programs that can handle huge loads and are ultra reliable. And there are many more companies that fantasize about being in such a position. :)

Knowing how to program Enterprise Java Beans (EJB's) makes you capable of writing programs can handle huge loads and are very reliable. The EJB server does all the hard stuff for you. You just have to worry about your business logic and following (xxx link) bean law.

The EJB server provides:

    Distributed computing (if your server gets loaded, add another!)

    Failover (if one server dies, the other picks up the slack!)

    Concurrancy (doing multiple things at the same time)

    Database connection pooling (makes your database stuff much faster)

    Transaction management (if there are any problems, everything can be reset)

If you were to try and mash all of this stuff into your programs, you would have to learn a lot more than what you need to learn to do EJB. Plus, a lot of these things are the places that even the most experienced developers have bugs. EJB makes it so that less experienced developers can develop quality applications that are used on a large scale.

Another way to look at it is that the EJB server vendor has done all the hard parts for you.

Let's get some EJB's running to make sure your environment is okay.

The good news is that you have already installed an EJB server: Orion! The bad news is that EJB requires making a lot of files.

Download this file and extract it into c:\java - this will create subdirectories test1 through test6. Each of these subdirectories contains the full source for a an EJB and a servlet that uses the EJB. Explanations about the why and how will come later. For now, let's just make sure that your EJB stuff is working.

These test programs assume that you have already done the servlet and JDBC assignments. If you haven't, you'll want to at least do the server tests from the servlet and JDBC cattle drive pages or else this stuff won't work.

For each of these tests there is a file called build.bat that will do all the work needed to compile, package and deploy the beans. You don't need to copy class files like you have been doing with servlets.

test1 - Plain EJB and servlets (no JavaRanch helper files). A very simple session bean.

    1) Pull up a console and go to c:\java\test1. Type build and press enter.

    2) Your application is now installed and ready to do its thing. We need to tell Orion about it.

    3) Add the following line to \orion\config\server.xml just above "</application-server>":

             <application name="oldmcdonald" path="../applications/oldmcdonald.ear" />
        
    4) Add the following line to \orion\config\default-web-site.xml just above "</web-site">:

             <web-app application="oldmcdonald" name="farm" root="/farm" />
        

    5) Orion is now all set! Direct your browser to http://localhost/farm/servlet/FarmServlet to see the secret message!

test2 - The same simple bean, made a little simpler because it is using JavaRanch helper files.

test3 - A similar session bean that uses JDBC

    1) run build

    2) Add the following to data-sources.xml

            <data-source
                         name="mysql"
                         class="com.evermind.sql.DriverManagerDataSource"
                         location         ="jdbc/mysqlCore"
                         pooled-location  ="jdbc/mysqlPooled"
                         xa-location      ="jdbc/xa/mysqlXA"
                         ejb-location     ="jdbc/DataSource"
                         username         =""
                         password         =""
                         url="jdbc:mysql://localhost/soup"
                         connection-driver="org.gjt.mm.mysql.Driver" />
        
    3) Invoke the following SQL statements in mySQL:

    
            USE SOUP;
    
            CREATE TABLE INVENTORY (TYPE VARCHAR(30) NOT NULL, QUANTITY INT);
    
            INSERT INTO INVENTORY (TYPE, QUANTITY) VALUES ('cows', 5);
            INSERT INTO INVENTORY (TYPE, QUANTITY) VALUES ('chickens', 22);
            INSERT INTO INVENTORY (TYPE, QUANTITY) VALUES ('pigs', 8);
        
    4) Copy the mm.mysql jar file to the c:\orion\lib directory.

    5) direct your browser to http://localhost/farm/servlet/FarmServlet

test4 - An entity bean.

test5 - Similar to test4, but you can also change data.

test6 - Adding new records to a table in the database, complete with unique ID's.

    1) run build

    xxx eieio\Animal.class needs to go in orion\lib\eieio - any way to put this in ear file?

    2) invoke the following SQL statements in mySQL

    
            CREATE TABLE BARN (
                ID INT UNIQUE, TYPE VARCHAR(30) NOT NULL, NAME VARCHAR(30) NULL, BREED VARCHAR(30) NULL, DESCRIPTION VARCHAR(200) NULL);
    
            INSERT INTO BARN (ID, TYPE, NAME, BREED, DESCRIPTION) VALUES (1, 'cow', 'Gertrude', 'Hereford', 'Halter broke. Very tame');
            INSERT INTO BARN (ID, TYPE, NAME, BREED, DESCRIPTION) VALUES (2, 'chicken', 'VanRenseleer', 'White Rock', 'Bossy');
            INSERT INTO BARN (ID, TYPE, NAME, BREED, DESCRIPTION) VALUES (3, 'chicken', 'Betty', 'Buff Orpington', 'Broody');
    
            CREATE TABLE TABLEIDCOUNTERS (TABLENAME VARCHAR(60) NOT NULL UNIQUE, COUNTER BIGINT, SKIP BIGINT);
    
            INSERT INTO TABLEIDCOUNTERS (TABLENAME, COUNTER, SKIP) VALUES ('BARN', 1, 100);
        
    3) direct your browser to http://localhost/farm/index.html

    4) watch the BARN table in mySQL for changes

xxx explain about packages.




Page maintained by Marilyn de Queiroz