Friday, February 26, 2010

Getting started : First EJB3 program

hello All,
I tried to explore EJB3-JPA few days back.After couple of days of debugging, searching and trial-and-error, I was able to successfully execute my first program.I would like to share this experience to other java developers.

Tools and technologies used:
MyEclipse IDE 6.5 ,EJB3,jboss-5.0.1.GA,Hibernate 3,Jdk 1.6 & Mysql 5.

Refer EJB3 tutorial by Sebastian :http://www.laliluna.de/ejb-3-tutorial-jboss.html


Now, I will mention all the steps which requires to make this tutorial example execute!

-First of all you need to make sure that your java classpath (JAVA_HOME) has been set inside MyEclipse IDE.


-Set JBOSS_HOME and and also set Jboss related values inside MyEclipse. Try to make sure that you are able to start/stop
Jboss server through MyEclipse.

-Download example source code of tutorial from site http://www.laliluna.de/ejb-3-tutorial-jboss.html, for your easy
reference.

-I used Mysql database to test this example, need to download and copy suitable mysql driver jar file and place it to
'JBOSS_HOME\default\lib'. Make sure you have created database named 'ejb3ProjectDS' as mentioned as JTA datasource inside 'persistence.xml'.

-After you set up project in MyEclipse, you need to add few more liberaries to buildpath to make sure that example works.
Create a 'lib' folder and add it to build-path. Copy following Jars to lib folder :
commons-beanutils-1.7.0.jar, commons-collections.jar, commons-digester.jar,commons-fileupload-1.2.1.jar, commons-io-1.4.jar, commons-lang-2.3.jar, commons-logging.jar, commons-validator.jar ejb3-persistence.jar, hibernate-annotations.jar hibernate-entitymanager.jar, hibernate3.jar, jakarta-poi.jar jboss-archive-browsing.jar, jboss-ejb3-ext-api.jar jboss.jar, jbossall-client.jar, log4j.jar, mysql-connector-java-5.0.8-bin.jar

(Note- you will find most of from 'JBOSS_HOME\common\lib' and rest of from apache-commons library).
Also configure you project's build-path by adding 'JBOSS_HOME\lib' and 'JBOSS_HOME\client' by adding as external class folder.

-As we are using Mysql5 database, the mentioned entity bean Id-generator is throwing error and not able to create DB table during deployment.

To resolve modify 'Book' class as follows:
change-1
---------------
@Entity @Table(name="book") @SequenceGenerator(name = "book_sequence", sequenceName = "book_id_seq") @TableGenerator( name="book_id", table="primary_keys", pkColumnName="key1", pkColumnValue="book", valueColumnName="value1") public class Book implements Serializable {
change-2
-----------------
@Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "book_id") public Integer getId() { return id; }
Keep rest of the code- as it is.

-Next step, downloaded tutorial is not having any file 'ejb-jar.xml'. So create it,it is just empty implementation.
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">
-Open 'Persistence.xml' file, we need to modify it little bit as per our database details.

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> org.hibernate.ejb.HibernatePersistence java:/ejb3ProjectDS

-Now our EJB application is ready to deploy.Deploy the application as mentioned in tutorial and start the jboss server.After successful deployment,open JMX-console try to view JNDI view. You will see 'BookTestBean' inside Global JNDI Namespace.
-Connect to Mysql database through any Mysql client tool for eg. 'SQLYog'. You will see that two tables have been created within 'ejb3ProjectDS' database. - Now let's execute Client code. To execute it your need to setup hibernate.properties, jndi.properties and log4j.properties in classpath of Client class.

see sample hibernate.properties file :
hibernate.connection.driver_class=com.mysql.jdbc.Driver hibernate.connection.url=jdbc:mysql://localhost:3306/ejb3ProjectDS hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.username=root hibernate.connection.password=YYYYY hibernate.show_sql="true" hibernate.archive.autodetection="class"
see sample jndi.properties file :
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces java.naming.provider.url=jnp://localhost:10099

Note- the port number inside 'java.naming.provider.url' depends on your Jboss server settings. see port number of 'jboss:service=Naming' : inside file 'bindings.xml' at path 'JBOSS_HOME\server\default\conf\bootstrap'

see sample log4j.properties file :
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.rootLogger=debug, stdout
-After all these steps the project package structure will be like as per screenshot :
-Set up run-time configuration for Client code. Include all libraries of 'JBOSS_HOME\client' in build-path of program 'FirstEJB3TutorialClient'

-Execute the Client program.

Hurray! You successfully executed your first EJB3 program !!

No comments: