接口HelloWorld.java
- package jCuckoo;
- public interface HelloWorld {
- public String sayHello(String name);
- }
- package jCuckoo;
- public interface Other {
- public String sayMe();
- }
- package jCuckoo;
- public interface HelloWorldLocal extends HelloWorld{
- }
- package jCuckoo.impl;
- import javax.ejb.Local;
- import javax.ejb.Stateless;
- import jCuckoo.Other;
- @Stateless
- @Local(Other.class)//默认注解是Local
- public class OtherBean implements Other {
- public String sayMe() {
- return "other";
- }
- }
方法一:通过JNDI进行查找实现调用另外一个Bean
- @Stateless
- //如果没有下面remote和local注解,默认是local
- @Remote(HelloWorld.class)
- @Local(HelloWorldLocal.class)
- public class HelloWorldBean implements HelloWorld,HelloWorldLocal {
- public String sayHello(String name) {
- //通过JNDI进行查找
- try {
- InitialContext ctx = new InitialContext();
- Other other=(Other)ctx.lookup("OtherBean/local");
- return name+"说:你好,"+other.sayMe();
- } catch (NamingException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
- @Stateless
- //如果没有下面remote和local注解,默认是local
- @Remote(HelloWorld.class)
- @Local(HelloWorldLocal.class)
- public class HelloWorldBean implements HelloWorld,HelloWorldLocal {
- //@EJB Other other;//通过注入方式实现查找
- //如果注入时发现两个相同类型的other的时候,会报错.这时候需要指定对应的beanName,这样哪怕同时有多个类实现other接口,也不会出现问题
- @EJB (beanName="OtherBean")Other other;
- public String sayHello(String name) {
- return name+"说:你好,"+other.sayMe();
- }
- }
//TimerService是EJB3中已知服务,可以采用Resource来注解
@Resource TimerService timerService;
//指定数据源也通过Resource来注解,并通过参数mappedName指定对应JNDI名称
@Resource (mappedName="") DataSource dataSource;
发布,可以通过多种方式,这里采用ant发布:
- <?xml version="1.0" encoding="UTF-8"?>
- <project basedir="." name="EJBTest">
- <property name="src.dir" value="${basedir}/src"/>
- <property environment="env"/>
- <property name="jboss.home" value="${env.JBOSS_HOME}"/>
- <property name="jboss.server.config" value="default"/>
- <property name="build.dir" value="${basedir}/build"/>
- <path id="build.classpath">
- <fileset dir="${jboss.home}/client">
- <include name="*.jar"/>
- </fileset>
- <pathelement location="${build.dir}"/>
- </path>
- <target name="prepare">
- <delete dir="${build.dir}"/>
- <mkdir dir="${build.dir}"/>
- </target>
- <target name="complie" depends="prepare" description="编译">
- <javac srcdir="${src.dir}" destdir="${build.dir}">
- <classpath refid="build.classpath"/>
- </javac>
- </target>
- <target name="ejbjar" depends="complie" description="创建EJB发布包">
- <jar destfile="${basedir}/${ant.project.name}.jar">
- <fileset dir="${build.dir}">
- <include name="**/*.class"/>
- </fileset>
- </jar>
- </target>
- <target name="deploy" depends="ejbjar" description="发布ejb">
- <copy file="${basedir}/${ant.project.name}.jar" todir="${jboss.home}/server/${jboss.server.config}/deploy"></copy>
- </target>
- <target name="undeplay" description="卸载ejb">
- <delete file="${jboss.home}/server/${jboss.server.config}/deploy/${ant.project.name}.jar"></delete>
- </target>
- </project>
接下来构建客户端EJBClient.java
- package jCuckoo.client;
- import jCuckoo.HelloWorld;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- public class EJBClient {
- public static void main(String[] args) {
- try {
- InitialContext ctx=new InitialContext();
- HelloWorld helloworld=(HelloWorld)ctx.lookup("HelloWorldBean/remote");
- System.out.println(helloworld.sayHello("远程小麦"));
- } catch (NamingException e) {
- System.out.println(e.getMessage());
- }
- }
- }
- 远程小麦说:你好,other