Article Source: Silicon Valley Power Author: Mainstay
1. Introduction
EJB (Enterprise JavaBeans) is a relatively advanced content in Java programming, and it is also the threshold for Java programmers to advance from entry level to expert level. A significant difference between SUN Certified Java Programmer (SCJP) and SUN Certified Java Developer (SCJD) is that SCJP does not take the EJB test, while SCJD does. As more and more enterprises adopt the J2EE platform to develop e-commerce application systems, EJB development has become a problem that today's Java programmers must face.
This article first introduces the basic principles of general distributed object applications with a routine, and then starts with basic downloading, installation, and configuration, and gradually introduces the method of EJB programming, making learning EJB an easy and interesting thing. .
2. Typical distributed object program
Whether it is CORBA or RMI, the strategies for implementing distributed objects are similar. We can use a simple program example to simulate the composition of a distributed object program.
This example simulates the process of remotely requesting object properties. There is a remote object Dog on the network, and now we need to get its name (strName) attribute. The program sets up a stub (Dog_Stub) class on the client side and starts a skeleton (dog_Skeleton) class on the server side. Both classes implement the Dog interface. Dog_Stub and Dog_Skeleton communicate remotely through Socket. When the client program DogClient sends 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 through Socket. After the Dog_Skeleton object receives this string, it will The content executes the getName() method of the DogServer object to obtain the name of the Dog, and then returns it to the DogStub object through 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 knows that this process has passed through the network. It only knows that the request to obtain the name attribute has obtained a satisfactory result. That’s all.
In fact, the implementation of CORBA or Java RMI is similar to this, but it is far less simple. This program is useful for illustrating the execution mechanism of distributed object applications.
The program source code is as follows:
FileDog.java
public interface Dog
{
public String getName() throws Exception;
}/* Dog */
FileDogClient.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 */
FileDogServer.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 */
FileDog_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 running this distributed object program, first run DogServer, and then run DogClient on the client to see the results.