<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    qiyadeng

    專注于Java示例及教程
    posts - 84, comments - 152, trackbacks - 0, articles - 34

    EJB入門--First EJB

    Posted on 2006-02-04 20:17 qiyadeng 閱讀(743) 評論(0)  編輯  收藏
     該文章在<<EJB編程指南>>的實例的基礎上建立的,主要是給新手一個比較直觀的例子和作為自己的日志,并不打算介紹EJB的原理性的東西。另外,由于本人水平有限,請不吝賜教。
        筆者使用的IDE為:Eclipse3.0+MyEclipse4.01GA
        J2EE容器為:JBoss4.0
       
        本文描述一個幫助存款和取款的無狀態會話Bean的完整開發及部署的過程。步驟如下:
    1、編寫無狀態會話Bean的實現類。
    2、編寫無狀態會話Bean的主接口和組件接口。
    3、將Bean匯編成應用程序,編寫部署描述項。
    4、在EJB服務器上部署應用程序。
    5、用Java應用程序進行測試。
    上面是主要的過程,有些步驟可以不用手工完成,通過IDE可以簡化開發過程,如果你對IDE的該功能不太清楚可以參考產品文檔(http: //myeclipseide.com/enterpriseworkbench/help/index.jsp?topic=/com.genuitec.myeclipse.doc/html/quickstarts/firstejb/index.html)。

    一、新建一個EJB Project,工程名字為FundEJB,其他默認就好。
    二、創建Session Bean:
        1、在src目錄下新建包:qiya.deng.fund.ejb,請注意包名最后一定要以ejb為后綴,因為后面我們需要使用的XDoclet工具。
        2、新建SessionBean class,命名為StatelessFundManagerEJB,要需要以EJB為后綴,原因同上,而且根據規范最好是以EJB或是Bean為后綴。
       

        3、配置XDoclet :
        右擊項目選擇Properties,選擇MyEclipse-XDoclet,點擊Add Stander...,選擇Standard EJB。
       
        選中Standard EJB,在ejbxdoclet上點擊右鍵添加Add,在其中選擇jboss,因為該例子中使用jboss作為應用服務器。選中jboss,修改下列屬性
        Version = 4.0
        destDir = src/META-INF
       修改完畢,點擊OK按鈕回到主窗口。

        4、運行Xdoclet:
        右擊項目選擇MyEclipse->run Xdoclet。運行是console窗口會產生提示信息,運行完畢可以看到目錄結構發生變化。
       
        5、編輯實現類StatelessFundManagerEJB:
        在編輯StatelessFundManagerEJB類之前選觀察下StatelessFundManager接口,一定可以發現這個遠程組件接口的接口方法和StatelessFundManager的方法是有對應關系的。
        在StatelessFundManager.java文件最后添加:

            /**
         *
         * @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;
        }
        重復第4步運行Xdoclet,之后觀察StatelessFundManager接口。

       6、部署該應用到EJB服務器:
        部署描述項在IDE自動生成了,該文件的位置在/META-INF/ejb-jar.xml。打開ejb-jar.xml,jboss.xml文件描述進行查看。
        利用MyEclipse提供的部署工具進行部署:
       
        然后運行JBoss容器,可以看到有如下信息提示:

       

        關于MyEclipse中Application Server的使用請查看文檔(http://www.myeclipseide.com/images/tutorials/quickstarts/appservers/)。
        到現在為止,你已經發布了一個簡單的無狀態的會話Bean。下面寫個簡單的應用程序進行測試.
       
    三、編寫進行測試的Java客戶端程序。
        客戶端程序可以是Web程序也可以是Application應用程序。這里以Application應用程序為例。
        同樣使用Eclipse,新建Java Project,這里命名為FundClient。右擊該項目選擇properties->Java Build path,在Projects中加入上面的Project:FundEJB。在Libraries中點擊Add External JARs...,把$JBoss_Home/client的目錄下的所有jar文件添加到Libraries中。
        最后,就是編寫客戶端代碼:

    package qiya.deng.client;
        //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(WindowEvent 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 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 home =
                    (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 actionPerformed(ActionEvent e) {
           
           
           
            if (e.getActionCommand().equalsIgnoreCase("Withdraw Funds")) {
                System.out.println("Withdraw Funds");
            }
            if (e.getActionCommand().equalsIgnoreCase("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();
                }
            }
        }

    }
        然后,你可以運行該程序進行測試了:
         
        至此,恭喜你,你已經大功告成,基本上對EJB建立了感性的認識,可以參考資料進行深入的學習了。

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 日韩精品无码免费一区二区三区 | 国产成人无码免费网站| 成年性午夜免费视频网站不卡| 亚洲AV成人精品网站在线播放| 国产精品极品美女自在线观看免费| 亚洲日韩在线中文字幕第一页| 一级午夜a毛片免费视频| 国产精品亚洲视频| 国产无遮挡又黄又爽免费网站| 亚洲中文字幕无码一区| 最新国产乱人伦偷精品免费网站| 亚洲国产成人片在线观看| 国产一区二区三区免费| 在线免费观看亚洲| av免费不卡国产观看| 亚洲成年人电影在线观看| 免费看国产精品3a黄的视频| 亚洲大码熟女在线观看| 亚洲国产成人VA在线观看| 国产精品永久免费视频| 亚洲AV人人澡人人爽人人夜夜| 桃子视频在线观看高清免费视频| 久久亚洲AV成人无码| 在线中文高清资源免费观看| 噜噜噜亚洲色成人网站| 国产亚洲免费的视频看| 亚洲精品在线免费观看| 亚洲精品成a人在线观看☆| 亚洲国产精品成人| 久久黄色免费网站| 亚洲 欧洲 视频 伦小说| 国产成人青青热久免费精品 | 美女内射无套日韩免费播放| 自拍偷区亚洲国内自拍| 亚洲国产成人久久综合区| 国产成人免费视频| 亚洲精品乱码久久久久久蜜桃图片 | 日本激情猛烈在线看免费观看| 亚洲AV无码不卡无码| 国产成人精品免费视| 污污免费在线观看|