簡単なステートレスSession Bean

ステートレスSession Beanの簡単なアプリケーションです。

このサンプルは、クライアントからhelloメソッドを呼び出す ステートレスSession Beanです。

ソースコードはsamples/j2ee/ejb/HelloServer/srcに、 ビルド済みアプリケーションはsamples/j2ee/ejb/HelloServer/jarsにあります。

 
ホーム・インタフェース

HelloHome.java


/*
  Copyright (C) NEC Corporation 2004
  NEC CONFIDENTIAL AND PROPRIETARY
  All rights reserved by NEC Corporation.
  This program must be used solely for the purpose for which
  it was furnished by NEC Corporation. No part of this
  program may be reproduced or disclosed to others, in any
  form, without the prior written permission of NEC
  Corporation. Use of copyright notice does not evidence
  publication of the program. 
 */
package sample;

import javax.ejb.*;

public interface HelloHome extends EJBHome {

    public Hello create() throws CreateException, java.rmi.RemoteException;
}

 
リモート・インタフェース

Hello.java


/*
  Copyright (C) NEC Corporation 2004
  NEC CONFIDENTIAL AND PROPRIETARY
  All rights reserved by NEC Corporation.
  This program must be used solely for the purpose for which
  it was furnished by NEC Corporation. No part of this
  program may be reproduced or disclosed to others, in any
  form, without the prior written permission of NEC
  Corporation. Use of copyright notice does not evidence
  publication of the program. 
 */
package sample;

import javax.ejb.*;
import java.rmi.*;

public interface Hello extends EJBObject {
    public String hello(String str) throws RemoteException;
}

 
Enterprise Beanクラス

HelloBean.java


/*
  Copyright (C) NEC Corporation 2004
  NEC CONFIDENTIAL AND PROPRIETARY
  All rights reserved by NEC Corporation.
  This program must be used solely for the purpose for which
  it was furnished by NEC Corporation. No part of this
  program may be reproduced or disclosed to others, in any
  form, without the prior written permission of NEC
  Corporation. Use of copyright notice does not evidence
  publication of the program. 
 */
package sample;

import javax.ejb.*;

public class HelloBean implements SessionBean {

    private SessionContext sessionContext;
    public HelloBean() {
    }

    public void setSessionContext(SessionContext arg0) {
        this.sessionContext = arg0;
    }

    public void ejbRemove() {
        // TODO 自動生成されたメソッド・スタブ
    }

    public void ejbActivate() {
        // TODO 自動生成されたメソッド・スタブ
    }

    public void ejbPassivate() {
        // TODO 自動生成されたメソッド・スタブ
    }

    public void ejbCreate() {
    }

    public String hello(String str) {
        return "Hello : " + str;
    }
}

 
クライアントクラス

Client.java


/*
  Copyright (C) NEC Corporation 2004
  NEC CONFIDENTIAL AND PROPRIETARY
  All rights reserved by NEC Corporation.
  This program must be used solely for the purpose for which
  it was furnished by NEC Corporation. No part of this
  program may be reproduced or disclosed to others, in any
  form, without the prior written permission of NEC
  Corporation. Use of copyright notice does not evidence
  publication of the program. 

*/
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import javax.ejb.*;
import sample.*;

public class Client {
    public static void main(String args[]) {
        new Client();
    }
    public Client() {
        try {
            InitialContext initial = new InitialContext();
            Object obj = initial.lookup("Hello");
            HelloHome home = (HelloHome)PortableRemoteObject.narrow(obj, HelloHome.class);
            Hello remote = home.create();
            String str = remote.hello("webotx");
            System.out.println(str);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}


 

Copyright (C) 1998 - 2004 NEC Corporation.All rights reserved.