基于EJB3的一个简单例子

来源:互联网 发布:知乎改名 编辑:程序博客网 时间:2024/06/11 19:54

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

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

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

代码:

HelloBeanLocal.java

package session;import javax.ejb.Local;@Localpublic interface HelloBeanLocal {}

 

HelloBeanRemote.java

package session;import javax.ejb.Remote;@Remotepublic interface HelloBeanRemote {public String sayHello(String name);}

 

HelloBean.java

package session;import javax.ejb.Stateless;@Statelesspublic class HelloBean implements HelloBeanLocal, HelloBeanRemote {public String sayHello(String name) {System.out.println("服务器端输出:" + name);return "Hello, " + name;}}

 

HelloBeanClient.java

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();}}
原创粉丝点击