Tutorial for building J2EE Applications using JBOSS and ECLIPSE(4)

本文介绍如何创建有状态会话EJB组件。详细说明了创建有状态会话Bean、无状态Bean、DAO接口,添加业务和回调方法,实现DAO接口,部署Bean,创建并测试客户端等步骤,最终成功部署和测试有状态会话Bean。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Chapter 4.

blurulr6.gif

Creating a Stateful Session Bean

This chapter covers how to create a stateful session EJB component. Unlike stateless beans, stateful bean instances are associated with a particular client session and can therefore maintain information useful during the lifetime of that session. A classic and familiar example of this in a web e-commerce context is that of a shopping cart.

We'll try something a little less ambitious for the time being! This bean is similar to the StoreAccess Bean created in the previous chapter except that it will store the userID of the client, after authentication is successful.

StoreAccessState.png


Note : This bean will not be used further in tutorial, as this chapter demonstrates how stateful beans are created.

Tasks :

  1. Create a Stateful Session Bean named StoreAccessState.

  2. Add a business method in the bean named loginUser with the signature

    public String loginUser (String username, String password)

  3. Add a call back method ejbCreate with the signature

    public void ejbCreate (String userID)

  4. Generate the DAO interface.

  5. Access the DAO named StoreAccesDAOImpl under package au.com.tusc.dao, created and implemented in the previous chapter. This DAO has a method named loginUser, called by loginUser in the bean with the signature

    public String loginUser (String username, String password)

  6. Deploy the StoreAccessState bean.

  7. Create your test client named SessionStateClient under package au.com.tusc.client.

  8. Run your client and test the bean.



Create Stateless Bean :


Go To Package Explorer > Expand Mystore (project) node > select src, right click and a menu will pop up.

On pop up menu > New > Lomboz EJB Creation Wizard.

Enter the package name au.com.tusc.sessionState, bean name StoreAccessState and select bean type as stateful > Finish.

This will create package named au.com.tusc.sessionState under src and StoreAccessStateBean under that package as shown below.

StaoreAccessState.png
As we can see from the figure above it has created a class level tag @ejb.bean, which has assigned the bean type, name and its JNDI name which will be generated in the Home interface. This tag will also generate deployment descriptors in ejb-jar.xml and jboss.xml file as well once you generate your EJB classes, which is covered later on in this chapter.

Note: It will generate the bean name, jndi-name and type of bean in file. Also, the name of file is appended with word 'Bean' as you gave the name of the bean as StoreAccess only. So be careful with your naming convention, as you only need specify the bean name in the wizard (ie. without a 'Bean' suffix as the wizard will append that for you).

SessionBeanGeneartedDescView.png


Create DAO Interface :


Since we are going to use a DAO to access the database for this Stateful Bean, we will use the StoreAccesDAOImpl class which was created and implemented in the previous chapter. This will provide the implementation for the generated DAO interface.

Now go to your Bean class and declare this tag at class level (i:e; at top), as described below to generate the DAO interface.


StoreAccessStateDAOImpl.png

Expand the StoreAccessStateBean node under Package Explorer. Right click and a pop up menu will appear.

On that menu go to Lomboz J2EE > Add EJB to module. Select EJB MyStoreMgr > OK.

Note : This step is covered in previous chapter.


Expand MyStoreMgr node under MyStore Project in Package Explorer. Right click and a pop up menu will appear.

Go to Lomboz J2EE > Generate EJB Classes as shown in figure at right.

GenerateEJBClassesNow.png



This tag also generates the following descriptors in jboss.xml as shown below.

jboss.png


Add Business Method :

The next step is to add a business method to the bean.

Go to StoreAccesStateBean node > right click > select New on pop up menu.

Select Lomboz EJB Method Wizard.

Note : This step is covered in the previous chapter.

Add a business method with the following signature..

public String loginUser (String username, String password).

Select Method Type as Business and Interface as Remote as shown below..

AddBusinessBMethod.png

This wizard generates a loginUser method in our bean class, with the method level tag' @ejb.interface' shown below.

SessionBeanInterfaceTag.png

This tag is responsible for generating this method in the Remote Interface (in this case it is StoreAccessSate which will be created once you generate your classes).



Add Callback Methods :

In contrast to the Stateless bean, the ejbCreate method will have an argument. This will be used to initialize a persistent field in the bean.

Add that field, and add accessor and mutator methods to access that field as shown below.





    
    
    





    
    
    


Now add the ejbCreate method with the signature

public void ejbCreate (String userID)

Assign userID to the persistent field userID we created above as shown.

StoreAccessStateEjbCreate.png
Now, the other two callback methods required to complete this bean are

1. setSessionContext.

2. unsetSessionContext.

Add a field to store sessionContext.



Add a method setSessionContext with sessionContext as its parameter and assign that to the sessionContext variable as shown below.

StoreAccessStateSetSessionContext.png
Similarly, add a method unsetSessionContext which sets the context variable to null as shown above.

Note : The StoreAccessStateSession class inherits the StoreAccessStateBean abstract class and implements SessionBean, which will override all methods of the interface SessionBean. So, after completing the methods in your bean class, generate your EJB classes again. The SessionContext methods will be overridden, as discussed in previous chapter.

Generate EJB classes.

Note : The steps to generate the EJB classes are discussed above and in the previous chapter.



Implement DAO Interface :

We don't have to implement the DAO interface as we are using the StoreAccessDAOImpl class created and implemented in the previous chapter.

So go into the StoreAccessDAOImpl class and change the class declaration statement, so that it implements StoreAccessStateDAO interface, as shown below in code snippet.

StoreAccessDAOImpl.png


Now, all methods are implemented, and only deployment descriptors remain to be done.


Deploy Bean :


In order to deploy our bean we have to declare a few tags in StoreAccessStateBean class as shown below.

StoreAccessStateBeanAllDescriptors.png


Add the tag shown below in StoreAccessStateBean at class level (at the top).


This tag will generate deployment descriptors in ejb-jar.xml, as the bean has to know which datasource you are going to connect, what is its type, etc. This will generate these descriptors as shown below.

ejb-jarResourceRef.png


Add this tag shown below in StoreAccessStateBean at class level (at top).


This tag will generate deployment descriptors in jboss.xml, as the application server has to know with what jndi-name the datasource has been registered with. This will generate these descriptors as shown below.

jbossResourceRef.png


Add this tag shown below in StoreAccessStateBean at class level (at top).

 

This tag will generate deployment descriptors in jboss.xml, as the application server has to know which configuartion is to be read for Stateful Bean from standardjboss.xml under /opt/jboss/jboss-3.2.1/server/all/conf/. This will generate these descriptors in jboss.xml as shown below.

  
  
  
  
      
      
  

Note : This configuration is for container in Jboss to read for Stateful Session Bean. Jboss comes with timeout of 60 mins for stateful session bean. Change the <max-bean-life> attribute to 120 secs (i:e2 mins) and restart your server as shown below in code snippet from /opt/jboss/jboss-3.2.1/servaer/all/conf/standardjboss.xml.
































Now, everything is complete, and it's time to deploy the bean.

First, regenerate your EJB classes as shown in the steps above for the final time.

Note : We have regenerated the classes again and again in order to explain every step and its result. Once you are familiar with these steps you will only need to generate your classes a few times as you go. Either way, it doesn't affect your implementation, so you can (re)generate your classes however you prefer to work.

Go to Lomboz J2EE View > expand node MyStore > expand MyStoreMgr > select 'Jboss 3.2.1 ALL' .

Right click > select 'Debug Sever' on the pop up menu.

Go to MyStoreMgr node in LombozJ2EE view > right click > select Deploy on the pop up menu.

Note : These steps are covered in the previous chapter.

And now wait for your deployment result.

If everything goes fine, you will have this message under your console as shown below.

LombozJ2EEBeanDeployed.png


So, now your bean is deployed successfully, let's create a test client, which will invoke the loginUser method on StoreAccessStateBean.


Create your Test Client :


Go to Project MyStore node > select src node > right click.

Select New on pop up menu > select Lomboz EJB Test Client Wizard as shown below.

CreateEJBTestClient.png


Select package name au.com.tusc.client, with name SessionStateClient and select Ejb Home as au.com.tusc.sessionState.StoreAccessStateHome and Ejb Interface as au.com.tusc.sessionState.StoreAccessState as shown below.

CreateEJBTestClientPackageName.png


This will generate the necessary helper methods for you in your SessionStateClient class and you simply have to invoke the loginUser method on bean.

So add the lines of code shown below in the testBean method.





           



                        













      



        
















Test your Client :

Now, in order to test your client, Select SessionStateClient node > Go at top level menu and select the icon with the 'Man running'.

On that select 'Run as' > select 'Java Application'.

Now under your console, if you get your reply for 'ANDY' as 'U2', event after 1 min but after 3 mins NoSuchObjectException is caught then your stateful bean is successful as shown below.

NewTestClientRunState.png


Note : So our Stateful Session Bean is deployed and tested successfully. This particular Bean is not used again in this tutorial.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值