How EJBS work

Source: Internet
Author: User
Tags flush socket stub throwable tostring

The first two days in this section of the essence of the Robbin on the principle of EJB call analysis, benefit is not shallow, but feel with plain text to express the effect seems not intuitive, and the RMI is also a little bit less elaboration. Here I based on their own experience, on the basis of Robbin post to say this topic, for your reference.

First of all, I want to talk about how RMI works, because EJBS are based on RMI after all. Not much nonsense, the essence of RMI is to implement the call between different JVMs, the working principle diagram is as follows:

It is implemented by opening a stub and skeleton in each of the two JVMs, which pass the socket communication to implement the parameter and return value.

The RMI example code can be found on the Internet, but most of them are through the extend the interface Java.rmi.Remote implementation, has been packaged very perfect, inevitably make people have mirrors feeling. The following example is what I see in the Enterprise JavaBeans, although very coarse, but very intuitive, conducive to quickly understand how it works.

1. Define a person's interface, with two business method, Getage (), and GetName () codes







2. Person's implementation Personserver class
Code





String name;

Public Personserver (String name, int.) {
This.age = age;
THIS.name = name;
}

public int getage () {
return age;
}

Public String GetName () {
return name;
}
}

3. OK, now we are going to call the two business method of Getage () and GetName () on the client machine, then we have to write the corresponding stub (client side) and skeleton (server side) program. This is the implementation of the stub:
Code





Import Java.net.Socket;

public class Person_stub implements person {
Socket socket;

Public Person_stub () throws Throwable {
Connect to Skeleton
Socket = new Socket ("computer_name", 9000);
}

public int getage () throws Throwable {
Pass method name to skeleton
ObjectOutputStream OutStream =
New ObjectOutputStream (Socket.getoutputstream ());
Outstream.writeobject ("Age");
Outstream.flush ();

ObjectInputStream instream =
New ObjectInputStream (Socket.getinputstream ());
return Instream.readint ();
}

Public String GetName () throws Throwable {
Pass method name to skeleton
ObjectOutputStream OutStream =
New ObjectOutputStream (Socket.getoutputstream ());
Outstream.writeobject ("name");
Outstream.flush ();

ObjectInputStream instream =
New ObjectInputStream (Socket.getinputstream ());
Return (String) instream.readobject ();
}
}

Note that person_stub, like personserver, implements person. They all implement Getage () and GetName () two business method, the difference is that Personserver is really implemented, Person_stub is to establish a socket connection and send a request to skeleton, and then through Skeleton invokes the Personserver method and finally receives the returned result.

4. Skeleton implementation
Code






Import Java.net.ServerSocket;

public class Person_skeleton extends Thread {
Personserver MyServer;

Public Person_skeleton (Personserver server) {
Get reference of Object Server
This.myserver = server;
}

public void Run () {
try {
New socket at Port 9000
ServerSocket serversocket = new ServerSocket (9000);
Accept stub ' s request

Socket socket = serversocket.accept ();

while (socket! = NULL) {

Get stub ' s request
ObjectInputStream instream =
New ObjectInputStream (Socket.getinputstream ());
String method = (string) instream.readobject ();

Check method name
if (Method.equals ("Age")) {
Execute Object Server ' s business method

int age = Myserver.getage ();

ObjectOutputStream OutStream =

New ObjectOutputStream (Socket.getoutputstream ());

return result to Stub

Outstream.writeint (age);

Outstream.flush ();

}

if (method.equals ("name")) {

Execute Object Server ' s business method
String name = Myserver.getname ();
ObjectOutputStream OutStream =
New ObjectOutputStream (Socket.getoutputstream ());

return result to Stub
Outstream.writeobject (name);
Outstream.flush ();
}
}
} catch (Throwable t) {
T.printstacktrace ();
System.exit (0);
}
}

public static void Main (String args []) {
New Object Server
Personserver person = new Personserver ("Richard", 34);

Person_skeleton Skel = new Person_skeleton (person);
Skel.start ();
}
}

Skeleton class extends from Thread, it runs in the background, at any time to receive the client sent request. and the corresponding business method is called according to the key sent over.

5. The last one, the implementation of the client
Code















The essence of client is that it needs to know the definition of the person interface, and instance a person_stub, call business method by stub, as to how the stub communicates with the server, the client will have no control.

Notice how it's written:
Person person = new person_stub ();
Instead of
Person_stub person = new person_stub ();

Why. Because to interface programming, hehe.

Thank you for your patience to see here, about RMI, I want to say so much. But it seems to have not written to the EJB, I was tired of a half-dead, forget, I still go to bed, tomorrow to continue ...

I have not used WebLogic, this is the combination of WebSphere to talk about the various classes of the call relationship.

Suppose we want to create a sessionbean that reads user information, we have 3 files to write:
1. Userservicehome.java
Home interface

2. Userservice.java
Remote interface

3. Userservicebean.java
Bean implementation

The Wsad will eventually generate 10 class. What are the other 7? Let's count it one by one:

4. _userservicehome_stub.java
This is of course the home interface in the client side (dynamic loading) Stub class, it implements Userservicehome.

5. _ejsremotestatelessuserservicehome_a940aa04_tie.java
The home interface on the server side of the skeleton class, "a940aa04" should be randomly generated, all other related class names will have this symbol string, tie is the name of CORBA to skeleton.

6. Ejsremotestatelessuserservicehome_a940aa04.java
The home interface is implemented on the server side, and of course it is implements Userservicehome.

7. Ejsstatelessuserservicehomebean_a940aa04.java
Called by the _userservice_stub, create. (Why can't you create _userservice_stub directly?) Let's talk about it later. )

8. _userservice_stub.java
The stub class of the remote interface on the client side (dynamically loaded). It implements UserService.

9. _ejsremotestatelessuserservice_a940aa04_tie.java
The skeleton class on the server side of the remote interface.

Ten. Ejsremotestatelessuserservice_a940aa04.java
The remote interface is implemented on the server side, and of course it is implements UserService. And it is responsible for invoking the business method of userservicebean--, the Bean implementation class we wrote.

So what is the call relationship between the classes? Simply put, it is two times the RMI loop.

Let's take a look at how the client-side program is written: code




InitialContext CTX = new InitialContext ();

The first step
Userservicehome home =
(Userservicehome) Portableremoteobject.narrow (
Ctx.lookup (jndistring),
Userservicehome.class);

Home: _userservicehome_stub
System.out.println (Home.tostring ());

Step Two
UserService object = Home.create ();

Ojbect: _userservice_stub
System.out.println (Object.ToString ());

Step Three
int userId = 1;
UserInfo UI = Object.getuserinfo (UserId);
}

After the first step, we get a userservicehome (interface)-defined object home, so what is the class instance of home? With the debug look, know that home is the original example of _userservicehome_stub.

From the second step, it is our focus, although only a simple line of code,
UserService object = Home.create ();
But the system behind him is how to do it. Let's go into the code to see:

1. Call Home.create ()
Code





2. Actually call _userservicehome_stub.create (), in this method, the Stub sends a create string to the skeleton:
Code





3. The server-side skeleton receives the request from the stub and calls the appropriate method:
Code













Code









4. Skeleton invokes the Create method of the Userservicehome server-side implementation class
Code







5. #4又调用EJSStatelessUserServiceHomeBean_a940aa04. Create ()
Code




At this point, we finally end the first RMI loop, and get the remote interface UserService Stub class _userservice_stub, that is, the result inside the # #.

Here is a question, why does the # # not directly create _userservice_stub, and then turn a # # of hands. Because # # extends from Ejswrapper, it does not have the ability to create stubs, so you must use #5,which extends from Ejshome to generate a stub. If not to generate this stub, it should be possible not to walk the # # this step.

OK, now we got the object which is instanceof _userservice_stub, and implements UserService

Now our client has come to the third step:
UserInfo UI = Object.getuserinfo (UserId);

Continue looking at the code and start the second RMI loop:

1. Call Object.getuserinfo ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.