Guide
In this article you will learn how to use JBOSS7 to develop a simple EJB application and the problems encountered in this process. Environment
MyEclipse10
JBOSS7
JDK1.8.0 Preparing the MyEclipse10 configuration JBOSS7 Understanding JBoss Directory Results
First look at the JBOSS7 directory structure, and the previous version is not the same
In JBoss 7, the file system is divided into two parts:
1. Standalone Server standalone
2. Domains server domain (this is the first introduction of JBoss 7)-for unified management of multiple instances
Bin: Contains a startup script to start a standalone server (if using a standalone instance) or a domain (using a domain server)
Docs: Contains server documentation with two subdirectories, licenses (Licenses.xml and related content) and Schema (the. xsd file used by the configuration)
Domains: Contains the domain structure, a subdirectory consists of a configuration file containing the domain, data (the content folder containing the published module), and Lib (for Java EE extensions) TMP (temporary directory).
Standalone: Structure and Domain folder are the same
Modules: Because JBoss 7 is a modular server, the module for the application server corresponds to a subdirectory here. Configure JBoss
Configuring the JDK
Run JBoss
developing EJBS Create EJB Project
authoring interfaces and implementations
Interface
Package COM.TGB.EJB;
Public interface FIRSTEJB {
string saysomething (string name);
}
Implement
Package COM.TGB.EJB;
Import Javax.ejb.Remote;
Import javax.ejb.Stateless;
@Stateless
@Remote Public
class Firstejbbean implements FIRSTEJB {
@Override public
String SaySomething (String name) {
//TODO auto-generated method stub
return "Hello" +name;
}
Add Annotations
The above annotations indicate a stateless session bean (@Stateless) and a remote call (@Remote). deploy to JBoss and run
Development Client Create a new Javaproject Connect the client with the EJB to invoke Packaging Interface
Right-click on FIRSTEJB, select Export, and package to Ejb_01_client project
Configure the packaged jar package to the client
Add a jar package to the client that calls the EJB to rely on
Jar Package Jboss-client.jar writing client in Bin\client under JBoss installation path
Package COM.TGB.EJB;
Import java.util.Hashtable;
Import Javax.naming.Context;
Import Javax.naming.InitialContext; public class Firstejbclient {public static void main (string[] args) throws exception{//jboss6,7 before/*i
Nitialcontext context=new InitialContext ();
FIRSTEJB firstejb= (FIRSTEJB) context.lookup ("Firstejbbean/remote");
String s=firstejb.saysomething ("Xu Chen Yang");
System.out.println (s); */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.
Final context context = new InitialContext (jndiproperties);
AppName and ModuleName are in the format of packaging//if the. Ear is AppName, the other is ModuleName (. Jar,.war) Final String appName = "";
Final String modulename = "ejb_01";
Final String distinctname = "";
Implement class name final String beanname = "Firstejbbean"; System.out.println (Beanname);
Interface class name final String Viewclassname = FirstEjb.class.getName ();
System.out.println (Viewclassname); String Jndi = "EJB:" + appName + "/" + ModuleName + "/" + Distinctname + "/" + Beanname + "!" + VIEWCLASSN
Ame
System.out.println (Jndi);
FIRSTEJB FIRSTEJB = (FIRSTEJB) context.lookup (JNDI);
String s= firstejb.saysomething ("Xu Chen Yang");
System.out.println (s);
}
}
writing a configuration file jboss-ejb-client.properties
Tell the client where the EJB is
Endpoint.name=client-endpoint
Remote.connectionprovider.create.options.org.xnio.options.ssl_enabled=false
Remote.connections=default
remote.connection.default.host=127.0.0.1
Remote.connection.default.port = 4448
Remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY _noanonymous=false
Remote.connection.default.username=sa
Remote.connection.default.password=sa
running the client
Summary
1. Configure host and port in Jboss-ejb-client.properties
remote.connection.default.host=127.0.0.1
remote.connection.default.port = 4448
Start host with "localhost", the result of running the client error:
warn:could not register a EJB receiver for connection to Remote:localhost
So changed to "127.0.0.1";
The port number to be based on the configuration in JBoss Standalone.xml file, default is 4447, my change to 4448.
In this process find yourself not very clear about some of the configurations in standalone. Refer to JBoss Configuration Guide (iii)
2. Meaning of appname in client code
AppName: If a. Ear package is deployed to the server, then the app-name is removed as the suffix. The package name after the ear. If you are deploying a. War package or a common. jar package, then appname is left blank, and I am the project is deployed with a. jar package, so appname is empty.
ModuleName: Here is the deployment to the server. The name of the war package or the. jar package, note that ModuleName cannot be empty.
Distinctname: This is an optional parameter name for specifying a specific deployment configuration on JBoss AS7, so leave it blank if you don't need it.
Beanname: This is the bean we are going to use lookup lookup for, this project is Firstejbbean, the implementation bean of FIRSTEJB, without the fully qualified class name, Beanname cannot be empty.
Finally, the path to our stitching is
EJB:/EJB_01//FIRSTEJBBEAN!COM.TGB.EJB.FIRSTEJB
If there is a stateful EJB, then the concatenation of the string will be followed by ". Stateful ".