Getting started with EJB-first EJB

Source: Internet
Author: User
This article is based on the <> instance. It mainly provides a relatively intuitive example for new users and serves as their own logs. It is not intended to introduce the principles of EJB. In addition, due to my limited level, please kindly advise.
The IDE used by the author is eclipse3.0 + myeclipse4.01ga.
The J2EE container is jboss4.0.

This article describes the complete development and deployment process of a stateless session bean that helps deposit and withdrawal. The procedure is as follows:
1. Compile the implementation class of the stateless Session Bean.
2. Compile the main interface and Component Interface of the stateless Session Bean.
3. Compile the bean into an application and write the deployment description.
4. Deploy applications on the EJB server.
5. Use a Java application for testing.
The above is the main process. Some steps do not need to be completed manually. IDE can simplify the development process. If you are not clear about this function of IDE, refer to the product documentation (http: // myeclipseide.com/#iseworkbench/help/index.jsp? Topic =/com.genuitec.myeclipse.doc/html/quickstarts/firstejb/index.html ).

1. Create an EJB project named fundejb, and use the default values for other projects.
Ii. Create a Session Bean:
1. Create a new package in the src directory: qiya. Deng. Fund. EJB. Note that the package name must be suffixed with EJB, because the XDoclet tool we need later.
2. Create a New sessionbean class named statelessfundmanagerejb. It must be suffixed with EJB for the same reason. It is best to use EJB or bean as the suffix according to the specifications.

3. Configure XDoclet:
Right-click the project, select Properties, select myeclipse-XDoclet, click Add Stander..., and select standard EJB.

Select standard EJB, right-click ejbxdoclet and choose add from the shortcut menu. In this example, JBoss is used as the application server. Select JBoss and modify the following attributes.
Version = 4.0
Destdir = src/META-INF
After modification, click OK to return to the main window.

4. Run XDoclet:
Right-click the project and choose myeclipse> RUN XDoclet. When running, a prompt message is displayed in the console window. After running, the directory structure is changed.

5. Edit the implementation class statelessfundmanagerejb:
Select and observe the statelessfundmanager interface before editing the statelessfundmanager class. You can find that the interface methods of this remote Component Interface correspond to those of statelessfundmanager.
Add the following at the end of the statelessfundmanager. Java file:


        /**
     *
     * @param balance
     * @param amount
     * @return
     *
     * @ejb.interface-method
     */
    public double addFunds(double balance,double amount){
        balance += amount;
        return balance;
    }
   
    /**
     *
     * @param balance
     * @param amount
     * @return
     * @throws InsufficientBalanceException
     *
     * @ejb.interface-method
     */
    public double withdrawFunds(double balance,double amount)throws InsufficientBalanceException {
        if (balance < amount) {
            throw (new InsufficientBalanceException());
        }
        balance -= amount;
        return balance;
    }

Repeat Step 1 to run XDoclet, and then observe the statelessfundmanager interface.

6. Deploy the application to the EJB Server:
The deployment description item is automatically generated in IDE, where the file is located in the/META-INF/ejb-jar.xml. Open the ejb-jar.xml and view the JBoss. xml file description.
Use the deployment tool provided by myeclipse for deployment:

Run the JBoss container and you will see the following message:

For use of application server in myeclipse, see the documentation (http://www.myeclipseide.com/images/tutorials/quickstarts/appservers ).
So far, you have released a simple stateless Session Bean. Below is a simple application for testing.

3. Compile the Java client program for testing.
The client program can be a web program or an application program. The application is used as an example.
Similarly, use eclipse to create a Java project named fundclient. Right-click the project and choose Properties> JAVA build path. Add the project: fundejb in projects. In libraries, click Add external jars... to add all jar files in the $ jboss_home/client directory to libraries.
Finally, write the client code:


Package qiya. Deng. client;
// Save the Import
Public class statelessfundmanagertestclient extends jframe implements
Actionlistener {

Double balance = 0;
Jtextfield amount = new jtextfield (10 );
Jbutton addfunds = new jbutton ("add funds ");
Jbutton withdrawfunds = new jbutton ("withdraw funds ");
String MSG = "current account balance ";
String strbal = "0 ";
Jlabel status;
Statelessfundmanager manager;
Numberformat currencyformatter;

Public statelessfundmanagertestclient (){
Super ("fund manager ");
}

Public static void main (string [] ARGs ){
New statelessfundmanagertestclient (). INIT ();
}

Private void Init (){

Buildgui ();

Addwindowlistener (New windowadapter (){
Public void windowclosing (invalid wevent event ){
System. Exit (0 );
}
});

Addfunds. addactionlistener (this );
Withdrawfunds. addactionlistener (this );

Createfundmanager ();

Currencyformatter = numberformat. getcurrencyinstance ();
String currencyout = currencyformatter. Format (0 );
Status. settext (MSG + currencyout );

Pack ();
Show ();
}

Private void buildgui (){
Gridbaglayout GL = new gridbaglayout ();
Gridbagconstraints GC = new gridbagconstraints ();
Container = getcontentpane ();
Container. setlayout (GL );

GC. Fill = gridbagconstraints. Both;
Jlabel label = new jlabel ("Enter amount ");
Gl. setconstraints (Label, GC );
Container. Add (Label );

GC. gridwidth = gridbagconstraints. remainder;
Gl. setconstraints (amount, GC );
Container. Add (amount );

Gl. setconstraints (addfunds, GC );
Container. Add (addfunds );
Gl. setconstraints (withdrawfunds, GC );
Container. Add (withdrawfunds );

Status = new jlabel (MSG );
Gl. setconstraints (status, GC );
Container. Add (Status );
}

Public void createfundmanager (){
Try {
Properties prop = new properties ();
Prop. Put (context. initial_context_factory, "org. jnp. Interfaces. namingcontextfactory ");
Prop. Put (context. provider_url, "localhost: 1099 ");
Context initial = new initialcontext (PROP );
Object objref = initial. Lookup ("EJB/statelessfundmanager"); // Jini-name
Statelessfundmanagerhome =
(Statelessfundmanagerhome) portableremoteobject. Narrow (objref, statelessfundmanagerhome. Class );
Manager = home. Create ();
} Catch (classcastexception e ){
E. printstacktrace ();
} Catch (RemoteException e ){
E. printstacktrace ();
} Catch (namingexception e ){
E. printstacktrace ();
} Catch (createexception e ){
E. printstacktrace ();
}
}

Public void actionreceivmed (actionevent e ){



If (E. getactioncommand (). inclusignorecase ("withdraw funds ")){
System. Out. println ("withdraw funds ");
}
If (E. getactioncommand (). inclusignorecase ("add funds ")){
System. Out. println ("add funds ");
}

If (E. getsource (). Equals (addfunds )){
System. Out. println ("addfunds ");
Try {
Status. settext (MSG + currencyformatter. Format (Manager. addfunds (0, double. parsedouble (amount. gettext ()))));
} Catch (numberformatexception E1 ){
E1.printstacktrace ();
} Catch (RemoteException E1 ){
E1.printstacktrace ();
}
}
If (E. getsource (). Equals (withdrawfunds )){
System. Out. println ("withdrawfund ");
Try {
Status. settext (MSG + currencyformatter. Format (Manager. withdrawfunds (100, double. parsedouble (amount. gettext ()))));
} Catch (numberformatexception E1 ){
E1.printstacktrace ();
} Catch (RemoteException E1 ){
E1.printstacktrace ();
} Catch (insufficientbalanceexception E1 ){
E1.printstacktrace ();
}
}
}

}

Then, you can run the program for testing:

Congratulations, you have achieved this. You have basically established a perceptual knowledge of EJB. You can refer to the materials for further study.

 

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.