我的EJB学习历程(四)

王朝java/jsp·作者佚名  2006-01-30
窄屏简体版  字體: |||超大  

前边的程序好用吧,我觉得比书上写的麻烦的程序强多了,所以还是得用工具的。

终于有了点EJB方面的成就,趁热打铁,现在马上就看第三章。

第一章 开发会话BEAN

大家都知道了,会话BEAN又分为状态会话BEAN和无状态会话BEAN。

书上总结了他们的共同点是:

1. 两者都可实现javax.ejb.SessionBean接口,因此具有相同的容器回调。容器回调?现在中国的翻译啊!

2. 两者都表示生成BEAN的客户的专用资源。

3. 两者都可以构造一个进程或一个任务模型

4. 两者都可以更新共享数据,但不象实体BEAN中一样表示共享数据。

NND,说了这么多的共同点,说实话,我一点都不知道他说的是什么意思?为什么要这么专业??:(

不过不同点但是很清楚:

状态会话BEAN可以在客户访问之间保存数据,而无状态则不可以!

管他呢,从例子上找切入点。

无状态会话BEAN的一个简单例子是计算数字,数字可以任意复杂,但是只在客户程序中存放。哦!是这样啊,其实就相当没有SESSION拉,但是好象还不能这么说,姑且先这么理解。

好拉,这次我决定采用书上的例子,书上的例子相当于一个BANK的系统!夸张,其实就是加减数字的BEAN。

这样,我们还是用(三)里边讲的方法来部署他:

记得是一个无状态会话BEAN的例子啊。

新建一个实现类,我的是这么写的:

package com.testing.fund;

import javax.ejb.SessionBean;

/**

* @ejb.bean name="FundManger"

* jndi-name="FundMangerBean"

* type="Stateless"

*

*--

* This is needed for JOnAS.

* If you are not using JOnAS you can safely remove the tags below.

* @jonas.bean ejb-name="FundManger"

* jndi-name="FundMangerBean"

*

*--

**/

public abstract class FundMangerBean implements SessionBean {

/**

* @ejb.interface-method

* view-type="remote"

**/

public double add(double b,double a){

b=b+a;

return b;

}

/**

* @ejb.interface-method

* view-type="remote"

**/

public double withdraw(double b,double a){

b=b-a;

return b;

}

}

要记得这里边的ADD和WITHDRAW方法不是手动加上的啊,是用WIZARD的啊。要是不会的话,看我的(三)去!!!!

然后按照(三)里边的方法部署到JBOSS上边去!

一切成功的话,还是按照(三)的方法写一个实现类!我的是这样写的,和书上有点不一样,不过实现方法是完全一样的!

package com.testing.client;

import java.rmi.RemoteException;

import java.util.Hashtable;

import javax.ejb.CreateException;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.text.*;

import com.testing.fund.FundManger;

public class FundTest extends JFrame implements ActionListener{

public FundTest(){

super("fund manger");

}

private com.testing.fund.FundMangerHome getHome() throws NamingException {

return (com.testing.fund.FundMangerHome) getContext().lookup(

com.testing.fund.FundMangerHome.JNDI_NAME);

}

private InitialContext getContext() throws NamingException {

Hashtable props = new Hashtable();

props.put(

InitialContext.INITIAL_CONTEXT_FACTORY,

"org.jnp.interfaces.NamingContextFactory");

props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");

// This establishes the security for authorization/authentication

// props.put(InitialContext.SECURITY_PRINCIPAL,"username");

// props.put(InitialContext.SECURITY_CREDENTIALS,"password");

InitialContext initialContext = new InitialContext(props);

return initialContext;

}

public void testBean() {

buildGUI();

addWindowListener(new WindowAdapter(){public void WindowClosing(WindowEvent evt){System.exit(0);}});

CreateFund();

addFund.addActionListener(this);

withdraw.addActionListener(this);

currencyFomat=NumberFormat.getCurrencyInstance();

String currency=currencyFomat.format(0);

status.setText(msg+currency);

pack();

show();

}

public void actionPerformed(ActionEvent e){

String str=amout.getText();

try{

if(e.getSource()==addFund){

balance=(double)myBean.add(balance,Double.parseDouble(str));

currencyFomat=NumberFormat.getCurrencyInstance();

strBar=currencyFomat.format(balance);

status.setText(msg+strBar);

}

if(e.getSource()==withdraw){

balance=(double)myBean.withdraw(balance,Double.parseDouble(str));

currencyFomat=NumberFormat.getCurrencyInstance();

strBar=currencyFomat.format(balance);

status.setText(msg+strBar);

}

}catch(Exception ex){}

}

public void CreateFund(){

try {

myBean = getHome().create();

//--------------------------------------

//This is the place you make your calls.

//System.out.println(myBean.callYourMethod());

} catch (RemoteException e) {

e.printStackTrace();

} catch (CreateException e) {

e.printStackTrace();

} catch (NamingException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

FundTest test = new FundTest();

test.testBean();

}

public void buildGUI(){

GridBagLayout gl=new GridBagLayout();

GridBagConstraints gc=new GridBagConstraints();

Container container=getContentPane();

container.setLayout(gl);

gc.fill=GridBagConstraints.BOTH;

JLabel label=new JLabel("enter amout");

gl.setConstraints(label,gc);

container.add(label);

gc.gridwidth=GridBagConstraints.REMAINDER;

gl.setConstraints(amout,gc);

container.add(amout);

gl.addLayoutComponent(addFund,gc);

container.add(addFund);

gl.addLayoutComponent(withdraw,gc);

container.add(withdraw);

status=new JLabel(msg);

gl.addLayoutComponent(status,gc);

container.add(status);

}

double balance=100;

JTextField amout=new JTextField(10);

JButton addFund=new JButton("add funds");

JButton withdraw=new JButton("withdraw funds");

String msg="current funds is:";

String strBar="0";

JLabel status;

FundManger myBean;

NumberFormat currencyFomat;

}

运行为JAVA 应用程序!成功了!界面如下:

现在来想想为什么是无状态会话BEAN了,其实这个EJB就相当于一个简单的应用程序后台数据处理器!你的所有数据记录信息全部都在前台了,也就是上边这个界面上边。

OK,下节课就是状态会话BEAN了。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航