原创作者: hwhuang   阅读:5344次   评论:1条   更新时间:2011-06-01    
昨天,打算把Ejb整理下,最早用到Ejb+Jboss是在三年前了。技术的关注度,往往同你所在项目有关。所以,昨天用Ejb也是碰到一个配置问题,搞了我半天。
Point:
Ejb定义
配置
Exception
实例(Sample)

一 Ejb 定义
   所谓Ejb,即Enterprise Java bean----企业级的Java bean.但同Java Bean有 区别:
  1.Javabean是面向于业务逻辑和表示层的显示。对于简单的服务器端应用而言,是个不错的选择。其只是一个组件。
  2.Ejb是部署在服务器上的可执行组件或商业对象。通过Socket允许对其进行远程访问或在特定服务器(eg:Jboss/tomcat/Weblogic/Websphere等)上进行安装或部署。用户不需要关心其底层操作,只需要关心外部实现而已。Ejb是一个容器,是一个组件架构。
二 Configuration(配置):
   关于Ejb3+Jboss 4的开发配置,在网上挺多,工作和即将工作的同仁,在google上search一下,就有很多的文章。在这里,我只是简单的说明下:
   1. Jboss配置:
   在以下web Site上download后,然后解压缩即可,建议:解压缩任何文件,最后不要使文件路径包含空格之类的字符,有时候会出现一些意想不到的错误。
  web site:http://www.jboss.org/jbossas/downloads
  注意:Jboss 4和Jboss 5在Jbossall_client.jar(Jboss 4)和Jboss_all_client.jar(Jboss 5)上有很大区别。
   设置系统变量:
   JBOSS_HOME:Jboss的解压缩路径
    Path:%JBOSS_HOME%/bin
   运行Bin目录下的Run.bat文件,以下是运行的log记录:
  02:44:08,437 INFO  [Server] Starting JBoss (MX MicroKernel)...
02:44:08,437 INFO  [Server] Release ID: JBoss [Trinity] 4.2.0.GA (build: SVNTag=JBoss_4_2_0_GA date=200705111440)
02:44:08,453 INFO  [Server] Home Dir: C:\TDDOWNLOAD\jboss-4.2.0.GA
02:44:08,453 INFO  [Server] Home URL: file:/C:/TDDOWNLOAD/jboss-4.2.0.GA/
02:44:08,453 INFO  [Server] Patch URL: null
02:44:08,453 INFO  [Server] Server Name: default
02:44:08,453 INFO  [Server] Server Home Dir: C:\TDDOWNLOAD\jboss-4.2.0.GA\server\default
02:44:08,453 INFO  [Server] Server Home URL: file:/C:/TDDOWNLOAD/jboss-4.2.0.GA/server/default/
02:44:08,468 INFO  [Server] Server Log Dir: C:\TDDOWNLOAD\jboss-4.2.0.GA\server\default\log
02:44:08,468 INFO  [Server] Server Temp Dir: C:\TDDOWNLOAD\jboss-4.2.0.GA\server\default\tmp
02:44:08,468 INFO  [Server] Root Deployment Filename: jboss-service.xml
02:44:09,062 INFO  [ServerInfo] Java version: 1.6.0_13,Sun Microsystems Inc.
02:44:09,062 INFO  [ServerInfo] Java VM: Java HotSpot(TM) Client VM 11.3-b02,Sun Microsystems Inc.
02:44:09,062 INFO  [ServerInfo] OS-System: Windows XP 5.1,x86
02:44:09,859 INFO  [Server] Core system initialized
02:44:14,078 INFO  [WebService] Using RMI server codebase: http://127.0.0.1:8083/
02:44:14,078 INFO  [Log4jService$URLWatchTimerTask] Configuring from URL: resource:jboss-log4j.xml
02:44:15,062 INFO  [TransactionManagerService] JBossTS Transaction Service (JTA version) - JBoss Inc.
02:44:15,062 INFO  [TransactionManagerService] Setting up property manager MBean and JMX layer
02:44:15,484 INFO  [TransactionManagerService] Starting recovery manager
02:44:15,703 INFO  [TransactionManagerService] Recovery manager started
02:44:15,703 INFO  [TransactionManagerService] Binding TransactionManager JNDI Reference
02:44:21,625 INFO  [EJB3Deployer] Starting java:comp multiplexer
02:44:25,281 INFO  [ServiceEndpointManager] jbossws-1.2.1.GA (build=200704151756)
02:44:28,531 INFO  [AprLifecycleListener] The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: F:\Program Files\Java\jdk1.6.0_13\bin
02:44:28,718 INFO  [Http11Protocol] Initializing Coyote HTTP/1.1 on http-127.0.0.1-8080
02:44:28,734 INFO  [AjpProtocol] Initializing Coyote AJP/1.3 on ajp-127.0.0.1-8009
02:44:28,734 INFO  [Catalina] Initialization processed in 846 ms
02:44:28,734 INFO  [StandardService] Starting service jboss.web
02:44:28,734 INFO  [StandardEngine] Starting Servlet Engine: JBossWeb/2.0.0.GA
02:44:28,843 INFO  [Catalina] Server startup in 104 ms
02:44:29,156 INFO  [TomcatDeployer] deploy, ctxPath=/, warUrl=.../deploy/jboss-web.deployer/ROOT.war/
02:44:30,578 INFO  [TomcatDeployer] deploy, ctxPath=/invoker, warUrl=.../deploy/http-invoker.sar/invoker.war/
02:44:31,187 INFO  [TomcatDeployer] deploy, ctxPath=/jbossws, warUrl=.../tmp/deploy/tmp7117955528796901291jbossws-context-exp.war/
02:44:31,484 INFO  [TomcatDeployer] deploy, ctxPath=/jbossmq-httpil, warUrl=.../deploy/jms/jbossmq-httpil.sar/jbossmq-httpil.war/
02:44:33,265 INFO  [TomcatDeployer] deploy, ctxPath=/web-console, warUrl=.../deploy/management/console-mgr.sar/web-console.war/
02:44:34,671 INFO  [MailService] Mail Service bound to java:/Mail
02:44:35,203 INFO  [RARDeployment] Required license terms exist, view META-INF/ra.xml in .../deploy/jboss-ha-local-jdbc.rar
02:44:35,312 INFO  [RARDeployment] Required license terms exist, view META-INF/ra.xml in .../deploy/jboss-ha-xa-jdbc.rar
02:44:35,437 INFO  [RARDeployment] Required license terms exist, view META-INF/ra.xml in .../deploy/jboss-local-jdbc.rar
02:44:35,593 INFO  [RARDeployment] Required license terms exist, view META-INF/ra.xml in .../deploy/jboss-xa-jdbc.rar
02:44:35,781 INFO  [RARDeployment] Required license terms exist, view META-INF/ra.xml in .../deploy/jms/jms-ra.rar
02:44:35,984 INFO  [RARDeployment] Required license terms exist, view META-INF/ra.xml in .../deploy/quartz-ra.rar
02:44:36,000 INFO  [QuartzResourceAdapter] start quartz!!!
02:44:36,156 INFO  [SimpleThreadPool] Job execution threads will use class loader of thread: main
02:44:36,218 INFO  [QuartzScheduler] Quartz Scheduler v.1.5.2 created.
02:44:36,218 INFO  [RAMJobStore] RAMJobStore initialized.
02:44:36,218 INFO  [StdSchedulerFactory] Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
02:44:36,218 INFO  [StdSchedulerFactory] Quartz scheduler version: 1.5.2
02:44:36,234 INFO  [QuartzScheduler] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
02:44:37,906 INFO  [ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=DataSourceBinding,name=DefaultDS' to JNDI name 'java:DefaultDS'
02:44:38,484 INFO  [A] Bound to JNDI name: queue/A
02:44:38,484 INFO  [B] Bound to JNDI name: queue/B
02:44:38,500 INFO  [C] Bound to JNDI name: queue/C
02:44:38,500 INFO  [D] Bound to JNDI name: queue/D
02:44:38,500 INFO  [ex] Bound to JNDI name: queue/ex
02:44:38,546 INFO  [testTopic] Bound to JNDI name: topic/testTopic
02:44:38,546 INFO  [securedTopic] Bound to JNDI name: topic/securedTopic
02:44:38,562 INFO  [testDurableTopic] Bound to JNDI name: topic/testDurableTopic
02:44:38,562 INFO  [testQueue] Bound to JNDI name: queue/testQueue
02:44:38,640 INFO  [UILServerILService] JBossMQ UIL service available at : /127.0.0.1:8093
02:44:38,734 INFO  [DLQ] Bound to JNDI name: queue/DLQ
02:44:39,093 INFO  [ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=ConnectionFactoryBinding,name=JmsXA' to JNDI name 'java:JmsXA'
02:44:39,671 INFO  [JmxKernelAbstraction] creating wrapper delegate for: org.jboss.ejb3.stateless.StatelessContainer
02:44:39,687 INFO  [JmxKernelAbstraction] installing MBean: jboss.j2ee:jar=EjbProject.jar,name=HelloWorldEjbBean,service=EJB3 with dependencies:
02:44:39,984 INFO  [EJBContainer] STARTED EJB: com.ejb.bean.HelloWorldEjbBean ejbName: HelloWorldEjbBean
02:44:40,093 INFO  [EJB3Deployer] Deployed: file:/C:/TDDOWNLOAD/jboss-4.2.0.GA/server/default/deploy/EjbProject.jar/
02:44:40,281 INFO  [TomcatDeployer] deploy, ctxPath=/jmx-console, warUrl=.../deploy/jmx-console.war/
02:44:40,718 INFO  [Http11Protocol] Starting Coyote HTTP/1.1 on http-127.0.0.1-8080
02:44:40,765 INFO  [AjpProtocol] Starting Coyote AJP/1.3 on ajp-1[b]27.0.0.1-8009
02:44:40,781 INFO  [Server] JBoss (MX MicroKernel) [4.2.0.GA (build: SVNTag=JBoss_4_2_0_GA date=200705111440)] Started in 32s:313ms

   2 Ejb3配置:
   你可以下载Jboss IDE for Eclipse或者MyEclipse即可。以下是其download的Web Sites。
   JbossIDE for Eclipse:http://www.jboss.org/jbosside/download/index.html;
对于MyEclipse,我下载的是8.5,下载地址为:http://downloads.myeclipseide.com/downloads/products/eworkbench/galileo/myeclipse-8.5.0-win32.exe
三 Exception:
  出现错误的Detail info 如下:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/net/SocketFactory
	at org.jnp.interfaces.NamingContextFactory.getInitialContext(NamingContextFactory.java:56)
	at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
	at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
	at javax.naming.InitialContext.init(InitialContext.java:223)
	at javax.naming.InitialContext.<init>(InitialContext.java:175)
	at com.ejbclient.ejb.HelloWorldEjbClient.main(HelloWorldEjbClient.java:16)
WARNING: Default charset GBK not supported, using ISO-8859-1 instead

这个错误,犯过这个错误的人不少,但没有一个贴出其中的错误原因:
我开始认为这个错误产生的Root cause:jboss的配置。经过几个小时的折腾,终于搞明白了,是少了jsse.jar文件,这个jar文件到Jre中imort jar即可。
四 Sample
  下面是我做的一个无状态的Session Bean Sample:
   1.Ejb Project:
    Create Ejb3工程,分为两步:
  (1)Create一个Ejb bean Interface:(HelloWorldEjb.java)
   代码如下:
package com.ejb.bean;

/**
 * @author Jamson Huang
 *
 */
public interface HelloWorldEjb {
    /*定义业务逻辑函数*/
	public String sayHelloWorld(String userName);
}

  这里需要注意的是:对于Ejb的业务处理,我们需要采取粗粒度的访问,也就是说,尽量少次数的通过Client去访问。
  (2)Create EjbBean类,实现Interface:(HelloWorldEjbBean.java)
package com.ejb.bean;

import javax.ejb.Remote;
import javax.ejb.Stateless;

/**
 * @author Jamson Huang
 *
 */
@Stateless
@Remote
public class HelloWorldEjbBean implements HelloWorldEjb {
	public String sayHelloWorld(String userName){
		return "Hello World! "+userName;
	}
}

通过Jboss deploy,并且打成jar包,这里产生的Jar包是:EjbProject.jar.
  2 Ejb Client:
   分成三步:
   1) Create 一个Project,然后将Jboss安装目录下的client文件夹里包含的所有Jar包都导入到项目。
   2) 将EjbProject.jar包导入项目中。
   3) Create Class:(HelloWorldEjbClient.java)
   代码如下:
   package com.ejbclient.ejb;

import java.util.Properties;

import javax.naming.InitialContext;

import com.ejb.bean.HelloWorldEjb;

/**
 * @author Jamson Huang
 * @version 1.0.0
 */
public class HelloWorldEjbClient {
	public static void main(String[] args)throws Exception{
/*Properties对象可以通过读取Jndi.properties文件产生,对于项目开发而言,应该是作为一个配置文件*/
		Properties props = new Properties();
		props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
		props.setProperty("java.naming.provider.url", "localhost:1099");
		props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
		
		InitialContext context = new InitialContext(props);
		
		HelloWorldEjb helloWorldEjb = (HelloWorldEjb)context.lookup("HelloWorldEjbBean/remote");
		String strName = helloWorldEjb.sayHelloWorld("Jamson");
		
		System.out.println(strName);
	}
}

jndi.properties:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming\:org.jnp.interfaces
java.naming.provider.url=localhost:1099


run main方法:
log4j:WARN No appenders could be found for logger (org.jboss.security.SecurityAssociation).
log4j:WARN Please initialize the log4j system properly.
Hello World! Jamson

下面会陆续介绍Ejb。
评论 共 1 条 请登录后发表评论
1 楼 craengjava 2012-04-22 20:07
thank you very much!

发表评论

您还没有登录,请您登录后再发表评论

文章信息

  • hwhuang在2010-05-09创建
  • hwhuang在2011-06-01更新
  • 标签: ejb3
Global site tag (gtag.js) - Google Analytics