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