Getting Started with EJB3-JPA
Create Simple Project EJB (Entity, Session,Client Java, Web Service) on JBOSS Server As 7.1 with Eclipse (Luna):
Prerequisites
Before performing this project, you need to meet the following prerequisites:
Eclipse (Luna) Java EE IDE for Web Developers :
- Download Eclipse Luna from follownig link : https://eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/lunar
- In order to handle our JBoss from Eclipse first we need to install JBoss Tools plug-in in the eclipse Marketplace
- Help >eclipse Marketplace > Jboss Tools
JBOSS 7.1.1 installation on Windows 7 :
- Download JBOSS7.1.1 Final from following link http://www.jboss.org/jbossas/downloads/
- Extract jboss-as-7.1.1.Final.Zip file on C:\ Drive , like c:\JEE\jboss-as-7.1.1.Final
Configuration JBOSS in Eclispe :
Add new Server Runtime Environement
window –> Preferences –> select Server –> Runtime Environment —> add
Start the server : Jboss server was not working. On server.log it was saying port 9990 was used. Here is the output;
To fix this, go to services.msc and find NVIDIA Network Service (NvNetworkService) and stop it.
Restart the server.
When show Administration Console we need to creat users to be acces the console admin
use command add-user.bat : create username and password
To be access in the console admin put your username and your password.
Adding a MySQL datasource to JBoss :
The connector can be downloaded from: http://dev.mysql.com/downloads/connector/j/.
Copy the driver jar to the new directory and move to that directory: jboss-as-7.1.1.Final/modules/com/mysql/main
The module.xml contains basically the module name, the resources used and its dependencies.
Now the module has been created :
We need to make a reference to it from the main application server configuration file : /standalone/configuration/standalone.xml.
The module name, com.mysql
, should match the directory structure for the module
Find the ‘drivers’ element and add a new driver to it:
Start the Jboss Server
Now try running the application server. Hopefully, amongst the application server’s console output you should see the following [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool — 27) JBAS010404: Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver (version 5.1)
To be sure, the directory structure for the module added new file mysql-connector-java-5.1.21.jar.index.
The JDBC driver is now installed and set up as a core module, and is available to be referenced by application datasources.
Add the Datasource in Administration Console
Profile –> connector –> DataSources
Select the JBDC Driver : mysql
put the property attributes name and JNDI Name
Configuration the connection: you should create a database in our tutorial named mydb.
Test the JDBC connection : we must to enable the datasource
You can see the configuration data source in the file standlone.xml
Create Project EJB:
Create class Entity Product
Create the file persistence.xml
Run the project on the server
The application server’s console output you should see the following :
The project was deployed and the tabe Product are created !!!
Create the interface IProductRemote:
Create the interface IProductLocal:
Create class EJBProductImpl implements IProductLocal and IProductRemote:
The application server’s console output you should see the following
Create new Project Java :
Create class ClientEJB to test the Project
In order to run this application you need to place a file named jboss-ejb-client.properties in the client classpath
Last thing, if you are running your client from Eclipse, you need to place an additional library named jboss-client.jar in the client CLASSPATH. (The library is located into jboss-as-7.1.1.Final/bin/client folder):
You need also to add the project EJBProject.
As you can see from the following picture :
Run the class as Java Application, in the console output you should see the following
As you can see the client can add new Product !!!
Web Service
Create class Product Service
Run the project as Full Publish :
The application server’s console output you should see the following :
http://localhost:8080/EJBProject/ProductService?wsdl
you can download the project by following this link https://github.com/AMhadhbi/EJBProject
I hope it will be useful for you all.
Enjoy it 🙂
Leave a comment