Chinaunix首页 | 论坛 | 博客
  • 博客访问: 649017
  • 博文数量: 632
  • 博客积分: 39960
  • 博客等级: 大将
  • 技术积分: 4975
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-16 18:20
文章分类

全部博文(632)

文章存档

2011年(1)

2008年(631)

我的朋友

分类:

2008-10-16 18:23:32

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

    关于EJB3,可参阅:

    选择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();
        }
    }

【责编:landy】

--------------------next---------------------

阅读(326) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~