After being ruthlessly torn down by the two heavyweight giants EJB and JBoss, coupled with a serious lack of relevant information, I finally ran the EJB demo.
Now let's take a look at this small development instance tutorial. Personal station: http://muscle1990.com /? P = 271
1. Configure the development environment:
JDK optional;
Eclipse optional;
JBoss recommends jboss-4.2.1.GA. Baidu can directly find the official red-hat website for download, and I will not post it.
In theory, the new version can be used, but I have a shadow before...
2. Configure jboss_home and run it.
Decompress the jboss-4.2.1.GA to your favorite directory, it is recommended that the directory name do not have spaces, do not have Chinese.
Open the environment variable, add "jboss_home", the value is the installation directory, such as "C: \ Dev \ jboss-4.2.1.GA", the quotation marks are wooden!
Open jboss_home/bin to view run and bat. Double-click to start JBoss.
You can also run "run" or "Run-C all" in the CD directory in cmd ".
If no error is reported and the following interface is displayed, the installation is successful.
Open http: // localhost: 8080/in the browser (sure port 8080 is not occupied). You can see the welcome page of JBoss.
In this way, the startup is successful.
3. Create an EJB project:
1) create a Java project in eclipse. My name is "helloworld", you know.
2) Introduce jbossall-client.jar in jboss_home/client.
2) create an interface named helloworld. The instance code is as follows:
/** @ Author Mo shihao * moshihao@gmail.com muscle1990.com * @ version: 1:11:04 **/public interface helloworld {Public String sayhello (string name );}
Create a stateless Session Bean named helloworldbean
Import javax. EJB. remote; import javax. EJB. stateless;/** @ author Mo shihao * moshihao@gmail.com muscle1990.com * @ version: 1:12:23 */@ stateless @ remote ({helloworld. class}) public class helloworldbean implements helloworld {Public String sayhello (string name) {return name + "This is the first EJB case of muscle! ";}}
The next step is to convert the project export into a jar file.
Place the location in the jboss_home \ Server \ All \ deploy \ directory.
OK. If there is no accident, your JBoss console should have a response.
02:13:53,083 INFO [EJBContainer] STARTED EJB: HelloWorldBean ejbName: HelloWorldBean02:13:53,136 INFO [EJB3Deployer] Deployed: file:/C:/dev/jboss-4.2.1.GA/server/all/deploy/HelloWorld.jar
If not, close JBoss and run cmd to the jboss_home \ bin directory. Enter run-C all in the console. Theoretically, the problem can be solved.
Next let's go to http: // localhost: 8080/JMX-console.
Select Service = jndiview.
Click invoke.
Congratulations, it has been more than half done.
4. Create a test program.
Create any class in the original project. The instance is as follows:
Import Java. util. properties; import javax. naming. initialcontext; import javax. naming. namingexception;/** @ author Mo shihao * moshihao@gmail.com muscle1990.com * @ version: 1:26:09 **/public class helloworldtest {public static void main (string ARGs []) {helloworld; try {properties props = new properties (); props. setproperty ("Java. naming. factory. initial "," org. jnp. interfaces. namingcontextfactory "); props. setproperty ("Java. naming. provider. URL "," localhost: 1099 "); props. setproperty ("Java. naming. factory. URL. pkgs "," org. JBoss. naming: Org. jnp. interfaces "); initialcontext CTX = new initialcontext (props); helloworld = (helloworld) CTX. lookup ("helloworldbean/remote"); system. out. print (helloworld. sayhello ("helloworld,");} catch (namingexception e) {e. printstacktrace ();}}}
Run:
Helloworld, this is the first EJB case of muscle!
In theory, this small EJB test was successful, and the remote interface was successfully accessed by using JNDI through EJB and JBoss.