Weitere ähnliche Inhalte
Ähnlich wie EJB Part-1 (20)
Kürzlich hochgeladen (20)
EJB Part-1
- 1. Java Training Center
(No 1 in Training & Placement)
1
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
RMI
(Remote Method Invocation)
1. RMI is distributed technology, which is first attempt of SUN to provide a
solution to distributed programming. RMI use socket. A socket is a transport
mechanism.
2. No extra software is required for developing a distributed application using
RMI other than JDK.
RMI provides a way for a java program on one machine to communicate with
object residing in different JVM.
The important part of RMI architecture are the Stubclass object serialization
and Skelton class.
RMI uses a layered architecture where each of layers can be enhanced without
affected the other layers.
=:-Developing First RMI application-:=
Server Side Steps:-
1. Define a Business Service Interface (B.S.I.).
a. Your Business Service interface has to extends java.rmi.Remote interface.
b. Each Method inside the Business interface has to throw
java.rmi.RemoteException.
2. Provide the implementation class for Business Service Interface.
a. Business Services implements class has to extends “UnicastRemoteObject”
if not extend then implement class is responsible for correct
implementation of hashCode(), equals(), toString().
b. B.S.I. class has to implement Business Interface.
3. Create the Business Service Implement Service Implementation class object.
4. Bind the B.S.I. class object inside RMI registry or JNDI registry.
- 2. Java Training Center
(No 1 in Training & Placement)
2
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
-: Steps in Eclipse :-
1. Create a java Project with the name RMIServer.
2. Create package com.jtcindia.rmi
3. Write the following three files in your package.
a. AccountService.java (interface)
b. AccountServiceImpl.java
c. Server.java (for binding object)
If you have more than one Business class for other purpose than that class
object will create in server.java also.
Client Side Steps :-
1. Locate and get the registry reference.
2. Look Up the registry and get the required remote interface stub.
3. Invoke any required business operation on remote Business stub.
1. Create a java Project called RMI.
2. Create a package java.jtcindia.rmi;
3. Write the following in the Package.
a. Client.java
b. AccountServices.java(copy from the server)
Way to create jar file :-
D:/test> Jar cvf jtc.jar com
(Here AccountServices.class file change to jar file)
- 3. Java Training Center
(No 1 in Training & Placement)
3
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
File Required:-
1. Package com.jtcindia.rmi;
Import java.rmi.Remote;
Import java.rmi.RemoteException;
Public interface AccountService extends Remote{
Public double getBal(int accno) throws RemoteException;
}
2. AccountServiceImpl.java
packagecom.jtcindia.rmi;
importjava.rmi.*;
importjava.rmi.server.UnicastRemoteObject;
publicclassAccountServiceImplextendsUnicastRemoteObject
implementsAccountService {
publicAccountServiceImpl() throwsRemoteException {
super();
}
publicdouble getBal(int accno) throws
RemoteException{
System.out.println("getBal() with accno :
"+accno);
return 9999.99;
}}
RemoteException :-This is declare as an interface because in this
there are so many private data available. You have interact with
their subclass method also to avoid data duplication
- 4. Java Training Center
(No 1 in Training & Placement)
4
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
3. Server.java :-
packagecom.jtcindia.rmi;
importjava.rmi.registry.LocateRegistry;
importjava.rmi.registry.Registry;
public class Server {
public static void main(String[] args) {
try {
Registry reg = LocateRegistry.createRegistry(6789);
AccountServiceasi = new AccountServiceImpl();
reg.bind("ASI", asi);
System.out.println("Server is Running");
} catch (Exception e) {
e.printStackTrace();
}
}
}
4. Client.java :-
packagecom.jtcindia.rmi;
importjava.rmi.registry.LocateRegistry;
importjava.rmi.registry.Registry;
1. Here we need to write a default constructor which throws
RemoteException because in super class UnicastRemoteObject there
is a default constructor without throws ServerException.
2. A remote object is one whose method can be invoke from another
JVM.
- 5. Java Training Center
(No 1 in Training & Placement)
5
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
public class Client {
public static void main(String[] args) {
try {
Registry reg = LocateRegistry.getRegistry("localhost", 6789);
AccountService as = (AccountService) reg.lookup("ASI");
doublebal = as.getBal(1);
System.out.println(bal);
bal = as.getBal(2);
System.out.println(bal);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Binding the instance
of the remote object
to the RMI registry.
- 6. Java Training Center
(No 1 in Training & Placement)
6
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
=: EJB2/EJB :=
For Developing any Enterprise Edition you need mainly three types
of services.
EJB is a remote, distributed multi-tier system. An EJB
application server provide mainly services like transaction
management, Security Management, Object Package which
simplifies the programming effect.
For any Enterprise Edition development you need these Low
Level Services Commonly which you can get freely from EJB.
High level Service is nothing but your application code business
logic which will change from Enterprise edition to enterprise
edition.
IMP -: To run RMI application you need “JDK” where to run EJB
application you need “application server”.
Java Training Center
(No 1 in Training & Placement)
6
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
=: EJB2/EJB :=
For Developing any Enterprise Edition you need mainly three types
of services.
EJB is a remote, distributed multi-tier system. An EJB
application server provide mainly services like transaction
management, Security Management, Object Package which
simplifies the programming effect.
For any Enterprise Edition development you need these Low
Level Services Commonly which you can get freely from EJB.
High level Service is nothing but your application code business
logic which will change from Enterprise edition to enterprise
edition.
IMP -: To run RMI application you need “JDK” where to run EJB
application you need “application server”.
Java Training Center
(No 1 in Training & Placement)
6
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
=: EJB2/EJB :=
For Developing any Enterprise Edition you need mainly three types
of services.
EJB is a remote, distributed multi-tier system. An EJB
application server provide mainly services like transaction
management, Security Management, Object Package which
simplifies the programming effect.
For any Enterprise Edition development you need these Low
Level Services Commonly which you can get freely from EJB.
High level Service is nothing but your application code business
logic which will change from Enterprise edition to enterprise
edition.
IMP -: To run RMI application you need “JDK” where to run EJB
application you need “application server”.
- 7. Java Training Center
(No 1 in Training & Placement)
7
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
Following are various Application servers available
1. JBOSS(“Red Hat” Linux)
2. Web Logic(“BEA”, Oracle Company)
3. Web sphere(IBM)
Steps to develop and run EJB 2.0 application with JBOSS 4.0
Java Training Center
(No 1 in Training & Placement)
7
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
Following are various Application servers available
1. JBOSS(“Red Hat” Linux)
2. Web Logic(“BEA”, Oracle Company)
3. Web sphere(IBM)
Steps to develop and run EJB 2.0 application with JBOSS 4.0
Java Training Center
(No 1 in Training & Placement)
7
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
Following are various Application servers available
1. JBOSS(“Red Hat” Linux)
2. Web Logic(“BEA”, Oracle Company)
3. Web sphere(IBM)
Steps to develop and run EJB 2.0 application with JBOSS 4.0
- 8. Java Training Center
(No 1 in Training & Placement)
8
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
Steps to install JBOSS 4.0 :-
1. Click on installer called jems-installer-1.2.0 CRI.jar which is
available in JBOSS-4.0.
2. Select English and OK.
3. Click on next.
4. Click on next.
5. Accept license agreement and next.
6. Provide the location of installation E:/Jboss 4.0.5
7. Click OK on popup.
8. Select the profile all and click on next.
9. Click on next.
10.Provide the server name jtcindia and next.
11. Configure the default and click next.
12. Click next.
13. Provide admin username and password only and click on
next.
14. Click on next.
15. Click on done.
After installing successfully you can see the directory :-
- 9. Java Training Center
(No 1 in Training & Placement)
9
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
-: Steps to integrate JBOSS 4.0 with MyEclipse :-
1. Click on window Preferences
2. My Eclipse workbench Servers JBOSS JBOSS 4.x
Integrate means only enable the Plugin.
In “Eclipse” you have to download all the plugin and then
enable if.
3. Select Enable
jBoss home directory: E:/jboss-4.0.5
Server name: jtcindia
Optional program argu:
Optional shutdown arg: -shutdown
4. Expend the JBOSS 4.x
Java Training Center
(No 1 in Training & Placement)
9
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
-: Steps to integrate JBOSS 4.0 with MyEclipse :-
1. Click on window Preferences
2. My Eclipse workbench Servers JBOSS JBOSS 4.x
Integrate means only enable the Plugin.
In “Eclipse” you have to download all the plugin and then
enable if.
3. Select Enable
jBoss home directory: E:/jboss-4.0.5
Server name: jtcindia
Optional program argu:
Optional shutdown arg: -shutdown
4. Expend the JBOSS 4.x
Java Training Center
(No 1 in Training & Placement)
9
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
-: Steps to integrate JBOSS 4.0 with MyEclipse :-
1. Click on window Preferences
2. My Eclipse workbench Servers JBOSS JBOSS 4.x
Integrate means only enable the Plugin.
In “Eclipse” you have to download all the plugin and then
enable if.
3. Select Enable
jBoss home directory: E:/jboss-4.0.5
Server name: jtcindia
Optional program argu:
Optional shutdown arg: -shutdown
4. Expend the JBOSS 4.x
- 10. Java Training Center
(No 1 in Training & Placement)
10
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
JDK
Launch
Paths and select the JDK option. And do the following to
add the JDK.
a. Click on add button.
b. Click on Apply.
5. Select the “Launch” under JBOSS 4.x… Run mode and click
Apply.
6. Click on OK of Preference window.
a. By default Port no: 8080 because it take default of
tomcat which is running on 8080.
b. Change that Port no:- if get any error like JVM_8080
i.e. lifecycleException.
-: Stating JBOSS :-
Change the Port number: 5555.
Start the JBOSS from myEclipse.
Open the browser and type http://localhost:5555/imx-console/
Enter username & password on popup window.
-: Creating EJB Project in myEclipse :-
1. File new “EJB Project”
Project name: - E2Hello4JB and select J2EE1.4-EJB2.1 and finish.
2. You can see the following directory structure.
- 11. Java Training Center
(No 1 in Training & Placement)
11
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
New otherXMLXML(Basic Tem)ejb-jar.xmlnextcreate xml file
from DTD select category.
To deployselect the Project name and gotodeploy(8th
) Icon. Select
Project E2Hello4JB add to your application server jBoss.
-//sun microsystem, Inc.//DTD Enterprise JavaBeans 3.0
--: Create client Project for EJB :-
1. Create a “Java Project” E2client4JB.
Java Training Center
(No 1 in Training & Placement)
11
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
New otherXMLXML(Basic Tem)ejb-jar.xmlnextcreate xml file
from DTD select category.
To deployselect the Project name and gotodeploy(8th
) Icon. Select
Project E2Hello4JB add to your application server jBoss.
-//sun microsystem, Inc.//DTD Enterprise JavaBeans 3.0
--: Create client Project for EJB :-
1. Create a “Java Project” E2client4JB.
Java Training Center
(No 1 in Training & Placement)
11
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
New otherXMLXML(Basic Tem)ejb-jar.xmlnextcreate xml file
from DTD select category.
To deployselect the Project name and gotodeploy(8th
) Icon. Select
Project E2Hello4JB add to your application server jBoss.
-//sun microsystem, Inc.//DTD Enterprise JavaBeans 3.0
--: Create client Project for EJB :-
1. Create a “Java Project” E2client4JB.
- 12. Java Training Center
(No 1 in Training & Placement)
12
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
Example:-
File required:-
1. HelloRemote.java
2. HelloBean.java
3. HelloHome.java
4. Ejb-jar.xml
5. Jboss.xml
6. HelloClient.java
Java Training Center
(No 1 in Training & Placement)
12
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
Example:-
File required:-
1. HelloRemote.java
2. HelloBean.java
3. HelloHome.java
4. Ejb-jar.xml
5. Jboss.xml
6. HelloClient.java
Java Training Center
(No 1 in Training & Placement)
12
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
Example:-
File required:-
1. HelloRemote.java
2. HelloBean.java
3. HelloHome.java
4. Ejb-jar.xml
5. Jboss.xml
6. HelloClient.java
- 13. Java Training Center
(No 1 in Training & Placement)
13
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
1. HelloHome.java
package com.jtcindia.ejb2;
importjava.rmi.*;
importjavax.ejb.*;
publicinterfaceHelloHomeextendsEJBHome{
publicHelloRemote create() throws createException,
RemoteException;
}
2. HelloRemote.java
package com.jtcindia.ejb2;
importjava.rmi.*;
importjavax.ejb.EJBObject;
publicinterfaceHelloRemoteextendsEJBObject{
public String getMessage(String name)
throwsRemoteException;
}
3. HelloBean.java
package com.jtcindia.ejb2;
importjavax.ejb.*;
publicclassHelloBeanextendsSessionBean{
SessionContentsc=null;
Public void setSessionContent(SessionContentsc){
System.out.println("setSesssionContent");
this.sc=sc;
- 14. Java Training Center
(No 1 in Training & Placement)
14
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
}
Public void ejbActivate(){
System.out.println("ejbActivate");
}
publicvoidejbPassivate(){
System.out.println("ejbPassivate()");
}
publicvoidejbRemove(){
System.out.println("ejbReamove");
}
publicvoidejbCreate(){
System.out.println("ejbCreate()");
}
publicStringgetMessage(String name){
String msg="Hi"+name+"welcome to JTC EJB with
JBOSS";
System.out.println(msg);
returnmsg;
}
}
4. HelloClient.java
package com.jtcindia.ejb2;
importjava.util.*;
importjavax.naming.*;
public classHelloClient {
publicstaticvoid main(String[] args) {
try{
Properties p=newProperties();
- 15. Java Training Center
(No 1 in Training & Placement)
15
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
p.put("java.naming.factory.initial","org.jnp.interf
aces.NamingContentFactory");
p.put("java.naming.provider.url","localhost:1099");
p.put("java.naming.factory.url.package","org.jboss.
naming");
Contentctx=newInitialContext(p);
Object o=ctx.lookup("JTCHelloHomeJNDI");
HelloHomehh=(HelloHome)o;
HelloRemotehr=hh.create();
String msg=hr.getMessage("Som");
System.out.println(msg);
msg=hr.getMessage("Som");
System.out.println(msg);
}catch(Exception e){
e.printStackTrace();
}
}
}
5. ejb-jar.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEejb-jarPUBLIC"-//Sun MicroSystem,INC.//DTD
Enterprise JavaBean
2.0//EN","http://Java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>HelloEJB</ejb-name>
<home>com.jtcindia.ejb2.HelloHome</home>
<remote>com.jtcindia.ejb2.HelloRemote</remote>
- 16. Java Training Center
(No 1 in Training & Placement)
16
RMI Part 1
Author: SomPrakashRai www.jtcindia.org
Copyright©JTC
<ejb-class>com.jtcindia.ejb2.HelloBean</ejb-class>
<session-type>Stateless</session-type>
<trasaction-type>Container</trasaction-type>
</session>
</enterprise-beans>
</ejb-jar>
6. jboss.xml
<jboss>
<enterprise-beans>
<session>
<ejb-name>HelloEJB</ejb-name>
<jndi-name>JTCHelloHomeJNDI</jndi-name>
</session>
</enterprise-beans>
</jboss>