Article Source: Silicon Valley Power Author: the mainstay
First, the introduction
EJB (Enterprise JavaBeans) is a relatively advanced content in Java programming, and also the threshold for Java programmers to advance from entry-level to master level. A notable difference between Sun's certified Java Programmer (SCJP) and Sun's certified Java Developer (SCJD) is that SCJP does not test EJBs, and Scjd to test. With more and more enterprises adopting Java-EE platform to develop e-business application system, EJB development has become a problem that Java programmers must face today.
This paper first introduces the basic principle of the general Distributed object application with a routine, then starts with the basic downloading, installation and configuration, and introduces the method of EJB program programming gradually, so that the learning of EJB becomes an easy and interesting thing.
ii. Typical Distributed object programs
Whether it is CORBA or RMI, the strategy for implementing distributed objects is similar, and we can simulate the composition of a distributed object program with a simple program example.
This example simulates the process of a remote Request object property. There is a remote object dog on the network and is now going to get its name (StrName) attribute. The program sets a stub (Dog_stub) class on the client and starts a skeleton (Dog_skeleton) class on the server side, all of which implement the Dog interface, dog_stub communicate with Dog_skeleton via the socket. When a client program dogclient a request to Dog_stub to obtain the name attribute, the Dog_stub object sends the method name "GetName ()" As a string to the remote Dog_skeleton object via the socket, Dog_ The skeleton object receives the string and then executes the Dogserver object's GetName () method based on the contents of the string, obtains the dog name, and then returns it to the Dogstub object via the socket. The entire process is implemented through the network, but for the client program dogclient, it does not know where the real dog object is, or even know that the process passed through the network, it only know the issue of the request to obtain the name attribute is satisfied with the result.
In fact, CORBA or Java RMI is implemented in a similar way, but it is far from simple. This program is useful for explaining the execution mechanism of a distributed object application.
The program source code looks like this:
File Dog.java
public interface Dog
{
Public String GetName () throws Exception;
}/* Dog * *
File Dogclient.java
public class Dogclient
{
public static void Main (string[] args) throws Exception
{
Dog Dog = new Dog_stub ();
String strName = Dog.getname ();
System.out.println ("Name:" + strName);
}//main ()
}/* dogclient * *
File Dogserver.java
public class Dogserver implements Dog
{
String StrName;
int intAge;
Public String GetName () throws Exception
{
return strName;
}//getname ()
Public Dogserver (String strnameinput)
{
StrName = Strnameinput;
}//dogserver ()
public static void Main (string[] args) throws Exception
{
New Dog_skeleton (New Dogserver ("TOMCAT"));
}//main ()
}/* Dogserver * *
File Dog_skeleton.java
Import java.io.*;
Import java.net.*;
public class Dog_skeleton extends Thread
{
static ServerSocket SS = NULL;
Dogserver ds;
Public Dog_skeleton (Dogserver dsinput) throws Exception
{
ds = Dsinput;
if (ss = null)
SS = new ServerSocket (8000);
This.start ();
}//dog_skeleton ()
Public synchronized void Run ()
{
Try
{
while (SS!= null)
{
Socket socket = ss.accept ();
ObjectInputStream ois = new ObjectInputStream (Socket.getinputstream ());
ObjectOutputStream oos = new ObjectOutputStream (Socket.getoutputstream ());
String strmethodname = (string) ois.readobject ();
if (Strmethodname.equals ("GetName ()"))
Oos.writeobject (Ds.getname ());
Oos.flush ();
Ois.close ();
Oos.close ();
Socket.close ();
}//while
}//try
catch (Exception e)
{
E.printstacktrace ();
}//catch
}//run ()
}/* Dog_skeleton * *
File Dog_stub.java
Import java.io.*;
Import java.net.*;
public class Dog_stub implements Dog
{
Socket socket;
ObjectOutputStream Oos;
ObjectInputStream Ois;
Public Dog_stub () throws Exception
{
Socket = new Socket ("Wudi", 8000);
Oos = new ObjectOutputStream (Socket.getoutputstream ());
Ois = new ObjectInputStream (Socket.getinputstream ());
}//dog_stub ()
Public String GetName () throws Exception
{
Oos.writeobject ("GetName ()");
Oos.flush ();
Return (String) ois.readobject ();
}//getname ()
}/* dog_stub * *
When you run the Distributed Object Program, run Dogserver first, and then run Dogclient on the client to see the results.