分类:
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();
}
}