欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

EJB 有/无状态会话bean的创建和部署运行

程序员文章站 2024-02-22 22:13:52
...
  1. 导入jboss-client中的所有jar包(jboss5,jboss7中只有一个jar包直接导入即可)
  2. 创建接口
    package inter;
    
    public interface hello {
    
    	public void sayHello();
    }
    
  3.  创建实现类
    package imple;
    
    import inter.hello;
    
    import javax.ejb.Remote;
    import javax.ejb.Stateless;
    @Stateless//无状态
    @Remote(value=hello.class)
    public class hellobea implements hello {
    
    	@Override
    	public void sayHello() {
    		// TODO Auto-generated method stub
    
    		System.out.println("hello");
    	}
    
    }
    
    package imple;
    
    import inter.hello;
    
    import javax.ejb.Remote;
    import javax.ejb.Stateful;
    
    @Stateful//有状态
    @Remote(value=hello.class)
    public class helloworld2 implements hello {
    
    	@Override
    	public void sayHello() {
    		// TODO Auto-generated method stub
    		System.out.println("hi");
    	}
    
    	
    }
    

     

  4.  

    添加build.xml文件

     

     

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project name="HelloWorld" default="ejbjar" basedir=".">
      
      <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="compile" depends="prepare" description="编绎">
    		<javac srcdir="${src.dir}" destdir="${build.dir}" debug="on" encoding="UTF-8">
    			
    			<classpath refid="build.classpath" />
    		</javac>
    	</target>
    
    	<target name="ejbjar" depends="compile" description="创建EJB发布包">
    		<jar jarfile="${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" />
    	</target>
    
    	
    	<target name="undeploy" description="卸载EJB">
    		<delete file="${jboss.home}\server\${jboss.server.config}\deploy\${ant.project.name}.jar" />
    	</target>
    
    </project>
    

     

  5. (myeclipse)点击项目,export(导出),导出文件类型为 jar,保存到 jboss/server/default/deploy (jboss5)目录下

  6. 通过build-path 或者复制刚刚的 jar 文件将其导入到一个新项目中

  7. 在新项目中编写测试类

    package client;
    
    import inter.hello;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    
    public class test {
    
    	/**
    	 * @param args
    	 * @throws NamingException 
    	 */
    	public static void main(String[] args) throws NamingException {
    		// TODO Auto-generated method stub
    
    		Context ctx = new InitialContext();
    		hello h = (hello)ctx.lookup("hellobea/remote");
    		h.sayHello();
    		hello h2 = (hello)ctx.lookup("helloworld2/remote");
    		h2.sayHello();
    	}
    
    }
    

     

  8. 启动 jboss 服务器然后执行程序就可以了

相关标签: JavaEE