`

ejb简单入门细节随笔

阅读更多

本章内容是在学习ejb过程中记录的入门的知识点,主要讲述了几个标签的使用以及一些细节问题

接口PersonRemote.java

 

package org.guo.ejb.remote;

public interface PersonRemote {
	
	public void hello(String userName);
	
	public void run(int age);
	
	public void methodForward();
}

 

接口CheckRemote.java

package org.guo.ejb.remote;

public interface CheckRemote {
	public String doCheck();
}

 

实现类PersonBean.java

package org.guo.ejb.bean;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptors;
import javax.interceptor.InvocationContext;

import org.guo.ejb.remote.CheckRemote;
import org.guo.ejb.remote.PersonRemote;
import org.jboss.ejb3.annotation.Clustered;

@Remote(PersonRemote.class)
@Stateless(name="person")
@Clustered
@Interceptors({Interceptor1.class,Interceptor2.class})
public class PersonBean implements PersonRemote {
	
	
	@EJB(beanName="check")
	private CheckRemote check;
	
	public void hello(String userName) {
		System.out.println(userName + "你好");
	}
	
	public void run(int age) {
		System.out.println("我的年龄是" + age);
	}
	
	public void methodForward() {
		System.out.println("测试获取EJB对象 方法调用结果为 --" + check.doCheck());
	}
	
	@AroundInvoke
	public Object mySelfInterceptor(InvocationContext ctx) throws Exception{
		System.out.println("这是我业务类中自己定义的拦截器...");
		return ctx.proceed();
	}
}

 

实现类CheckBean.java

package org.guo.ejb.bean;

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

import org.guo.ejb.remote.CheckRemote;

@Remote(CheckRemote.class)
@Stateless(name="check")
public class CheckBean implements CheckRemote {

	public String doCheck() {
		String msg = "执行必要检查....";
		return msg;
	}

}

 

   客户端测试程序

package org.guo.ejb;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;

import org.guo.ejb.remote.PersonRemote;


public class TestEJB {
	public static void main(String[] args) throws Exception{
		String providerUrl = "10.10.62.133:1099,10.10.62.193:1099";
		String factoryInit =  "org.jnp.interfaces.NamingContextFactory";
		Hashtable<String, String> env = new Hashtable<String, String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, factoryInit);
		env.put(Context.PROVIDER_URL, providerUrl);
		Context ctx = new InitialContext(env);
		PersonRemote personRemote = (PersonRemote)ctx.lookup("person/remote");
		
		personRemote.run(23);
		personRemote.methodForward();
		
	}

}

 

 需要注意的问题:
(1)在实现类上写Remote标签时,如果标签括号内不写xxx.class 则在调用ejb lookup强转后会有类型转换错误。
(2)在jboss下如果ejb应用打包为ear包则发布的路径为 <应用>/<公布的接口名>/remote,如果是jar的方式直接部署则为<公布的接口名>/remote 或者 <公布的接口名>
(3)不同的服务器发布的jndi名称是不相同的,jboss是(2)这种写法  而weblogic则是 xxxx#xxx 类似于这种
(4)jboss下 Stateless注解括号内如果是写name="hello" 则绑定的jndi名称为 hello/remote 如果是mappedName="hello" jndi名称为 "hello" 当前这两个属性和jndi名称的关系没
搞太明白,不同的容器实现也不同,在EJB In Action 一书 3.2.2 Using the @Stateless Annotation 中有段说明可以参考一下
:::
As we know, the @Stateless annotation marks the  BidManagerBean  POJO as a Stateless 
Session Bean. Believe it or not, other than marking a POJO for the purposes of making the container 
aware of its purpose, the annotation really does not do very much else. The specification of the  
 
@Stateless annotation is as follows: 
@Target(TYPE) @Retention(RUNTIME) 
public @interface Stateless { 
    String name() default ""; 
       String mappedName() default ""; 
   String description() default ""; 
 
} 
 
The single parameter,  name, specifies the name of the Bean. Some containers use this parameter 
to bind the EJB to the global JNDI tree. Recall that JNDI is essentially the application server’s 
managed resource registry. All EJBs automatically get bound to JNDI as soon as they catch the 
container’s watchful eye. You will see real use of the  name parameter in Chapter 11 when we discuss 
deployment descriptors. In Listing 3.1, the Bean name is specified to be ‘BidManager’. As the 
annotation definition shows, the  name parameter is optional since it is defaulted to an empty 
String. We could easily omit it as follows: 
@Stateless 
public class BidManagerBean implements BidManager { 
If the  name parameter is omitted, the container assumes that the bean name should be set to the 
name of the Class. In this case, the Bean name would be assumed to be ‘BidManagerBean’. The 
mappedName  is a vendor specific name that you can assign to your EJB, some containers such as 
Glassfish application server uses this name to assign the global JNDI name for the EJB. As we noted, 
the  BidManagerBean implements a business interface named BidManager . Although we’ve 
touched on the idea of a business interface, we haven’t really dug very deep into the concept. This is a 
great time to do exactly that.

:::

(5)jndi调用时url别写顺手写成http
(6)关于EJB的集群@Clustered、拦截器@Interceptors的使用,请参见本博客其他文章
(7)@EJB标签的作用就是为了多个EJB服务之间的相互调用引进的。
分享到:
评论

相关推荐

    EJB3.0入门经典(PDF)

    《EJB3.0入门经典》是关于EJB 3.0的专业技术教程,从实用的角度出发,理论联系实际,用9章的篇幅详细讲解了EJB 3.0开发的方法和技巧。《EJB3.0入门经典》内容丰富,讲解由浅入深,全面系统,在讲解EJB 3.0最新开发...

    ejb3.0入门图文教程

    ejb3.0入门教程。 详解! 本教程详细的讲述了ejb3.0入门的配制、开发环境、以及基本操作等!

    EJB3.0入门图文教程

    EJB3.0入门图文教程电子书,Jbuilder 2007,新版JBuilder2007基于Eclipse平台,与以往的JBuilder版本完全不 同.借助于Eclipse的开放平台以及丰富的插件,使得JBulider对当前的流行的框架支持非 常好,同时支持EJB3.0的...

    EJB3入门例子

    EJB3入门例子,jboss5+EJB3+myEclipse

    ejb3.0入门经典.rar

    ejb3.0入门经典.rarejb3.0入门经典.rarejb3.0入门经典.rarejb3.0入门经典.rarejb3.0入门经典.rarejb3.0入门经典.rarejb3.0入门经典.rar

    《EJB 3.0入门经典》 源码

    黎活明出版的EJB 3.0入门经典所要的源码,完全可以用,大家可以下载

    EJB3.0入门教程 PDF

    EJB3.0入门教程,PDF高清版,主要讲了在JBUILDER中配置EJB3.0和在JBOSS中配置EJB3.0的相关知识,同时也介绍了一些入门级要点,适合初学者阅读。

    一个EJB项目入门例程

    这是大学选修课javaEE的一个基于EJB的作业,可以作为EJB的入门参考资料

    EJB 3.0 入门经典源码part1

    EJB 3.0 入门经典源代码part1 共5卷

    EJB3.0入门经典_完整版02

    EJB3.0入门经典完整版,于网上搜集,分享与大家。文件较大,分3部分上传。

    EJB3.0入门经典_完整版03

    EJB3.0入门经典完整版,于网上搜集,分享与大家。文件较大,分3部分上传。

    ejb3.0入门经典教程及源码

    本光盘是配书CD-ROM光盘,其中包括黎活明的入门教程、sourcecode、“软件”两个文件夹、ejb-3_0-fr-spec-ejbcore.pdf、 ejb-3_0-fr-spec-persistence.pdf、ejb-3_0-fr-spec-simplified.pdf。两个演示文档以及mysql-...

    EJB 3.0入门经典 源码

    本书是关于EJB 3.0的专业技术教程,从实用的角度出发,理论联系实际,用9章的篇幅详细讲解了EJB 3.0开发的方法和技巧。  本书内容丰富,讲解由浅入深,全面系统,在讲解EJB 3.0最新开发技术的同时,精心设计了与...

    《EJB3.0入门经典教程精简版》源码

    《EJB3.0入门经典教程精简版》源码

    EJB的入门教材.htm

    EJB的入门教材.htm

    EJB3.0入门经典源代码.part1

    本书是关于EJB 3.0的专业技术教程,从实用的角度出发,理论联系实际,用9章的篇幅详细讲解了EJB 3.0开发的方法和技巧。  本书内容丰富,讲解由浅入深,全面系统,在讲解EJB 3.0最新开发技术的同时,精心设计了与...

    EJB3 入门经典 源码

    EJB3 入门经典 源码 .

    EJB的入门教材.pdf

    EJB的入门教材.pdf

Global site tag (gtag.js) - Google Analytics