Pages

Saturday, November 16, 2013

Beginning EJB3 with JBoss and MySQL

Here, I am going to show how to begin with EJB and deploy in JBoss server. For this example, I am using MySQL database.

Prerequisites for this example:
- JBoss
- Eclipse
- MySQL

The first step involves the work in JBoss server side.

- Copy jboss\docs\examples\jca\mysql-ds.xml to jboss\server\default\deploy and edit as in the xml below.
- Copy mysql-connector-java-.jar to jboss\server\default\lib.

Let’s run eclipse and create an EJB project. File-->New-->EJB Project. 



Click on ‘New Runtime’ button.


Select Jboss 5.0 and click Next.


Browse JBoss root directory and click ‘Finish’.


Click Finish.

Add a file named persistence.xml in ejbModule/META-INF.
Add an entity class User in com.test.user.domain package.


Add a Session Bean(New-->Session Bean(EJB 3.x)).


It will create a session bean and a remote interface.


Run As --> Run on Server

Which shows our service is up and running.

While deploying, if you get an error due to AttachmentStore,you may need to change the content of jboss/server/default/conf/bootstrap/profile.xml. Look for the definition of the AttachmentStore, and change the constructor line so that it starts:
The original one doesn't have the class="java.io.File" attribute. 


Now, let’s start with the client.Create a test class with main method as follows.  
Here, JNDI lookup can be performed in two ways. 

1. By setting properties in the Java class. 

As commented above 
Or 

2. By adding jndi.properties file in src folder. 

 jndi.properties:


Run UserTest as a Java application.

Output: User has been created successfully.

1 comment:

  1. Casino Online - DrmCAD
    The casino is a trusted online 충주 출장안마 destination for all your favourite casino 공주 출장마사지 games. 익산 출장마사지 We've tested over 3000 창원 출장안마 slots and hundreds of other casino games. 남양주 출장샵 Get your Sign Up Bonus now!

    ReplyDelete