JBoss很容易安装和运行,而且它是免费的——它以Lesser GNU Public License(LGPL)的方式发行。在JBoss上部署EJB几乎就象安装和运行应用服务器本身一样简单。尽管如此,在JBoss上部署Bean时,我们仍旧发现了一些细微的差别。 首先,在ejb-jar.xml部署描述器的编写格式方面,我们遇到了一个问题。JBoss希望元素的开始标记和结束标记位于同一行上,而不是把它们分开到多个行。例如,下面的ejb-jar.xml部署描述器虽然顺利通过了WebLogic和WebSphere XML解析器的检查,但在JBoss上会出现部署错误: 【初始的ejb-jar.xml文件】<?xml version="1.0"?>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>
EjbLdap
</ejb-name>
<home>
HelloWorldLdap.EjbLdapHome
</home>
<remote>
HelloWorldLdap
</remote>
<ejb-class>
HelloWorldLdap.EjbLdapbean
</ejb-class>
<session-type>
Stateless
</session-type>
<transaction-type>
Bean
</transaction-type>
</session>
</enterprise-beans>
</ejb-jar> 纠正格式错误之后得到的ejb-jar.xml部署描述器如下所示: 【修改后的ejb-jar.xml】<?xml version="1.0"?>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name> EjbLdap</ejb-name>
<home> HelloWorldLdap.EjbLdapHome</home>
<remote> HelloWorldLdap</remote>
<ejb-class> HelloWorldLdap.EjbLdapbean</ejb-class>
<session-type> Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar> 另外,我们还必须增加一个名为jboss.xml的部署描述器,把它加入到META-INF目录。jboss描述器与WebLogic的weblogic-ejb-jar.xml部署描述器相似,它的作用是把EJB名称绑定到一个JNDI树上的名称。 部署JBoss EJB简单到只需把jar文件复制到部署目录,该目录就在JBoss安装的根目录之下。JBoss支持EJB热部署,有关Bean部署的信息直接在命令行上输出,从而方便了观察(在Unix上,它就在启动JBoss的那个shell窗口)。然而,在JBoss上部署EJB也有一个小小的不便之处,这就是我们不能浏览JNDI树验证Bean已经成功注册,不能在JNDI树中看到Bean的注册名字。如果我们未能在jboss.xml部署描述器中指定Bean的JNDI名称,默认情况下,JBoss将使用ejb-jar.xml中<ejb-name>标记提供的值注册它。如果加入了jboss.xml,我们可以利用<jndi-name>标记指定Bean在JBoss JNDI树上的注册名字。 总地看来,JBoss作为一个轻量级的应用服务器,它在注册EJB的时候只需要很简单的部署操作。我们只需把jar文件复制到部署目录,然后观察控制台上的输出结果。JBoss支持我们所谓的Bean热部署。如果我们在构造ejb-jar.xml文件时足够小心,按照要求正确格式化了ejb-jar.xml文件,原先的EJB可以不经过任何修改直接在JBoss服务器上部署。然而,JBoss也要用到附加的部署描述器,例如jboss.xml和jaws.xml,这使得部署复杂Bean的过程更令人感兴趣。
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>
EjbLdap
</ejb-name>
<home>
HelloWorldLdap.EjbLdapHome
</home>
<remote>
HelloWorldLdap
</remote>
<ejb-class>
HelloWorldLdap.EjbLdapbean
</ejb-class>
<session-type>
Stateless
</session-type>
<transaction-type>
Bean
</transaction-type>
</session>
</enterprise-beans>
</ejb-jar> 纠正格式错误之后得到的ejb-jar.xml部署描述器如下所示: 【修改后的ejb-jar.xml】<?xml version="1.0"?>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name> EjbLdap</ejb-name>
<home> HelloWorldLdap.EjbLdapHome</home>
<remote> HelloWorldLdap</remote>
<ejb-class> HelloWorldLdap.EjbLdapbean</ejb-class>
<session-type> Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar> 另外,我们还必须增加一个名为jboss.xml的部署描述器,把它加入到META-INF目录。jboss描述器与WebLogic的weblogic-ejb-jar.xml部署描述器相似,它的作用是把EJB名称绑定到一个JNDI树上的名称。 部署JBoss EJB简单到只需把jar文件复制到部署目录,该目录就在JBoss安装的根目录之下。JBoss支持EJB热部署,有关Bean部署的信息直接在命令行上输出,从而方便了观察(在Unix上,它就在启动JBoss的那个shell窗口)。然而,在JBoss上部署EJB也有一个小小的不便之处,这就是我们不能浏览JNDI树验证Bean已经成功注册,不能在JNDI树中看到Bean的注册名字。如果我们未能在jboss.xml部署描述器中指定Bean的JNDI名称,默认情况下,JBoss将使用ejb-jar.xml中<ejb-name>标记提供的值注册它。如果加入了jboss.xml,我们可以利用<jndi-name>标记指定Bean在JBoss JNDI树上的注册名字。 总地看来,JBoss作为一个轻量级的应用服务器,它在注册EJB的时候只需要很简单的部署操作。我们只需把jar文件复制到部署目录,然后观察控制台上的输出结果。JBoss支持我们所谓的Bean热部署。如果我们在构造ejb-jar.xml文件时足够小心,按照要求正确格式化了ejb-jar.xml文件,原先的EJB可以不经过任何修改直接在JBoss服务器上部署。然而,JBoss也要用到附加的部署描述器,例如jboss.xml和jaws.xml,这使得部署复杂Bean的过程更令人感兴趣。
解决方案 »
- SSH hibernate + oracle做好的一个项目 现在数据库要换成sybase,求高手指点下整个流程?大概讲讲就好
- 短信平台与业务系统通讯方式设计
- spring中DelegatingActionProxy
- struts2.0的 <s:textfield>value问题
- 编程中字符集问题,谢谢参与讨论!!
- 这个异常,太怪了,求求大家帮我解决阿
- SQL Server的Windows认证,JDBC能做吗?
- 哪位大狭有"Practical J2EE Application Architecture"电子版,扔一个上来,50分相送!!
- 十万火急 怎么样在jb9中开发struts1.1啊?
- spring与mybatis整合遇到bug
- 帮忙写个代码
- hibernate中的session同jsp中的sesssion有什么关系?有什么不同?
呵呵,我猜的。
请将:
Object objref=initial.lookup("ejb/ProductBean");
改为:Object objref=initial.lookup("java:comp/env/ejb/ProductBean");
这个才是测试j2ee环境下的jndi调用
props.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
Properties props = new Properties();
props.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
props.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
import javax.rmi.PortableRemoteObject;import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.EntityBean;
import javax.naming.InitialContext;
import javax.naming.NamingException;import java.util.Date;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;import oa.business.*;public class DepartmentTest{
public static InitialContext context;
public static Object obj;
public static DepartmentHome departHome;
public static Department department;
public static void main(String[] args){
Properties prop = new Properties();
prop.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
prop.setProperty("java.naming.provider.url","localhost:1099");
prop.setProperty("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
try{
context = new InitialContext(prop);
System.out.println("Has got context!");
}catch(javax.naming.NamingException e){
System.out.println("NamingException:"+e.toString());
}
try{
obj = context.lookup("oa/department");
System.out.println("Has got stub!");
}catch(NamingException e){
System.out.println("invoking lookup() method catch Exception:"+e.toString());;
}
//try{
departHome = (DepartmentHome)PortableRemoteObject.narrow(obj,DepartmentHome.class);;
System.out.println("Has narrow to DepartmentHome.class!");
//}catch(CreateException e){
// System.out.println("narrow method has Exception:"+e.toString());
// }
String id = "020";
try{
department = departHome.findByPrimaryKey(id);
System.out.println("id="+department.getId());
System.out.println("name="+(new String(department.getName().getBytes("iso-8859-1"),"gbk")));
System.out.println("level="+department.getLevel());
System.out.println("parentId"+department.getParentId());
Collection collection = departHome.findAll();
if(!collection.isEmpty()){
Iterator iterator = collection.iterator();
while(iterator.hasNext()){
department = (Department)iterator.next();
System.out.println("id="+department.getId());
System.out.println("name="+(new String(department.getName().getBytes("iso-8859-1"),"gbk")));
System.out.println("level="+department.getLevel());
System.out.println("parentId"+department.getParentId());
}
}
}/*catch(CreateException e){
System.out.println("findByprimaryKey method has Exception:"+e.toString());
}*/catch(FinderException e){
System.out.println("findByPrimaryKey method has FinderException:"+e.toString());
try{
departHome.create("020",(new String("aaa".getBytes(),"iso-8859-1")),"02","01");
departHome.create("021",(new String("ddd".getBytes(),"iso-8859-1")),"02","01");
departHome.create("022",(new String("fff".getBytes(),"iso-8859-1")),"01","root");
departHome.create("023",(new String("sss".getBytes(),"iso-8859-1")),"02","01");
departHome.create("024",(new String("ffff".getBytes(),"iso-8859-1")),"02","01");
}catch(RemoteException ex){
System.out.println("departHome.create() method has RemoteException:"+ex.toString());
}catch(CreateException ex){
System.out.println("departHome.create() method has CreateException:"+ex.toString());
}
catch(java.io.UnsupportedEncodingException codeing){
System.out.println("encodingException!");
}
}catch(RemoteException e){
System.out.println("findByPrimaryKey method has RemoteException:"+e.toString());
}catch(java.io.UnsupportedEncodingException e){
System.out.println("encodingException!");
}
}
}