本章内容是在学习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服务之间的相互调用引进的。