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
Prepare to configure JBOSS7 in MyEclipse10 to learn about 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 to create EJB Project
Authoring interfaces and implementations
Interface
package com.tgb.ejb;publicinterface FirstEjb { String saySomething(String name);}
Realize
package com.tgb.ejb;import javax.ejb.Remote;import javax.ejb.Stateless;@Stateless@Remotepublicclass FirstEjbBean implements FirstEjb { @Override publicsaySomething(String name) { // TODO Auto-generated method stub return"你好"+name; }}
Add annotations
The above annotations indicate a stateless session bean (@Stateless) and a remote call (@Remote).
Deploy to JBoss and run
Development client creates a new Javaproject to connect the client to the EJB to invoke the packaged 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
The jar package is in the bin\client under the JBoss installation path Jboss-client.jar
Writing clients
PackageCOM.TGB.EJB;Importjava.util.Hashtable;ImportJavax.naming.Context;ImportJavax.naming.InitialContext; Public class firstejbclient { Public Static void Main(string[] args)throwsexception{before//jboss6,7 /*initialcontext context=new InitialContext (); FIRSTEJB firstejb= (FIRSTEJB) context.lookup ("Firstejbbean/remote"); String s=firstejb.saysomething ("Xu Chen Yang"); System.out.println (s); */ FinalHashtable jndiproperties =NewHashtable (); Jndiproperties.put (Context.url_pkg_prefixes,"Org.jboss.ejb.client.naming");//Let the Jndi API know who is managing the namespace we use to find the Jndi name. FinalContext context =NewInitialContext (jndiproperties);//appname and ModuleName are packaged in different formats. //If the. Ear is appname, the others are ModuleName (. Jar,.war) FinalString AppName ="";FinalString ModuleName ="Ejb_01";FinalString Distinctname ="";//Implementation class name FinalString Beanname ="Firstejbbean"; System.out.println (Beanname);//Interface class name FinalString viewclassname = FirstEjb.class.getName (); System.out.println (Viewclassname); String Jndi ="EJB:"+ AppName +"/"+ ModuleName +"/"+ Distinctname +"/"+ Beanname +"!"+ Viewclassname; 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-endpointremote. ConnectionProvider. Create. Options. org. Xnio. Options. SSL_enabled=falseremote. Connections=default Remote. Connection. Default. Host=127.0. 0. 1Remote. Connection. Default. Port=4448Remote. Connection. Default. Connect. Options. org. Xnio. Options. SASL_policy_noanonymous=falseremote. Connection. Default. Username=saremote. Connection. Default. Password=sa
Running the client
Summarize
1. Configure host and port in Jboss-ejb-client.properties
remote.connection.default.host=127.0.0.1 remote.connection.default.port4448
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 a "? Stateful ".
"EJB series" (i) developing a simple EJB application in--JBOSS7