当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

初识EJB2.1

发表于: 2013-10-15   作者:cutesunshineriver   来源:转载   浏览:
ejb
摘要: 企业Bean分为实体Bean、会话Bean和消息驱动Bean,其中会话Bean又分为有状态会话Bean和无状态会话Bean。 Enterprise Java Bean = EJB |--Entity Bean |--Session Bean |   |--Stateful Session Bean = SFSB |   |--Stateless S
企业Bean分为实体Bean、会话Bean和消息驱动Bean,其中会话Bean又分为有状态会话Bean和无状态会话Bean。
Enterprise Java Bean = EJB
|--Entity Bean
|--Session Bean
|   |--Stateful Session Bean = SFSB
|   |--Stateless Session Bean = SLSB
|--Message Driven Bean = MDB

Remote Method Invocation = RMI

Container Managed Persistent = CMP
Container Managed Relationships = CMR
Bean Managed Persistent = BMP

Container Managed Transaction = CMT
Bean Managed Transaction = BMT
EJB不支持嵌套事务。

Business Interface[EJBObject]
Home Interface[EJBHome]
Bean Class[EnterpriseBean]
Client通过JNDI找到Server的Stub,然后找到Home Interface,用Home Interface生成一个Business Interface,Business Interface代理了Bean Class。
Business Interface中的方法和Bean Class中的业务方法存在一一对应的关系,需要手工或者IDE来确保这一点。

接下来是从《Head First EJB》中的第一章EJB入门里实现的例子。
首先是Bean Class,
package com.bms;

import java.rmi.RemoteException;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

public class AdviceBean implements SessionBean {
	
	private static final long serialVersionUID = 1L;
	
	public String advice() {
		return "this is for EJB 2.1";
	}

	@Override
	public void ejbActivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	@Override
	public void ejbPassivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	@Override
	public void ejbRemove() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setSessionContext(SessionContext arg0) throws EJBException,
			RemoteException {
		// TODO Auto-generated method stub

	}

}


接着是Business Interface,
package com.bms;

import java.rmi.RemoteException;

import javax.ejb.EJBObject;

public interface Advice extends EJBObject {
	
	public String advice() throws RemoteException;

}


最后是Home Interface,
package com.bms;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface AdviceHome extends EJBHome {
	
	public Advice create() throws CreateException, RemoteException;

}


ejb-jar.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" version="3.0">
  <display-name>bpm-ejb </display-name>
  <!-- 
  <ejb-client-jar>bpm-ejbClient.jar</ejb-client-jar>
   -->
  <enterprise-beans>
  	<session>
  		<display-name>AdviceBean</display-name>
  		<ejb-name>AdviceBean</ejb-name>
  		<home>com.bms.AdviceHome</home>
  		<remote>com.bms.Advice</remote>
  		<ejb-class>com.bms.AdviceBean</ejb-class>
  		<session-type>Stateless</session-type>
  		<transaction-type>Bean</transaction-type>
  		<security-identity>
  			<description></description>
  			<use-caller-identity/>
  		</security-identity>
  	</session>
  </enterprise-beans>
</ejb-jar>


最后部署在weblogic server上,增加一个JNDI对象。
文件weblogic-ejb-jar.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-ejb-jar xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-ejb-jar" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd http://xmlns.oracle.com/weblogic/weblogic-ejb-jar http://xmlns.oracle.com/weblogic/weblogic-ejb-jar/1.2/weblogic-ejb-jar.xsd">
    <!--weblogic-version:10.3.6-->
    <wls:weblogic-enterprise-bean>
        <wls:ejb-name>TaskBean</wls:ejb-name>
        <wls:stateless-session-descriptor></wls:stateless-session-descriptor>
        <wls:jndi-name>com/bms/TaskBeanRemote</wls:jndi-name>
        <wls:local-jndi-name>com/bms/TaskBeanLocal</wls:local-jndi-name>
    </wls:weblogic-enterprise-bean>
    <wls:weblogic-enterprise-bean>
        <wls:ejb-name>QueryBean</wls:ejb-name>
        <wls:stateless-session-descriptor></wls:stateless-session-descriptor>
        <wls:jndi-name>QueryBean</wls:jndi-name>
    </wls:weblogic-enterprise-bean>
    <wls:weblogic-enterprise-bean>
        <wls:ejb-name>AdviceBean</wls:ejb-name>
        <wls:stateless-session-descriptor></wls:stateless-session-descriptor>
        <wls:jndi-name>AdviceBean</wls:jndi-name>
    </wls:weblogic-enterprise-bean>
</wls:weblogic-ejb-jar>


这3个Java文件和两个部署描述的XML准备好了,就可以把整个EJB项目部署到weblogic上了。

客户端的调用代码如下:
package com.bms;

import java.rmi.RemoteException;
import java.util.Properties;

import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

public class Ejb2InvocationTest {

	public static void main(String[] args) {
		Properties p = new Properties();
		p.put(Context.INITIAL_CONTEXT_FACTORY,
				"weblogic.jndi.WLInitialContextFactory");
		p.put(Context.PROVIDER_URL, "t3://localhost:7001");
		p.put(Context.SECURITY_PRINCIPAL, "weblogic");
		p.put(Context.SECURITY_CREDENTIALS, "welcome1");

		InitialContext ctx;
		try {
			ctx = new InitialContext(p);
			Object stub = ctx.lookup("AdviceBean");
			AdviceHome home = (AdviceHome) PortableRemoteObject.narrow(stub,
					AdviceHome.class);
			Advice bean = home.create();
			System.out.println(bean.advice());

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (CreateException e) {
			e.printStackTrace();
		}

	}

}


控制台打印出“this is for EJB 2.1”。

Done!!!!!!!!!!!!!

初识EJB2.1

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号