#眉標=Oracle
#副標=Oracle 10g開發專題(11)
#大標=建立、測試與部署EJB (上)
#作者=文/何致億
========程式========
程式1 MySessionEJB.java:
package demo;
import javax.ejb.EJBObject;
public interface MySessionEJB extends EJBObject {
}
========程式========
========程式========
程式2 MySessionEJBBean.java
package demo;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class MySessionEJBBean
implements SessionBean {
public void ejbCreate() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void ejbRemove() {
}
public void setSessionContext(
SessionContext ctx) {
}
}
========程式========
========程式========
程式3 MySessionEJBHome.java:
package demo;
import javax.ejb.EJBHome;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
public interface MySessionEJBHome
extends EJBHome {
MySessionEJB create() throws RemoteException,
CreateException;
}
========程式========
========程式========
程式4 ejb-jar.xml:
Session Bean ( Stateless )
MySessionEJB
MySessionEJB
demo.MySessionEJBHome
demo.MySessionEJB
demo.MySessionEJBBean
Stateless
Container
MySessionEJB
*
Required
========程式========
========程式========
程式5 orion-ejb-jar.xml:
========程式========
========程式========
public String SayHello(String person) {
return null;
}
========程式========
========程式========
public String SayHello(String person) {
return “Hello, “ + person;
}
========程式========
========程式========
String SayHello(String person) throws RemoteException;
========程式========
========程式========
// mySessionEJB.SayHello( java.lang.String person );
========程式========
========程式========
System.out.println( mySessionEJB.Hello(“Rich”) );
========程式========
========程式========
程式6
package demo;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import ejbdemo.MySessionEJB;
import ejbdemo.MySessionEJBHome;
import javax.naming.NamingException;
public class MySessionEJBClient {
public static void main(String [] args) {
MySessionEJBClient mySessionEJBClient =
new MySessionEJBClient();
try {
Context context = getInitialContext();
MySessionEJBHome mySessionEJBHome =
(MySessionEJBHome) PortableRemoteObject.narrow(
context.lookup("MySessionEJB"),
MySessionEJBHome.class);
MySessionEJB mySessionEJB;
// Use one of the create() methods below
// to create a new instance
============以下為粗體字==========
mySessionEJB = mySessionEJBHome.create();
============以上為粗體字==========
// Call any of the Remote methods below
// to access the EJB
============以下為粗體字==========
System.out.println( mySessionEJB.SayHello("Rich"));
============以上為粗體字==========
} catch(Throwable ex) {
ex.printStackTrace();
}
}
private static Context getInitialContext()
throws NamingException {
// Get InitialContext for Embedded OC4J.
// The embedded server must be running
//for lookups to succeed.
return new InitialContext();
}
}
========程式========
========程式========
程式7
false
true
MySessionEJB
-
Project Output
========程式========
========程式========
MyFirstEJB
========程式========
========程式========
System.out.println("Calling MySessionEJB.SayHello()
on Standalone OC4J.");
System.out.println(mySessionEJB.SayHello("Rich"));
========程式========
========程式========
程式8
package demo;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import ejbdemo.MySessionEJB;
import ejbdemo.MySessionEJBHome;
import javax.naming.NamingException;
import java.util.Hashtable;
public class MySessionEJBClient1 {
public static void main(String [] args) {
MySessionEJBClient1 mySessionEJBClient1
= new MySessionEJBClient1();
try {
Context context = getInitialContext();
MySessionEJBHome mySessionEJBHome =
(MySessionEJBHome)PortableRemoteObject.narrow(
context.lookup("MySessionEJB"),
MySessionEJBHome.class);
MySessionEJB mySessionEJB;
// Use one of the create() methods below
// to create a new instance
mySessionEJB = mySessionEJBHome.create();
// Call any of the Remote methods below
// to access the EJB
============以下為粗體字==========
System.out.println("Calling MySessionEJB.SayHello()" +
" on Standalone OC4J.");
System.out.println(mySessionEJB.SayHello("Rich"));
============以上為粗體字==========
} catch(Throwable ex) {
ex.printStackTrace();
}
}
private static Context getInitialContext()
throws NamingException {
Hashtable env = new Hashtable();
// Standalone OC4J connection details
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.evermind.server.rmi.RMIInitialContextFactory");
env.put(Context.SECURITY_PRINCIPAL, "admin");
env.put(Context.SECURITY_CREDENTIALS, "admin");
env.put(Context.PROVIDER_URL, "ormi://Rich/MySessionEJB");
return new InitialContext(env);
}
}
========程式========
========程式========
Calling MySessionEJB.SayHello() on Standalone OC4J.
Hello, Rich
========程式========