关闭

关于EJB3的一个简单例子

368人阅读 评论(0) 收藏 举报

EJB3已经变得很简单了,在这记上一笔。

    关于EJB3,可参阅:http://www.foshanshop.net/

    服务器选择Jboss。客户端需要导入Jboss/client/jbossall-client.jar才能调用发布在Jboss中的EJB

    代码:

    HelloBeanLocal.java

    view plaincopy to clipboardprint?
    package session;

    import javax.ejb.Local;

    @Local
    public interface HelloBeanLocal {
    }

    package session;

    import javax.ejb.Local;

    @Local
    public interface HelloBeanLocal {
    }

    HelloBeanRemote.java

    view plaincopy to clipboardprint?
    package session;

    import javax.ejb.Remote;

    @Remote
    public interface HelloBeanRemote {

        public String sayHello(String name);
    }

    package session;

    import javax.ejb.Remote;

    @Remote
    public interface HelloBeanRemote {

     public String sayHello(String name);
    }

    HelloBean.java

    view plaincopy to clipboardprint?
    package session;

    import javax.ejb.Stateless;

    @Stateless
    public class HelloBean implements HelloBeanLocal, HelloBeanRemote {

        public String sayHello(String name) {

            System.out.println("服务器端输出:" + name);
            return "Hello, " + name;
        }
    }

    package session;

    import javax.ejb.Stateless;

    @Stateless
    public class HelloBean implements HelloBeanLocal, HelloBeanRemote {

     public String sayHello(String name) {

      System.out.println("服务器端输出:" + name);
      return "Hello, " + name;
     }
    }

    HelloBeanClient.java

    view plaincopy to clipboardprint?
    import java.util.*;
    import javax.naming.*;
    public class HelloBeanClient {

        public static void main(String[] args)throws Exception {

    //      Hashtable properties=new Hashtable();
    //      properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
    //      properties.put(Context.PROVIDER_URL,"jnp://127.0.0.1");
    //      InitialContext ctx=new javax.naming.InitialContext(properties);

            Properties props = new Properties();
            props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
            props.setProperty("java.naming.provider.url", "127.0.0.1:1099");
            props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
            InitialContext ctx = new InitialContext(props);

            session.HelloBeanRemote remote=(session.HelloBeanRemote)ctx.lookup("HelloBean/remote");
            System.out.println(remote.sayHello("Beijing"));
            ctx.close();
        }
    }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:160530次
    • 积分:2470
    • 等级:
    • 排名:第14954名
    • 原创:61篇
    • 转载:156篇
    • 译文:0篇
    • 评论:22条
    最新评论