I have already reproduced a remote method call based on the WebLogic server, RMI technology, and now let's take a look at the implementation of the remote method invocation based on EJB3.0.
In fact, both are a similar principle in the WebLogic server as a transit point. Implement the transfer of the parameters and return values between the server and the client, and the only difference: RMI technology is written to the WebLogic servers by registering, and EJB3.0 directly, publishing an EJB project to WebLogic ... Very simple ....
Well, nonsense not much to say, on the code.
First create an EJB project in MyEclipse, and as for the creation process, I'm not going to go into detail. Inside the Ejbtest.java and Ejbtestremote.java These two Java files myeclipse automatically help you generate, there would have been a Ejbtestlocal.java file, but that file is used for local access, we are talking about remote access today, so not to use, I Just to delete it ~ ~ ~ ~ ~ ~ ~
First look at the code inside the Ejbtestremote.java
Package ejbtest;
/**
* Ejbtest local interface
/public interface ejbtestremote{public
string Gethello (string name); c5/>}
Very simply, it simply declares an interface with a public Gethello (String name) abstract method inside the interface.
Let's take a look at the code inside the Ejbtest.java.
Package ejbtest;
Import Javax.ejb.Remote;
Import javax.ejb.Stateless;
/**
* Session Bean Implementation Class Ejbtest * *
@Stateless (mappedname= "Ejbname")
@Remote (Ejbtestremote.class)
public class Ejbtest implements Ejbtestremote {
/** * Default constructor. */
public string Gethello (string name) {
TODO auto-generated Method Stub
Return "Hello," +name;
}
}
This class implements the Ejbtestremote interface and implements the method of the interface, the most noteworthy of which are these two words
@Stateless (mappedname= "Ejbname")
The main function of the first sentence @stateless (mappedname= "Ejbname") is to declare that the class maps to the name on the WebLogic, and also to declare that it is a stateless entity bean.
@Remote (Ejbtestremote.class)
Instead, declare the class to be a remote calling class with an interface of Ejbtestremote.
。 Okay, here's the server-side code, and here's a look at the client's project.
This is a client code, just a simple Java project. After you have built the client project, load the server side project library before you write the code, and don't ask why, this will be explained later. The specific loading process is: Click the Client Project Right button and choose Build path--"Configure build Path"
Select Project, I have been added here, so you will see the Ejbtest project already exists, if you do not, you can add the click OK through the Add button can be added to the client side of the code.
When you're done, you'll see a lot of new jar packs in the JRE System library.
OK, now it's time to write the client code.
Package Yang.tian.biao;
Import java.util.Properties;
Import Javax.naming.Context;
Import Javax.naming.InitialContext;
Import javax.naming.NamingException;
Import Ejbtest. Ejbtestremote;
public class Test {
/**
* @param args
*
/public void SayHello () {
String url= "t3://localhost:7001" ;
Properties P=new properties ();
P.put (Context.initial_context_factory, "weblogic.jndi.WLInitialContextFactory");
P.put (Context.provider_url, URL);
Try {context
ctx=new InitialContext (p);
Ejbtestremote ert= (ejbtestremote) ctx.lookup ("Ejbname#ejbtest"). Ejbtestremote ");
System.out.println (Ert.gethello ("Yang")); catch (Namingexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} public static void Main (string[] args) {//TODO auto-generated method Stubnew Test (). SayHello ();}}
Ejbtestremote ert= (ejbtestremote) ctx.lookup ("Ejbname#ejbtest"). Ejbtestremote ");
Note that this code, which is the meaning of the wenlogic from the mapping name to find the remote delivery class, that is, ejbtestremote
Ejbtestremote ert= (Ejbtestremote)
Well, here is a little bit, we all see the use of ejbtestremote may be strange why the client has not built ejbtestremote this class, but can use it. This is just the use of adding server-side code to the client.
All right, all set, open WebLogic, deploy the server-side code to WebLogic, run the client, and the background will print the result:
Hello,yang
OK, the project is successful, so you can do the remote method call the ~~~~~~~~