Getting started with Session Bean

Source: Internet
Author: User

Bromon originality, please respect copyright

After so long mixing in the Java World, I have been asked the most questions since I broke away from the deep-rooted Java cainiao community. The first question is "What books should I read when I learn java ", the second is "What is J2EE ". The first question is confusing, and the second question is hard to explain. The J2EE framework is huge, with 13 core technologies. It is too complicated to explain. The concept of J2EE is often abused. What is a J2EE application? This standard is not fixed. However, in any case, EJB is the most important core of J2EE, which is certainly unquestionable. The topic of this article is as small as possible. It only involves the session bean in the EJB in J2EE, so that experts may not have a joke. All the examples in the demo are passed under j2sdk 1.4.2 + j2sdkee 1.3.1. The server used is included in j2sdkee1.3.1. to simplify the operation, we also use the deployment tool deploytool that comes with j2sdkee1.3.1. The development tool is eclipse 2.1.2 without any additional plug-ins.

What did J2EE do? Suppose you need to develop a very large system (for example, finance or Telecom). Obviously, in addition to completing functions, you need to consider other issues, for example:

Distribution. Different modules may be distributed on different JVMs.
Scalability. A single module needs to handle a large number of concurrent operations. New machines and distributed components may be added at any time.
Transaction. Strict transaction control is required between multiple distributed components.
Integration. Most of these systems need to be integrated with old systems. You may not even know what database your customers are using or what the server is.

On the Java platform, If you develop a system according to certain specifications, abide by a strict framework, define interfaces that comply with the specifications, and write classes to implement the specified interfaces, then the system you write can meet the above requirements. This framework is collectively referred to as J2EE. I think this is a reasonable explanation.

There are three types of ejbs: Session Bean, Entity Bean, and message-driven bean ). The message Bean is a new member and does not need to be involved. The Session Bean and the Entity Bean are very differentiated. You can simply regard the Entity Bean as the proxy of the database table, that is, Data. Session beans can be seen as operations on data. You can understand the field and method in the object.

There are two types of session beans: stateful Session Bean and stateless Session Bean ). The stateful session bean can save the customer status. You can simply use the session in HTTP to understand it. Some user information can be saved temporarily and will disappear once the session ends. The stateless session bean does not store information at all. Obviously, stateless Session Bean is simpler, and we start with it.

A standard Session Bean requires four interfaces and at least one class. Let's get the simplest Hello world example. This project is named hello. According to the naming rules recommended by Sun, we should define the following interfaces and classes:

The first is 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 logic method we specify to actually provide services. It will be implemented in the bean class. One thing to note is that, as an experienced Java developer, you should avoid having the same object name with its method, such:
Hello = new Hello ();
Hello. Hello ();
Here, the object Hello has a hello () method, which should be avoided when designing classes and interfaces.

Remote interfaces do not deal with clients. This is the task of the home interface. The following defines the home interface hellohome. Java:
/*
* Created on 2004-4-4
*/
Package 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 obtains a hello object by calling this method, and then can call the methods defined in the hello interface.

You can write the class to execute the 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 the javax. EJB. sessionbean interface, so it must have the following methods to pass the syntax check:
The ejbcreate () Container calls this method to generate an instance.
The ejbremove () Container calls this method to destroy the instance.
The ejbactivate () Container calls this method to activate the instance.
Ejbpassivate () Container calls this method to deactivate an instance.
The setsessioncontext (sessioncontext CTX) Container calls this method to enable the instance to register in the environment.

There is also a say () method, which is actually a logical method for providing services. Note that the logical methods in hellobean must be stated in the hello interface; otherwise, they will fail. Similarly, if the ejbcreate () method is overloaded in hellobean, it must be declared in the hellohome interface; otherwise, it will fail. These two points will be demonstrated in the future stateful Session Bean.

So far, an EJB can work normally, but do you remember the above? The most standard Session Bean should have four interfaces, but now we only have two. Both of the above interfaces are remote interfaces, and both of their methods need to throw a remote exception java. RMI. RemoteException. Implementing remote interfaces is resource-consuming, which is also the main reason why J2EE is criticized. In fact, many ejbs are not called remotely. They may be called by other ejbs running on the same server. In this case, it is obviously unwise to use remote interfaces, this is especially evident in entity beans. Therefore, the J2EE specification defines a local interface to solve the problem, which is used exclusively for local calls. The following is a definition:
Local interface hellolocal. Java:
/*
* Created on 2004-4-4
*/
Package org. bromon. EJB. stateless;

/**
* @ Author bromon
*/
Public interface hellolocal extends javax. EJB. ejblocalobject
{
Public String say ();
}

The difference between this interface and the hello interface is that it inherits different interfaces, and its say () method does not need to throw a remote exception.

Then define the 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 inherits different interfaces than the hellohome interface and does not need to throw remote exceptions. However, it still needs to throw javax. EJB. createexception, which is an exception that may occur when the container creates an EJB and must be thrown.

The coding is basically complete. It also requires the deployment descriptor, but we can do it using tools. The following describes the deployment process.
Start the server first:
J2EE-verbose
This is probably the case after startup: (figure 1)

Screen. width/2) This. width = screen. width/2 "vspace = 2 border = 0>

Then you can start the deployment tool:
Deploytool
The deployment tool automatically detects the running J2EE. Otherwise, the tree structure such as server-localhost is invisible in the window on the left, most of which is caused by the failure of J2EE.

The subsequent operations are complex and require careful and patience.
Create an application:
File-New-application, Click Browse, select the path of the generated. Ear file, and fill in the file name as hello. Ear.
Then add the EJB:
Select the new Hello application in the left window, and then:
File-New-enterprise Bean
In the pop-up window, click "Next" and click "edit" to add a file. The configuration should look like this: (figure 2)

Screen. width/2) This. width = screen. width/2 "vspace = 2 border = 0>

All the notes are marked.
Next, you need to fill in some options and fill in the options as shown in the following figure: (figure 3)

Screen. width/2) This. width = screen. width/2 "vspace = 2 border = 0>

Next, finish, or finish.
Then you need to specify a JNDI name for the EJB. The client uses this name to find the EJB: (Figure 4)

Screen. width/2) This. width = screen. width/2 "vspace = 2 border = 0>

Set the JNDI name to hello in red. Note the case sensitivity.
Verify that the tool is correct: Tools-verifier
A warning may be reported, which does not affect deployment. However, if the report has failed, check 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, and the system will generate. JAR file. Here is helloclient. jar, which contains the local stub (stud) of the remote call, which must be referenced by the client program. Then you can finish the deployment process and then OK.

Write the client program client. Java below:
/*
* Created on 2004-4-4
*/
Package org. bromon. 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 = home. Create ();
String result = Hello. Say ();
System. Out. println (result );
} Catch (exception E)
{
System. Out. println (E );
}
}
}

To compile and run the program, you must add the helloclient. Jar generated during deployment to the classpath. The procedure is as follows:
Javac-classpath %; F: helloclient. jar-D. Client. Java
F: helloclient. jar is the location of the. jar file generated during deployment. Modify the file by yourself.
Run the program:
Java-classpath %; F: helloclient. Jar org. bromon. EJB. Stateless. Client
Get the running result: Hello. This is J2EE's hello. It's always worth it. Get a distributed Hello service cluster to scare people.

The development process of the stateless Session Bean is basically completed, and many details are ignored. However, it is still good to get started. Many beginners have spent a lot of time not working with Hello world, as a result, the system gave up after vomiting blood. When I tried to find out the above process in the case of lack of data, it was really a history of blood and tears.

All the code in this example is actually copied from eclipse. The code is hot and can be executed. If you have any problems, consider it first.

Tired, dual keyboards and enthusiasm have caused great wear and tear. Let's talk about stateful session beans later.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.