Guide
This is because the jboss6,7 version differs from the previous version in the syntax for local and remote invocation of the EJB. Therefore, the code example is given in this article for informational purposes only. Environment
MyEclipse10
JBOSS7 Theoretical knowledge remote Access process
Remote Client
Run on a different machine or a different JVM process than the EJB
It can be a Web component (such as a JSP, a Servlet), an application client, or other EJB
For the client, the EJB's location is transparent.
In order to create an EJB that can be accessed by a remote client, you must define these EJBS with @remote annotations. parameter Passing method
Pass Value
Reason:
Because the client and the server belong to different processes, the memory space cannot be shared.
After the client's user object has been serialized and deserialized, it becomes another user object on the service side, and the property of the client's user object is unchanged when the server modifies a property of the user object. We will show this in the following code. Precautions
The passed parameter, if it is an object, needs to implement the serialized interface implements Serializable the local calling procedure
Local Client
Running on the same JVM process as the EJB
It can be a Web component (such as a JSP, Servlet), or other EJB
For the client, the EJB's location is transparent.
In order to create an EJB that can be accessed by a remote client, you must define these EJBS with @local annotations. parameter Passing method
Transfer address
Reason: The object is passed a reference, whether the client or the server to modify the object, are modified by the same copy. Code interface and implementation of EJB
Unlike previous versions of JBoss, EJBS here cannot be declared as @remote and @local at the same time.
Project directory
User.java (be sure to implement the Serializable interface)
Package COM.TGB.EJB;
Import java.io.Serializable;
@SuppressWarnings ("Serial") Public
class User implements Serializable {
private String username;
private int id;
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
this.username = username;
}
public int getId () {
return ID;
}
public void setId (int id) {
this.id = ID;
}
}
Usermanagerremote.java (Remote Call interface)
Package COM.TGB.EJB;
Public interface Usermanagerremote {public
void AddUser (user user);
Usermanagerlocal.java (Local call interface)
Package COM.TGB.EJB;
Public interface Usermanagerlocal {public
void AddUser (user user);
Usermanagerbean.java (Implementation)
Package COM.TGB.EJB;
Import javax.ejb.Local;
Import Javax.ejb.Remote;
Import javax.ejb.Stateless;
@Stateless
@Local (usermanagerlocal.class)
@Remote (usermanagerremote.class) Public
class Usermanagerbean implements Usermanagerremote,usermanagerlocal {
@Override public
void AddUser (user user) { C17/>system.out.println ("User.username=" +user.getusername ());
User.setid (9);
}
}
When you're done, remember to deploy to JBoss and run JBoss. Remote Call
A common Javaproject
Detailed steps refer to the previous post "EJB series" (i) developing a simple EJB application in--JBOSS7
Results
Although the server performed SetID (9), the client GetID () was still 0. Local Call Create Web Project writing and configuring a servlet
Userservlet.java
Package com.tgb.web;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.util.Hashtable;
Import Javax.naming.Context;
Import Javax.naming.InitialContext;
Import javax.naming.NamingException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Com.tgb.ejb.User;
Import com.tgb.ejb.UserManagerLocal; @SuppressWarnings ("Serial") public class Userservlet extends HttpServlet {public void doget (HttpServletRequest requ EST, httpservletresponse response) throws Servletexception, IOException {response.setcontenttype ("
Text/html ");
PrintWriter out = Response.getwriter (); Out.println ("<!
DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > ");
Out.println ("<HTML>");
Out.println ("
Xml
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class> com.tgb.web.userservlet</servlet-class>
</servlet>
<servlet-mapping>
< Servlet-name>userservlet</servlet-name>
<url-pattern>/servlet/userservlet</url-pattern >
</servlet-mapping>
Deploy Project Access servlet
My project's access is connected when Http://localhost:8091/ejb_03_webclient/servlet/UserServlet
The port number of the project should be based on JBoss configuration file standalone.xml.
Package The jar file into the Lib directory
Export the EJB project "ejb_03" four Java files into a jar package and add them to the Web-inf Lib path of this project
Writing Clients
Final Hashtable jndiproperties = new Hashtable (); Jndiproperties.put (context.url_pkg_prefixes, "org.jboss.ejb.client.naming");//Let the Jndi API know who manages what we use to find Jndi
The name of the namespace.
Context context;
try {context = new InitialContext (jndiproperties); AppName and ModuleName are packaged separately//if yes. Ear is AppName, others are modulename (. Jar,.war) Final String AppName
= "";
Final String modulename = "Ejb_03_webclient";
Final String distinctname = "";
Implement class name final String beanname = "Usermanagerbean";
System.out.println (Beanname);
Interface class name final String Viewclassname = UserManagerLocal.class.getName ();
System.out.println (Viewclassname);
String Jndi = "java:module/" + Beanname + "!" + viewclassname;
System.out.println (Jndi);
Usermanagerlocal usermanagerlocal; Usermanagerlocal = (usermanagerlocal) context.loOkup (Jndi);
User User=new user ();
User.setusername ("Xu Chen Yang");
Usermanagerlocal.adduser (user);
SYSTEM.OUT.PRINTLN ("User id:" +user.getid ());
} catch (Namingexception e) {//TODO auto-generated catch block E.printstacktrace (); }
Run JBoss
From the console we can see the path of our deployed EJB in Jndi, the above code we use one of the paths, the remaining two can also:
Java:global/ejb_03_webclient/usermanagerbean!com.tgb.ejb.usermanagerlocal
java:app/ejb_03_webclient/ Usermanagerbean!com.tgb.ejb.usermanagerlocal
We see that the last user ID entered is 9, indicating the service side changes, the client can see that the operation is the same user object. Summary
The problems encountered in this process include:
1. Local calls do not need to introduce Jboss-client.jar, and do not need to configure jboss-ejb-client.properties, but to introduce Ejb_03.jar, and to be placed in the Lib directory, Otherwise it will report classdefnotfound,classcastexception and other errors.
2. The other is the name passed when Context.lookup.
The remote invocation is:
String Jndi = "EJB:" + appName + "/" + ModuleName + "/"
+ distinctname + "/" + Beanname + "!" + viewclassname;
When calling locally:
Remove the previous "EJB:". Change to "Java:global" or "Java:app" or "java:module", but be aware that ModuleName is ejb_03_webclient instead of "ejb_03". The specific path of the stitching, referring to our console output of the three paths, slightly different. Reference
JNDI Reference
[AS7.1.1] EJB JNDI lookup confusion:remote vs Local