Bromon original, please respect the copyright
After mixing for so long, since the Java rookberlore group of the masses was released, the first is "Java should look at what book", the second is "J2EE". The first question is so embarrassed, and the second problem is not explained. The J2EE framework is huge, and there are 13 core technologies, too complicated, not well explained. J2EE's concept is often abused, how can J2EE applications? This standard is not good. But no matter what, EJB is the most important core of J2EE, which is definitely no doubt. The topic of the article is also small as much as possible, and only involves the session bean in the EJB in J2EE, lests to read jokes. All examples of demonstration are passed under J2SDK 1.4.2 J2SDKee 1.3.1, and the server used is comes with J2SDKee1.3.1, in order to simplify operations, we also use J2SDKee1.3.1 Deployment tool deploytool. The development tool is Eclipse 2.1.2 and does not use any additional plugins.
Why is J2EE? Suppose you need to develop a very large system (for big thinking, financial level, telecom level), very clearly in addition to completing the function, such as:
Distributed. Different modules may be distributed on different JVMs. Portability. A single module requires a high number of concurrent operations, which may add new machines, new distribution components. Transactionality. Strict transaction control is required between multiple distributed components. Integration. Such a system is mostly integrated with the old system, you may even don't know what the database is used, do not know what the server is.
On the Java platform, if you develop the system in accordance with certain specifications, follow a strict framework, define the compliant interface, then write some classes to implement the specified interface, then the system you write can implement the above requirements. This frame is collectively referred to as J2EE. I think this is reasonable.
EJB has three types: session bean, entity bean (Entity Bean), message-driven bean (Message-Driven Bean). Among them, the message bean is a new member, which can not be involved. Session beans and entity beans are very good, you can simply think of entity beans as a proxy for database tables, that is, data. The session bean can be seen as an operation of data. Want to understand the domain (Field) and Method (Method) in the object.
Session bean is divided into two: stateful session beans, and stateless session beans (Stateless session beans). State Session Bean can save your client status. You can simply understand it in HTTP, some information of the user can be temporarily saved, and once the session is over, it will disappear. The stateless session bean does not save information at all. Obviously, there is no status session bean is simpler, we start with it.
A specification session bean requires four interfaces and at least one class. Let's get the simplest Hello, WORLD as an example. This project is named Hello. According to the suggested naming specification recommended, we should define the following interfaces and classes:
The first is the remote interface hello.java: / * * created on 2004-4-4 * / package org.bromon.ejb.stateless;
/ ** * @Author bromon * / public interface hello extends javax.ejb.ejbobject {public string say () throws java.rmi.remoteException;}
This interface defines a SAY () method, this method is the logical method of the real service provider we specified, which will be implemented in the Bean class. One thing to explain is that as an experienced Java developer, it should avoid the same name of the object and its method, such as: hello hello = new hello (); hello.hello (); Hello here Hello has a Hello ( The method should avoid this when designing classes and interfaces. The remote interface is not dealing with the client, this is the task of the HOME interface. The HOME interface hellohome.java: / * * created on 2004-4-4 * / pack org.bromon.ejb.stateless
/ ** * @Author bromon * / public interface hellohome extends javax.ejb.ejbhome {Hello Create () throws java.rmi.remoteException, javax.ejb.createException;}
The Home interface defines the create () method, the client gets a Hello object by calling this method before calling the method defined in the Hello interface.
Below you can write a class that performs actual operation, named hellobean.java: / * * created on 2004-4-4 * / package org.bromon.ejb.stateless; import javax.ejb. *; / ** * @Author bromon * / public class HelloBean implements javax.ejb.SessionBean {private SessionContext ctx; public void ejbCreate () {System.out.println ( "create");} public void ejbRemove () {System.out.println ( "remove" );} public void ejbActivate () {System.out.println ( "activate");} public void ejbPassivate () {System.out.println ( "passivate");} public void setSessionContext (SessionContext ctx) {this.ctx = CTX;} public string Say () {Return ("Hello");}}
This class implements a javax.ejb.sessionBean interface, so it must have the following method to check the method through syntax: EJBCREATE () container calling this method to generate an instance ejbremove () container calling this method to destroy the instance ejbactivate () container call this method To activate an instance ejbpassivate () Container calls the method class passivation instance setSessionContext (sessionContext CTX) Container calls this method, let the instance register in the environment
There is another SAY () method, which is a logical method for providing services. It should be noted that the logical ways in Hellobean must be declared in the Hello interface, otherwise it is not. Similarly, if the overload of the EJBCREATE () method occurs in Hellobean, it must be declared in the HelloHome interface, otherwise it will not be. About these two points will be demonstrated in the subsequent state session beans.
At this point, a EJB that can work is produced, but still remembers the above? The most standardized session bean should have four interfaces, and we are only two now. The above two interfaces are remote interfaces, and their methods need to throw remote exception java.rmi.RemoteException. It is also a major resource for realizing remote interfaces. This is also the main reason why J2EE is underwriting. In fact, many EJBs are not called remotely, and they are likely to run other EJBs running on the machine, which is obviously unwise to use the remote interface in this case. This is embodied in the entity bean. Especially obvious. Therefore, the J2EE specification defines the local interface to solve the problem, which is used for local calls. Below: Local interface hellolocal.java: / * * created on 2004-4-4 * / package org.brom.ejb.stateless; / ** * @author bromon * / public interface hellolocal Extends javax.ejb.eblocalobject {Public string Say ();
The difference between this interface and Hello interface is inheriting different interfaces, and its SAY () method does not need to throw remote exceptions.
Then you need to define local home interface hellolocalhome.java: / * * created on 2004-4-4 * / package org.bromon.ejb.stateless;
/ ** * @Author bromon * / public interface hellolocalhome extends javax.ejb.ejblocalHome {Hellolocal create () throws javax.ejb.createException;}
Similarly, it is compared with the HelloHome interface, but also inherits different interfaces and does not need to throw remote exceptions. But it still needs to throw javax.ejb.createException, which is an exception that may be generated when the container creates EJB, and must be thrown.
The encoding work is basically completed, and it also needs to deploy a descriptor, but we can do it through tools. The following is a deployment process. First start the server: J2EE -Verbose is probably this look after starting complete: (Figure 1)
Screen.width / 2) this.width = Screen.width / 2 "vSpace = 2 border = 0>
Then you can start the deployment tool: Deploytool Deployment Tool automatically detects J2EE in the run, otherwise you can't see the tree structure like Server-localhost in the window, mostly J2EE is not started.
Then the operation is more complicated and needs carefully and patience. Create a new Application: file-new-application, click Browse, select the path where the generated .ser file is located, and fill in the file name to Hello. pear. Then add EJB: Select the application in the left window, then this application, then: file-new-enterprise bean in the prompt window in the pop-up, click Edit to add files, configure it should be this picture: (Figure 2 )
Screen.width / 2) this.width = Screen.width / 2 "vSpace = 2 border = 0>
The places you need to pay attention to are marked. Then Next, you need to fill in some options, fill it into this, ok: (Figure 3)
Screen.width / 2) this.width = Screen.width / 2 "vSpace = 2 border = 0> You can then all the way, last finish, direct finish can also. Then you need to specify a JNDI name for this EJB, the client is Look for this EJB through this name: (Figure 4)
Screen.width / 2) this.width = Screen.width / 2 "vSpace = 2 border = 0>
For the red place, I fill in a JNDI Name as Hello, paying attention to case. Verify that it is correct: Tools-Verifier may report a Warning, which does not affect deployment, but if there is failed, please check it carefully. Now you can deploy: Tools-Deploy (Figure 5)
Screen.width / 2) this.width = Screen.width / 2 "vSpace = 2 border = 0>
You need to select Return Client JAR, which generates a .jar file in the specified location, here is HelloClient.jar, which contains local stubs (STUDs) remotely, which is a client program that needs reference. Then you can finish, OK after the deployment process is completed.
The following is written in client program client.java: / * * created on 2004-4-4 * / package org.brom.ejb.stateless; import javax.naming. *; Import javax.rmi. *;
/ ** * @Author bromon * / public class client {
public static void main (String [] args) {try {Context ctx = new InitialContext (); Object obj = ctx.lookup ( "hello"); HelloHome home = (HelloHome) PortableRemoteObject.narrow (obj, HelloHome.class); Hello Hello = Home.create (); string result = hello.say (); system.out.println (result);} catch (exception e) {system.out.println (e);}}}
Compilation and running the program requires the HelloClient.jar that is generated in the deployment in the ClassPath. The operation is as follows: javac -classpath% classpath%; f: helloclient.jar -d. Client.java where F: helloclient.jar is the location of the .jar file generated in the deployment, please modify itself. Run the program: java -classpath% classpath%; f: helloclient.jar org.bromon.ejb.Stateless.Client gets the results: Hello. This is the J2EE's Hello, the old value is worthy. Get a distributed Hello service cluster, scare people.
The development process of stateless session bean is basically completed, and many details are ignored, but as a entry is still good, many beginners can't get Hello World because they spend a lot of time, and the results of vomiting will give up. When it was docked in the absence of the above process, it was a history of blood and tears.
The code in the example is all actually copied in the Eclipse, and the heat is also ventilated, absolutely executable. If you encounter problems, you will consider your first. Tired, and the negative keyboard and enthusiasm have caused great wear. There is a status session bean will then say.