3. Develop an EJB
Do so much preparation, let's write an example helloworld, select the menu file-new-other, bring the following dialog box:
Figure 5
Choose to select Lomboz J2EE Project in Java-Lomboz J2EE Wizard, then click Next:
Figure 6
Enter a project name in Project Name, then click Next button,
Figure 7
Click the Add External Jars button in Figure 7Librarles tab, add c: /sun/appserver/lib/j2ee.jar, then click Next button,
Figure 8
Click the Add button in the EJB Modules tab, add an EJB Module: HelloWorld, then click Targeted Servers,
Figure 9
Select the JBoss 3.2.x server, click the Add button, and finally click the Finish button, then Package Explorer looks like Figure 10 looks like Figure 10.
Figure 10
At the project name HelloWorldProject, click the right mouse button. Select New in the pop-up menu, pop up the chart 11 dialog,
Figure 11
Select Lomboz EJB CREATION WIZARD, then click Next button,
Figure 12
Package Enter Com.test, Name Enter helloworldejb, EJB Type Selects Stateless Session EJB, then click the Finish button, then Package Explorer looks like Figure 13
Figure 13
At HelloWorldejbbean, click the right mouse button. Select Lomboz J2EE in the pop-up menu, then select Add EJB Method, pop up Figure 14 dialog,
Figure 14
Enter public string helloworld () in Method Signture, click the Finish button, find it in the generated code:
Public string helloworld () {
Return NULL;
}
change into:
Public string helloworld () {
Return "Hello World!";
}
HelloWorldejbbean in Package Explorer Click the right mouse button, select Lomboz J2EE in the pop-up menu, then select Add EJB To Module, pop up Figure 15 dialog box.
Figure 15
Select EJB [HelloWorld] and click the OK button. Finding HelloWorld in Package Explorer as shown in Figure 16:
Figure 16
At HelloWorld, click the right button, then select Lomboz J2EE in the pop-up menu, then select Generate EJB CLASSES, if there is no error, this is automatically generated an EJBSRC folder, which is automatically generated automatically.
Right click on the HelloWorld, select Lomboz J2EE in the pop-up menu, and then select Deploy Module to deploy EJB Module.
After the EJB Module is deployed, let's make a client to call the HelloWorld () method.
Select New in the File menu, then select Other, in the pop-up dialog box as shown in Figure 11, select the Lomboz EJB Test Client Wizard, and fill in the corresponding parameters as shown in Figure 17.
Figure 17
Then click the Finish button to generate the HelloWorldClient.java file, edit this file, so that the code looks like the following: //HelloWorldClient.java
/ *
* CREATED ON 2004-7-19
*
* Todo to change the Template for this generated file go to to THERATED FILE Go TO
* WINDOW - Preferences - Java - Code Style - Code Templates
* /
Package com.test;
Import java.rmi.remoteexception;
Import java.util.hashtable;
Import javax.ejb.createException;
Import javax.naming.initialcontext;
Import javax.naming.namingexception;
/ **
* @Author administrator
*
* Todo to change the Template for this generated Type Comment Go To To To To To To To TO
* WINDOW - Preferences - Java - Code Style - Code Templates
* /
Public class helloworldclient {
Private com.test.helloWorldejbhome gethome () throws namingexception {
return (com.test.helloworldejbhome) getContext (). Lookup
com.test.heloworldejbhome.jndi_name);
}
Private InitialContext getContext () throws namingexception {
Hashtable props = new hashtable ();
Props.put (InitialContext.Initial_Context_Factory,
"Org.jnp.interfaces.namingContextFactory");
Props.PUT (InitialContext.Provider_URL, "JNP: //127.0.0.1: 1099");
// this Establishes the security for authorization / authentication
// Props.Put (InitialContext.security_principal, "username");
// Props.Put (InitialContext.security_credentials, "password");
InitialContext InitialContext = New InitialContext (PROPS);
Return INIALCONTEXT;
}
Public void testbean () {
Try {
com.test.helloworldejb mybean = gethome (). CREATE ();
// --------------------------------------
// this is the place you make your caver
//System.out.println (MyBean.callyourthod ());
System.out.println (MyBean.helloworld ());
} catch (remoteException E) {
E.PrintStackTrace ();
} catch (creteException e) {E.PrintStackTrace ();
} catch (namingexception e) {
E.PrintStackTrace ();
}
}
Public static void main (String [] args) {
HelloWorldClient Test = new helloworldclient ();
Test.TestBean ();
}
}
Let's run this program, enter the JBoss installation directory D: /JBoss-3.2.3/bin, run run.bat to start the server, after the server starts, find HelloWorldClient.java in Package Explorer, right-click it In the selected menu, select the RUN, then select Java Application, if the Hello World is output in the console, the call is successful. If there is an exception, then you need to look at this article or email give me JJC @ ykjt .
There is also a problem, someone will ask how to run the client in the command prompt environment?
Need to write a batch file RunhelloWorldClient.bat
Java-ClassPath "D: / Eclipse3 / Workspace / EJBPROJECT / BIN; D: / JBOSS-3.2.3 / Client / Concurrent.jar; D: /Jboss-3.2.3/client/gnu-regExp.jar; D: / JBoss-3.2.3 / client / jacorb.jar; d: /jboss-3.2.3/client/jbossall-client.jar; d: /jboss-3.2.3/client/jboss-client.jar; d: / jboss -3.2.3 / Client.jar; d: /jboss-3.2.3/client/jbossha-client.jar; d: /jboss-3.2.3/client/jboss-iiop-client.jar D: /jboss-3.2.3/client/jboss-j2ee.jar; d: /jboss-3.2.3/client/jboss-jaas.jar; d: /jboss-3.2.3/client/jbossjmx-ant. JAR; D: /JBOSS-3.2.3/client/jbossmq-client.jar; d: /boss-3.2.3/client/jbossmqha.jar; d: /jboss-3.2.3/client/Jboss-Net-Client .jar; d: /jboss-3.2.3/client/jbosssx-client.jar; d: /jboss-3.2.3/client/jboss-system-client.jar; d: /jboss-3.2.3/client/ JCERT.JAR; D: /JBOSS-3.2.3/client-factory.jar; d: /jboss-3.2.3/client/jmx-ejb-connector-client.jar; d: / jboss -3.2.3 / Client / JMX-RMI-Connector-Client.jar; D: /JBoss-3.2.3/client/JNet.jar; D: /Jboss-3.2.3/client/jnp-client.jar; D : /jboss-3.2.3/client/jsse.jar; D: /JBoss-3.2.3/clie NT / log4j.jar "com.test.helloworldclient
Place it in the D: / Eclipse3 / Workspace / HelloWorldProject / bin directory (Copy of COM / TEST), then run, or add the path in the quotation marks in the above batch number in the environment variable ClassPath, then run Java com.test .Heloworldclient
It is also possible.
Is it very ridiculous, "Rain, everyone will take the clothes" :)