Battle JBOSS - teach you to write the first EJB ----- 3

xiaoxiao2021-03-06  63

Deploying our EJB deployment EJB is a very easy task in JBOSS, you can copy helloWorld.jar to the C: / jboss-3.2.6 / server / default / deploy directory. The illustration is as follows:

At this time, you can switch to the DOS window running in JBoss, you will find the following prompt information on the screen:

15: 09: 21,184 info [maindeployer] Starting Deployment USA: file: / f: /jboss-3.2.3/server/default/deploy/helloworld.jar15: 09: 21,324 info [ejbmodule] Creating15: 09: 21,354 Info [ejbModule] Deploying HelloWorld15: 09: 21,464 INFO [ejbModule] Created15: 09: 21,484 INFO [ejbModule] Starting15: 09: 21,555 INFO [ejbModule] Started15: 09: 21,555 INFO [MainDeployer] Successfully completeddeployment of package: file: / F: /jboss-3.2.6/server/default/deploy/helloworld.jar

Client code

If there is no client code, then EJB is almost unused. We will write client code to call this helloworld.

If you run the client code and the JBoss server on the same machine, then you can run without any modifications, but your client runs on another machine, then you have to change the corresponding line in the source code. a bit:

/ * The following is a line * / env.put (Context.Provider_url, "Localhost: 1099") in the client source code.

Suppose the EJB is deployed on a machine that is 192.168.0.1 in an IP address, then the above source code should be changed as follows:

/ * The following is the modified row after the client source code * / env.put (Context.Provider_URL, "192.168.0.1:1099" ;/*helloworldclient.java*/package sample; import javax.naming.context; import javax .naming.InitialContext; import java.util.Hashtable; public class HelloWorldClient {public static void main (String [] args) {Hashtable env = new Hashtable (); env.put (Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces. NamingContextFactory "); Env.Put (Context.Provider_url," Localhost: 1099 "); Env.Put (" java.naming.factory.url.pkgs "," org.jboss.naming: org.jnp.interfaces "); try {Context ctx = new InitialContext (env); Object obj = ctx.lookup ( "HelloWorld"); HelloWorldHome home = (HelloWorldHome) javax.rmi.PortableRemoteObject.narrow (obj, HelloWorldHome.class); HelloWorld helloWorld = home.create (); System.out.println (HelloWorld.Hello ()); helloWorld.remove ();} catch (Exception E) {E.PrintStackTrace (); System.out.Println ("Exception:" E.GetMessage )));}}} OK, I will compile and run this client below. If you can't find some classes when you compile, you may not be J2EE.ja. r This package is placed in the path variable of ClassPath. Although the execution result of the client is just a simple "Hello, World" on the screen, it is from another world.

--Jboss's voice!

转载请注明原文地址:https://www.9cbs.com/read-84560.html

New Post(0)