关于JBOSS 在两台电脑上布置EJB,一台作为SERVER,一台做为CLIENT的简单sample.
1首先启动 jboss :run -b 192.168.1.4 run.bat 这样可以启动server并绑定SERVER IP。
2在 server电脑上写以下代码。
一个EJB远程接口
一个EJB session bean
一个EJB远程 HOME
然后在meta-inf目录下写个 ejb-jar.xml
[quote]<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.1"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<display-name>ConverJAR</display-name>
<enterprise-beans>
<session>
<display-name>ConverterEJB</display-name>
<ejb-name>ConverterEJB</ejb-name>
<home>ConverterHome</home>
<remote>Converter</remote>
<ejb-class>ConverterEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
<security-identity>
<use-caller-identity/>
</security-identity>
</session>
</enterprise-beans>
</ejb-jar>[/quote]
和jboss.xml
[quote]<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 4.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd">
<jboss>
<enterprise-beans>
<session>
<ejb-name>ConverterEJB</ejb-name>
<jndi-name>MyConverter</jndi-name>
</session>
</enterprise-beans>
</jboss>[/quote]
然后绑定 JNDI
jndi.properties
[quote]java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=192.168.1.4:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces[/quote]
目录结构是
ejb_server
|___src
|___Converter.java,ConverterEJB,ConverterHome
|___META-INF/ejb-jar.xml,jboss.xml
|___jndi.properties
需要的jar有 jboss-j2ee.jar(一般也就是j2ee.jar)
3.CLIENT端
我在另一台电脑上写。
建立一个ejb_client 工程,
写个类
需要的JAR包是 jbossall-client.jar包,
还有就是要调用的server端的远程接口,只用Converter,ConverterHome接口就可以,其他什么SESSION BEAN,什么配置文件都不要,也不用安装jboss
其它要注意的小知识
1.jboss 里,MarshalledValueEX 类在E:\development\jboss-4.2.0.GA\jbossall-client.jar里
2.不需要环境变量里jboss_home.
3.在JBOSS里,JNDI树查看 :查找service=JNDIView ,进入该页面后点击 list方法,就会列出JBoss上所有的JNDI服务。
1首先启动 jboss :run -b 192.168.1.4 run.bat 这样可以启动server并绑定SERVER IP。
2在 server电脑上写以下代码。
一个EJB远程接口
import java.rmi.RemoteException;
import javax.ejb.*;
public interface Converter extends EJBObject {
public double dollarToYen(double dollars) throws RemoteException;
public double yenToEuro(double yen) throws RemoteException;
// Simple example.
public String helloEcho(String word) throws RemoteException;
}
一个EJB session bean
import java.rmi.RemoteException;
import javax.ejb.*;
public class ConverterEJB implements SessionBean {
private static final long serialVersionUID = 1L;
public double dollarToYen(double dollars) {
return dollars * 121.6000;
}
public double yenToEuro(double yen) {
return yen * 0.0077;
}
public String helloEcho(String word) {
System.out.println("Someone called 'Hello Echo Successed!'");
return "*********" + word + "*********";
}
public void ejbCreate() {
System.out.println("Ejb 4 is creating!...");
}
public void ejbPostCreate() {
}
public void ejbActivate() throws EJBException, RemoteException {
}
public void ejbPassivate() throws EJBException, RemoteException {
}
public void ejbRemove() throws EJBException, RemoteException {
}
public void setSessionContext(SessionContext arg0) throws EJBException,
RemoteException {
}
}
一个EJB远程 HOME
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface ConverterHome extends EJBHome {
Converter create() throws CreateException, RemoteException;
}
然后在meta-inf目录下写个 ejb-jar.xml
[quote]<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.1"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<display-name>ConverJAR</display-name>
<enterprise-beans>
<session>
<display-name>ConverterEJB</display-name>
<ejb-name>ConverterEJB</ejb-name>
<home>ConverterHome</home>
<remote>Converter</remote>
<ejb-class>ConverterEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
<security-identity>
<use-caller-identity/>
</security-identity>
</session>
</enterprise-beans>
</ejb-jar>[/quote]
和jboss.xml
[quote]<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 4.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd">
<jboss>
<enterprise-beans>
<session>
<ejb-name>ConverterEJB</ejb-name>
<jndi-name>MyConverter</jndi-name>
</session>
</enterprise-beans>
</jboss>[/quote]
然后绑定 JNDI
jndi.properties
[quote]java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=192.168.1.4:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces[/quote]
目录结构是
ejb_server
|___src
|___Converter.java,ConverterEJB,ConverterHome
|___META-INF/ejb-jar.xml,jboss.xml
|___jndi.properties
需要的jar有 jboss-j2ee.jar(一般也就是j2ee.jar)
3.CLIENT端
我在另一台电脑上写。
建立一个ejb_client 工程,
写个类
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class EJB_Client {
public static void main(String[]args){
Properties pros=new Properties();
pros.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
pros.setProperty("java.naming.provider.url", "jnp://192.168.1.4:1099");
try {
Context context=new InitialContext(pros);
Object objref=context.lookup("MyConverter");
ConverterHome home=(ConverterHome)PortableRemoteObject.narrow(objref, ConverterHome.class);
Converter currentConverter=home.create();
System.out.println(currentConverter.dollarToYen(100));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
需要的JAR包是 jbossall-client.jar包,
还有就是要调用的server端的远程接口,只用Converter,ConverterHome接口就可以,其他什么SESSION BEAN,什么配置文件都不要,也不用安装jboss
其它要注意的小知识
1.jboss 里,MarshalledValueEX 类在E:\development\jboss-4.2.0.GA\jbossall-client.jar里
2.不需要环境变量里jboss_home.
3.在JBOSS里,JNDI树查看 :查找service=JNDIView ,进入该页面后点击 list方法,就会列出JBoss上所有的JNDI服务。