论坛首页 Java企业应用论坛

Eclipse4.2+Jboss7/Jboss-eap-6.0分布式开发 第三篇

浏览 3549 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-05-09  

第三篇(EJB Client端的开发)

  1. 建立一个Dynamic Web Project,项目名称为EJBClient。项目结构为:
  2. 还记得上一篇中给Server端的项目添加Maven支持了吗,这里就用到了。右键那个项目->run as->maven build...,run 完之后你就可以在项目根目录的target下面找到此项目打成的jar包了。 E:\project\J2EE\MyEJBServer\target\MyEJBServer-0.0.1-SNAPSHOT.jar
    [INFO] Scanning for projects...
    [INFO] ------------------------------------------------------------------------
    [INFO] Building MyEJBServer
    [INFO]    task-segment: [package]
    [INFO] ------------------------------------------------------------------------
    [INFO] [resources:resources {execution: default-resources}]
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 1 resource
    [INFO] [compiler:compile {execution: default-compile}]
    [INFO] Nothing to compile - all classes are up to date
    [INFO] [resources:testResources {execution: default-testResources}]
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory E:\project\J2EE\MyEJBServer\src\test\resources
    [INFO] [compiler:testCompile {execution: default-testCompile}]
    [INFO] Not compiling test sources
    [INFO] [surefire:test {execution: default-test}]
    [INFO] Tests are skipped.
    [INFO] [ejb:ejb {execution: default-ejb}]
    [INFO] Building EJB MyEJBServer-0.0.1-SNAPSHOT with EJB version 3.1
    [INFO] Building jar: E:\project\J2EE\MyEJBServer\target\MyEJBServer-0.0.1-SNAPSHOT.jar
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESSFUL
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 3 seconds
    [INFO] Finished at: Thu May 09 11:19:37 CST 2013
    [INFO] Final Memory: 11M/28M
    [INFO] ------------------------------------------------------------------------
    
  3. 把这个jar包引入到此项目的build path中
  4. 开发EJBUtil用来获取EJB
    package com.synnex.ejb.util;
    
    import java.util.Properties;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    
    public class EjbUtil {
    	private static Context initialContext;
    	
    	private static final String PKG_INTERFACES = "org.jboss.ejb.client.naming";
    	
    	private static Context getInitialContext() throws NamingException{
    		if(initialContext == null){
    			Properties prop = new Properties();
    			prop.put(Context.URL_PKG_PREFIXES, PKG_INTERFACES);
    			
    			initialContext = new InitialContext(prop);
    		}
    		
    		return initialContext;
    	}
    	
    	public static Object lookup(String jndi) throws Exception{
    		return getInitialContext().lookup(jndi);
    	}
    	
    	private static Object lookup(Class<?> serviceClass, String moduleName, String beanName) throws Exception {
            final String appName = "";			//ear包的名字,如果没有ear包,那么这个就是空字符串就行
            final String distinctName = "";
            
            final String interfaceName = serviceClass.getName();
            
            String jndi = "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + interfaceName;
            
            System.out.println("JNDI: " + jndi);
    
    		return getInitialContext().lookup(jndi);
        }
    	
    	public static Object lookupRemoteBizService(Class<?> serviceClass, String moduleName) throws Exception{
    		String beanName = serviceClass.getSimpleName().replace("Remote", "");
    		return lookup(serviceClass, moduleName, beanName);
    	}
    }
    
     
  5. 开发Servlet用来作为程序的入口点
    package com.synnex.servlet;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.hua.ejb.LoginBeanRemote;
    import com.synnex.ejb.util.EjbUtil;
    
    /**
     * Servlet implementation class TestRemoteEJB
     */
    @WebServlet("/TestRemoteEJB")
    public class TestRemoteEJB extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public TestRemoteEJB() {
            super();
            // TODO Auto-generated constructor stub
        }
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		this.doPost(request, response);
    	}
    
    	/**
    	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		try {
    			LoginBeanRemote loginBean = (LoginBeanRemote)EjbUtil.lookupRemoteBizService(LoginBeanRemote.class, "MyEJBServer");
    			System.out.println(loginBean.login("ejb", "123456"));
    			System.out.println(loginBean.login("ejb", "qwerty"));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    }
    
     
  6. 拷贝EJB server端程序的jar包到Client 的Jboss Server的modules下面。目录位置D:\server\jboss-eap-6.0\modules\com\huag\ejb\main,然后添加module.xml。

     
    <module xmlns="urn:jboss:module:1.1" name="com.huag.ejb">
        <resources>
            <resource-root path="MyEJBServer-0.0.1-SNAPSHOT.jar"/>
        </resources>
        <dependencies>
        </dependencies>
    </module>
  7. 为项目部署到jboss7下面,添加jboss-deployment-structure.xml,到WEB-INF下面。
    <jboss-deployment-structure>
       <deployment>
          <dependencies>
          	<module name="com.huag.ejb" />
          </dependencies>
       </deployment>
    </jboss-deployment-structure>
       
  8. 发布项目到Jboos,然后启动JBoss,看是否能够启动成功。

    下一篇为最终篇,我们将讲解Client Server的配置和展示分布式调用。

  • 大小: 24.8 KB
  • 大小: 76.9 KB
  • 大小: 73 KB
  • 大小: 36.4 KB
  • 大小: 22.4 KB
  • 大小: 21.5 KB
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics