做了一个ejb的小例子,开始利用jboss5出现了问题,后来换了3的版本就好了。下面是代码贴出来,主要是别人的代码改的,相信很多人都看过这个代码了。
package com.myejb.greet;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Greet extends EJBObject{
public double calculateMagic(double seed) throws RemoteException;
}
package com.myejb.greet;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class GreetBean implements SessionBean {
private static final long serialVersionUID = 6729996959104578698L;
public double calculateMagic(double seed) {
System.out.println("Someone called `calculateMagic!'");
return seed * Math.random();
}
public GreetBean() {
}
public void ejbCreate() {
System.out.println("Create Greet EJB.");
}
public void ejbRemove() {
System.out.println("Remove Greet EJB.");
}
public void ejbActivate() {
System.out.println("Activate Greet EJB");
}
public void ejbPassivate() {
System.out.println("Passivate Greet EJB");
}
/**
* Set context for `Greet' EJB
*/
public void setSessionContext(SessionContext sc) {
System.out.println("Set context for Greet EJB");
}
}
package com.myejb.greet;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface GreetHome extends EJBHome {
Greet create() throws RemoteException, CreateException;
}
下面是META-INF下面的XML配置文件:
ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<description>jBoss test application </description>
<display-name>Test</display-name>
<enterprise-beans>
<session>
<ejb-name>GreetEJB</ejb-name>
<home>com.myejb.greet.GreetHome</home>
<remote>com.myejb.greet.Greet</remote>
<ejb-class>com.myejb.greet.GreetBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
jboss.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>GreetEJB</ejb-name>
<jndi-name>GreetingEJB</jndi-name>
</session>
<secure>false</secure>
</enterprise-beans>
</jboss>
下面是测试java类:
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import com.myejb.greet.Greet;
import com.myejb.greet.GreetHome;
public class TestMain {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");
env.put(Context.PROVIDER_URL, "localhost:1099");
try {
Context jndiContext = new InitialContext(env);
System.out.println("Got context");
Object ref = jndiContext.lookup("GreetingEJB");
System.out.println("Got reference");
GreetHome home = (GreetHome) PortableRemoteObject.narrow(ref, GreetHome.class);
Greet greet = home.create();
System.out.print("The magic number from server is ");
System.out.println(greet.calculateMagic(123.456));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}