Write the first EJB: "hello, world"
Next we will officially start EJB programming. Before writing our first EJB, you should have a general understanding of EJB. If you do not have one, I suggest you read some articles in this regard on the Internet, otherwise, you will not be able to understand the content described below.
Remote Interface
Remote interface refers to the interface that can be viewed by the client
// HelloWorld. java Package sample; /* This is a remote interface. The client calls this interface to make real EJB work */ Public interface helloworld extends javax. EJB. ejbobject { Public String Hello () throws java. RMI. RemoteException; } |
Home Interface
We can think of the Home interface as a factory for creating ejbs. The Home interface tells the EJB container: "Hi, my customer wants me to generate an ejbs. Now I will hand over this task to you!"
// Helloworldhome. Java Package sample; /* The home interface tells the EJB container how to generate or destroy an EJB instance */ Public interface helloworldhome extends javax. EJB. ejbhome { Helloworld create () throws java. RMI. RemoteException, javax. EJB. createexception; } |
Implementation of EJB
Here is the real implementation of EJB.
// Helloworldbean. Java Package sample; Import javax. EJB. sessioncontext; /* The remote interface helloworld implemented by this class */ Pubic class helloworldbean implements javax. EJB. sessionbean { Private sessioncontext CTX; Public void setsessioncontext (sessioncontext CTX) { This. ctx = ctx; } Pubic void ejbRemove () { System. out. println ("ejbRemove ()"); } Public void ejbActivate () { System. out. println ("ejbActivate ()"); } Public void ejbPassivate () { System. out. println ("ejbPassivate ()"); } /* The hello method is the actual business logic. It can display the "hello, world" string on the client */ Public String hello () { System. out. println ("hello ()"); Return "hello, world "; } } |
Now, all the code for this session EJB has been compiled. Next, we need to write its deployment file:
Ejb-jar.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Ejb-jar> <Description> JBoss Hello World Application </description> <Display-name> Hello World EJB </display-name> <Enterprise-beans> <Session> <Ejb-name> Hello </ejb-name> <Home> sample. HelloHome <Remote> sample. Hello </remote> <Ejb-class> sample. HelloBean </ejb-class> <Session-type> Stateless </session-type> <Transaction-type> Bean </transaction-type> </Session> </Enterprise-beans> </Ejb-jar> |
This ejb-jar.xml file should be placed under the META-INF directory of the current project, in this example I put it in
F:/projects/jbss-tutorial directory. Of course, you should put it in your own project directory as needed, to give you a clearer picture of the location of the ejb-jar.xml file, I listed the entire directory structure for this example below:
In this way, we have completed the compilation of a simple session EJB, but in fact, JBOSS also provides an additional configuration file: JBoss. xml, which can be used to customize the JBOSS server. However, this example is too simple, so we can omit it.
Although we have completed compiling this session EJB, there is still a last step to do: package. First, go to the root directory of the current project:
Cd F:/project/jboss-tutorial |
Then run the jar command to package all the classes and ejb-jar.xml:
Jar cf HelloWorld. jar sample META-INF |
At this time, you will find that there is an additional file named HelloWorld. jar in the current directory, which is our final product. The diagram is as follows: