`

EJB学习之三---Local和Remote接口

    博客分类:
  • J2EE
阅读更多
这篇文章主要来学习Ejb的组成元素,重点介绍Local和Remote接口。

Key
EJB的组成
Local接口
Remote接口
Ejb Instance及工作方式
Sample实例

一 Ejb的组成
   标准的Ejb至少由三个部分组成:Local接口,Remote接口以及Ejb Instance。
二 Local接口
   1. Local接口,称为Home接口,继.承接基类javax.ejb.Local.从Ejb2.0开始出现的 新的接口,本质上是Java RMI接口.
  
   2. 对于Local接口的使用,只能在本地使用。它列出了所有定位/创建/删除Ejb实例的方法。
    3. 定义local接口的方法:
     1) 在Ejb文件中定义:
        @Local(value={Interface1, interface2,..})
     2) 在Ejb文件中定义:
        @Local(Interface1,Interface2,..)
     3) 在Interface文件中定义
         @Local
三 Remote接口
   1. Remote接口列出Ejb类的业务逻辑方法。集成基类javax.ejb.Remote.本质上是Java RMI接口.
   2. 定义local接口的方法:
     1) 在Ejb文件中定义:
        @Remote(value={Interface1, interface2,..})
     2) 在Ejb文件中定义:
        @Remote(Interface1,Interface2,..)
     3) 在Interface文件中定义
         @Remote
四 Ejb instance及工作方式:
   对于Ejb的工作方式,如附件中的图:
  

五 Sample
  1.Interface 定义:
     1)UserManage
public interface UserManage {
  public void addUser(User user);
}

     2)UserIdManage
public interface UserIdManage {
	public void addUserId(User user);
}
  
     3)UserNameManage
  public interface UserNameManage {
	public void addUserName(User user);
}


  2.Ejb :
    1)

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

/**
 * @author Jamson Huang
 *
 */
@Stateless(name="UserManageEjbBean")/*rename ejb name*/
@Remote(value={UserManage.class,UserNameManage.class})/*定义Remote接口*/
@Local(value={UserManage.class,UserIdManage.class})/*定义Local接口*/
public class UserManageBean implements UserManage,UserNameManage,UserIdManage {

	public void addUser(User user) {
		System.out.println(user.getUserName());
		user.setUserId(10);
	}
	
	public void addUserName(User user){
		System.out.println(user.getUserName());
		user.setUserName("EJB");
	}
	public void addUserId(User user){
		System.out.print(user.getUserId());
		user.setUserId(100);
	}
}
  
deploy到Jboss服务器上,其Ejb name为:UserManageEjbBean.
3)JavaBean:
   
import java.io.Serializable;

/**
 * @author Jamson Huang
 *
 */
public class User implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1089875714097918835L;
	
	private String userName;
	private int userId;
	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}
	/**
	 * @param userName the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}
	/**
	 * @return the userId
	 */
	public int getUserId() {
		return userId;
	}
	/**
	 * @param userId the userId to set
	 */
	public void setUserId(int userId) {
		this.userId = userId;
	}
}

4) EjbClient:
   (1) Main Class:
    import javax.naming.InitialContext;

import com.ejb.bean.user.User;
import com.ejb.bean.user.UserManage;
import com.ejb.bean.user.UserNameManage;

/**
 * @author Jamson Huang
 *
 */
public class UserManageEjbClient {
 
	public static void main(String[] args) throws Exception {
	    InitialContext context = new InitialContext();
	    UserManage userManage = (UserManage)context.lookup("UserManageEjbBean/remote");
	    
	    User user = new User();
	    user.setUserName("Jamson");
	    
	    userManage.addUser(user);
	    System.out.println("UserId:"+user.getUserId());
	    
	    UserNameManage userNameManage = (UserNameManage)context.lookup("UserManageEjbBean/remote");
	    
	    userNameManage.addUserName(user);
	    
	    System.out.println("UserName:" + user.getUserName());
	}
}

run Main,Console出现的Log:
  UserId:0
UserName:Jamson
  (2)web 页面:
   <%@ page language="java" import="java.util.*,javax.naming.*,com.ejb.bean.user.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Ejb Interface test page</title>
  </head>
  
  <body>
   <%
   	 Properties props = new Properties();
   	 props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
	 props.setProperty("java.naming.provider.url", "localhost:1099");
	 props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
  
     InitialContext context = new InitialContext(props);
     UserManage userManage = (UserManage)context.lookup("UserManageEjbBean/local");
     User user = new User();
     user.setUserName("Jamson");
     
     userManage.addUser(user);
     out.println("UserID:"+user.getUserId());
     
     UserIdManage userIdManage = (UserIdManage)context.lookup("UserManageEjbBean/local");
     
     userIdManage.addUserId(user);
     out.println("UserId:"+user.getUserId());
    %>
  </body>
</html>

deploy web project,输入:http://localhost:8080/EjbWebProject_Client/index.jsp
显示:
UserID:10
UserId:100
  • 大小: 34.5 KB
3
0
分享到:
评论
1 楼 liu765023051 2013-12-26  
亲,local与remote有什么区别呢

相关推荐

    Local和Remote方式访问EJB

    NULL 博文链接:https://676744379-qq-com.iteye.com/blog/1853336

    EJB3.0 实例教程 -- 切片1

    4.1.3 开发存在Remote与Local接口的无状态Session Bean ...17 4.2 STATEFUL SESSION BEANS(有状态BEAN)开发20 4.3 STATELESS SESSION BEAN与STATEFUL SESSION BEAN的区别...22 4.4 如何改变SESSION BEAN的JNDI ...

    EJB3.0 实例教程 -- 切片2

    4.1.3 开发存在Remote与Local接口的无状态Session Bean ...17 4.2 STATEFUL SESSION BEANS(有状态BEAN)开发20 4.3 STATELESS SESSION BEAN与STATEFUL SESSION BEAN的区别...22 4.4 如何改变SESSION BEAN的JNDI ...

    Tomcat web工程 调用 JBOSS EJB local及remote源码

    Tomcat下web工程调用JBOSS部署的EJB项目 此源码包 包括local本地调用及remote远程调用 文档说明在http://blog.csdn.net/heardy/article/details/6906225

    EJB 3.0学习之实体Bean

    在EJB3.0中开发实体Bean非常简单,你可以象开发一般的java bean一样编程,只需做少量的注释。一个实体bean不需要实现Home接口或者Remote、Local接口。

    javax.ejb.jar下载

    javax.ejb.Local.class javax.ejb.LocalBean.class javax.ejb.LocalHome.class javax.ejb.Lock.class javax.ejb.LockType.class javax.ejb.MessageDriven.class javax.ejb.MessageDrivenBean.class javax.ejb....

    javax.ejb.rar

    javax.ejb.Local.class javax.ejb.LocalBean.class javax.ejb.LocalHome.class javax.ejb.Lock.class javax.ejb.LockType.class javax.ejb.MessageDriven.class javax.ejb.MessageDrivenBean.class javax.ejb....

    javax.ejb.jar

    javax.ejb.Local.class javax.ejb.LocalBean.class javax.ejb.LocalHome.class javax.ejb.Lock.class javax.ejb.LockType.class javax.ejb.MessageDriven.class javax.ejb.MessageDrivenBean.class javax.ejb....

    eclipse + JBoss 5 + EJB3开发指南

    [7] 建立 HelloWorld Stateless Session Bean 及 Local, Remote interfaces: 19 [8] 建立客户端测试程序: 20 [9] 使用 ANT 建立 EJB-JAR 并执行 Client 程序: 20 MyEclipse EJB 3.0 Tutorial 22 Table of ...

    EJB葵花宝典(题集)

    3.4.2Remote接口 20 3.4.3Local接口 21 3.4.4Bean类 22 3.4.5远程客户 23 3.4.6本地客户 24 3.5Stateful SessionBean(有状态会话Bean) 25 3.6State(状态) 26 3.6.1无状态 26 3.6.2有状态 27 3.6.3效果 27 4消息...

    JEEProjectStructure:具有推荐结构的 Java EE 项目

    JEE项目结构 具有推荐结构的 Java EE 项目 ... sample-ejb-service:具有@Local 和@Remote 接口的 EJB 服务,以及一个嵌入的本地客户端 sample-ejb-client:调用sample-ejb-service的@Remote接口的远程客户端

    J2EE程序设计复习题.doc

    J2EE程序设计复习题 1. 选择题 1. EJB是: [B] A. 服务器端产品 B.... Remote interface D. Primary key class E. Local home interface F. Local interface G. ejb-jar.xml H. application.xml I. Bean class 5.

    EJB3.0实例教程

    4.1.3 开发存在Remote与Local接口的无状态Session Bean ...............................................................................17 4.2 STATEFUL SESSION BEANS(有状态BEAN)开发.........................

    J2EE中文版指南 CHM格式 带全文检索

    Local接口和CMR(Container-Managed RelationShips) 49 两种访问方式的抉择 49 性能和访问方式 50 方法参数和访问方式 50 数据访问粒度 50 6,企业Bean的“内容” 51 7,企业Bean的命名约定 51 8,企业Bean的生存...

    超级有影响力霸气的Java面试题大全文档

     Collection是集合类的上级接口,继承与他的接口主要有Set 和List. Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。 13、&和&&的区别。 &是位运算符...

    java 面试题 总结

     Collection是集合类的上级接口,继承与他的接口主要有Set 和List. Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。 10、&和&&的区别。 &是位运算符...

    Java Web Services

    Java Web Services shows you how to use SOAP to perform remote method calls and message passing;how to use WSDL to describe the interface to a web service or understand the interface of someone else's ...

    java web services

    Java Web Services shows you how to use SOAP to perform remote method calls and message passing; how to use WSDL to describe the interface to a web service or understand the interface of someone else's...

    Spring-Reference_zh_CN(Spring中文参考手册)

    13.3.1. AbstractController 和 WebContentGenerator 13.3.2. 其它的简单控制器 13.3.3. MultiActionController 13.3.4. 命令控制器 13.4. 处理器映射(handler mapping) 13.4.1. BeanNameUrlHandlerMapping 13.4.2...

Global site tag (gtag.js) - Google Analytics