Eclipse+MyEclipse+weblogic8開發EJB初探
一、無狀態會話BEAN
搞了幾年JAVA了,最近由于完全不懂EJB技術,被取笑了!我怒而學之.
以下為自己保留的代碼,大部分抄襲網上的,只為自己保留,望看客一笑而過!
關鍵字一:DOMAIN域
為WEBLOGIC管理應用的機制.用WEBLOGIC自帶的Configuration Wizard工具生成,根據功能不同能創建不同的工作域.
關鍵字二:部署
Eclipse+MyEclipse提供了比較強大的部署功能,這里忽略先.
具體代碼如下:
package com.ejb;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
/**
?* XDoclet-based session bean.? The class must be declared
?* public according to the EJB specification.
?*
?* To generate the EJB related files to this EJB:
?*??- Add Standard EJB module to XDoclet project properties
?*??- Customize XDoclet configuration for your appserver
?*??- Run XDoclet
?*
?* Below are the xdoclet-related tags needed for this EJB.
?*
?* @ejb.bean name="HelloWorld"
?*?????????? display-name="Name for HelloWorld"
?*?????????? description="Description for HelloWorld"
?*?????????? jndi-name="ejb/HelloWorld"
?*?????????? type="Stateless"
?*?????????? view-type="remote"
?*/
public class HelloWorld implements SessionBean {
?/** The session context */
?private SessionContext context;
?private String message = "helloworld!";
?public HelloWorld() {
??// TODO Auto-generated constructor stub
?}
?public void ejbActivate() throws EJBException, RemoteException {
??// TODO Auto-generated method stub
?}
?public void ejbPassivate() throws EJBException, RemoteException {
??// TODO Auto-generated method stub
?}
?public void ejbRemove() throws EJBException, RemoteException {
??// TODO Auto-generated method stub
?}
?/**
? * Set the associated session context. The container calls this method
? * after the instance creation.
? *
? * The enterprise bean instance should store the reference to the context
? * object in an instance variable.
? *
? * This method is called with no transaction context.
? *
? * @throws EJBException Thrown if method fails due to system-level error.
? */
?public void setSessionContext(SessionContext newContext)
??throws EJBException {
??context = newContext;
?}
/**
* 自己新創建的業務方法
**/
?public String getMessage()throws EJBException
?{
??return this.message;
??//return new String("helloworld!");
?}
///////////////////////////////////////////////////////////////////////////////////分割線///////////////////////////////////////////////////////////////////////////
/*
?* Generated by XDoclet - Do not edit!
?*/
package com.ejb;
/**
?* Session layer for HelloWorld.
?* @xdoclet-generated at ${TODAY}
?* @copyright The XDoclet Team
?* @author XDoclet
?* @version ${version}
?*/
public class HelloWorldSession
?? extends com.ejb.HelloWorld
?? implements javax.ejb.SessionBean
{
?? public void ejbActivate() throws javax.ejb.EJBException, java.rmi.RemoteException
?? {
????? super.ejbActivate();
?? }
?? public void ejbPassivate() throws javax.ejb.EJBException, java.rmi.RemoteException
?? {
????? super.ejbPassivate();
?? }
?? public void setSessionContext(javax.ejb.SessionContext ctx) throws javax.ejb.EJBException
?? {
????? super.setSessionContext(ctx);
?? }
?? public void unsetSessionContext()
?? {
?? }
?? public void ejbRemove() throws javax.ejb.EJBException, java.rmi.RemoteException
?? {
????? super.ejbRemove();
?? }
?? public void ejbCreate() throws javax.ejb.CreateException
?? {
?? }
}
//////////////////////////////////////////////////////////////////////////////分割線////////////////////////////////////////////////////////////////////////
/*
?* Generated by XDoclet - Do not edit!
?*/
package com.interfaces;
import javax.ejb.EJBException;
import java.rmi.*;
/**
?* Remote interface for HelloWorld.
?* @xdoclet-generated at ${TODAY}
?* @copyright The XDoclet Team
?* @author XDoclet
?* @version ${version}
?*/
public interface HelloWorld
?? extends javax.ejb.EJBObject
{
?? /**
??? * An example business method
??? * @throws EJBException Thrown if method fails due to system-level error.??? */
?? /*public void replaceWithRealBusinessMethod(? )
????? throws java.rmi.RemoteException;*/
?? public String getMessage()throws EJBException,RemoteException;
}
//////////////////////////////////////////////////////////////////////////////分割線/////////////////////////////////////////////////////////////////////////////
/*
?* Generated by XDoclet - Do not edit!
?*/
package com.interfaces;
/**
?* Home interface for HelloWorld.
?* @xdoclet-generated at ${TODAY}
?* @copyright The XDoclet Team
?* @author XDoclet
?* @version ${version}
?*/
public interface HelloWorldHome
?? extends javax.ejb.EJBHome
{
?? public static final String COMP_NAME="java:comp/env/ejb/HelloWorld";
?? public static final String JNDI_NAME="ejb/HelloWorld";
?? public com.interfaces.HelloWorld create()
????? throws javax.ejb.CreateException,java.rmi.RemoteException;
}
/////////////////////////////////////////////////////////////////////////////分割線/////////////////////////////////////////////////////////////////////////////
/*
?* Generated file - Do not edit!
?*/
package com.interfaces;
/**
?* Utility class for HelloWorld.
?* @xdoclet-generated at ${TODAY}
?* @copyright The XDoclet Team
?* @author XDoclet
?* @version ${version}
?*/
public class HelloWorldUtil
{
?? /** Cached remote home (EJBHome). Uses lazy loading to obtain its value (loaded by getHome() methods). */
?? private static com.interfaces.HelloWorldHome cachedRemoteHome = null;
?? private static Object lookupHome(java.util.Hashtable environment, String jndiName, Class narrowTo) throws javax.naming.NamingException {
????? // Obtain initial context
????? javax.naming.InitialContext initialContext = new javax.naming.InitialContext(environment);
????? try {
???????? Object objRef = initialContext.lookup(jndiName);
???????? // only narrow if necessary
???????? if (java.rmi.Remote.class.isAssignableFrom(narrowTo))
??????????? return javax.rmi.PortableRemoteObject.narrow(objRef, narrowTo);
???????? else
??????????? return objRef;
????? } finally {
???????? initialContext.close();
????? }
?? }
?? // Home interface lookup methods
?? /**
??? * Obtain remote home interface from default initial context
??? * @return Home interface for HelloWorld. Lookup using COMP_NAME
??? */
?? public static com.interfaces.HelloWorldHome getHome() throws javax.naming.NamingException
?? {
????? if (cachedRemoteHome == null) {
??????????? cachedRemoteHome = (com.interfaces.HelloWorldHome) lookupHome(null, com.interfaces.HelloWorldHome.COMP_NAME, com.interfaces.HelloWorldHome.class);
????? }
????? return cachedRemoteHome;
?? }
?? /**
??? * Obtain remote home interface from parameterised initial context
??? * @param environment Parameters to use for creating initial context
??? * @return Home interface for HelloWorld. Lookup using COMP_NAME
??? */
?? public static com.interfaces.HelloWorldHome getHome( java.util.Hashtable environment ) throws javax.naming.NamingException
?? {
?????? return (com.interfaces.HelloWorldHome) lookupHome(environment, com.interfaces.HelloWorldHome.COMP_NAME, com.interfaces.HelloWorldHome.class);
?? }
?? /** Cached per JVM server IP. */
?? private static String hexServerIP = null;
?? // initialise the secure random instance
?? private static final java.security.SecureRandom seeder = new java.security.SecureRandom();
?? /**
??? * A 32 byte GUID generator (Globally Unique ID). These artificial keys SHOULD <strong>NOT </strong> be seen by the user,
??? * not even touched by the DBA but with very rare exceptions, just manipulated by the database and the programs.
??? *
??? * Usage: Add an id field (type java.lang.String) to your EJB, and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
??? */
?? public static final String generateGUID(Object o) {
?????? StringBuffer tmpBuffer = new StringBuffer(16);
?????? if (hexServerIP == null) {
?????????? java.net.InetAddress localInetAddress = null;
?????????? try {
?????????????? // get the inet address
?????????????? localInetAddress = java.net.InetAddress.getLocalHost();
?????????? }
?????????? catch (java.net.UnknownHostException uhe) {
?????????????? System.err.println("HelloWorldUtil: Could not get the local IP address using InetAddress.getLocalHost()!");
?????????????? // todo: find better way to get around this...
?????????????? uhe.printStackTrace();
?????????????? return null;
?????????? }
?????????? byte serverIP[] = localInetAddress.getAddress();
?????????? hexServerIP = hexFormat(getInt(serverIP), 8);
?????? }
?????? String hashcode = hexFormat(System.identityHashCode(o), 8);
?????? tmpBuffer.append(hexServerIP);
?????? tmpBuffer.append(hashcode);
?????? long timeNow????? = System.currentTimeMillis();
?????? int timeLow?????? = (int)timeNow & 0xFFFFFFFF;
?????? int node????????? = seeder.nextInt();
?????? StringBuffer guid = new StringBuffer(32);
?????? guid.append(hexFormat(timeLow, 8));
?????? guid.append(tmpBuffer.toString());
?????? guid.append(hexFormat(node, 8));
?????? return guid.toString();
?? }
?? private static int getInt(byte bytes[]) {
?????? int i = 0;
?????? int j = 24;
?????? for (int k = 0; j >= 0; k++) {
?????????? int l = bytes[k] & 0xff;
?????????? i += l << j;
?????????? j -= 8;
?????? }
?????? return i;
?? }
?? private static String hexFormat(int i, int j) {
?????? String s = Integer.toHexString(i);
?????? return padHex(s, j) + s;
?? }
?? private static String padHex(String s, int i) {
?????? StringBuffer tmpBuffer = new StringBuffer();
?????? if (s.length() < i) {
?????????? for (int j = 0; j < i - s.length(); j++) {
?????????????? tmpBuffer.append('0');
?????????? }
?????? }
?????? return tmpBuffer.toString();
?? }
}
////////////////////////////////////////////////////////////////////////////////////////分割線///////////////////////////////////////////////////////////////////
配置文件如下:
ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "
<ejb-jar > ?? <description><![CDATA[No Description.]]></description> ?? <enterprise-beans> ????? <!-- Session Beans --> ???????? <ejb-name>HelloWorld</ejb-name> ???????? <home>com.interfaces.HelloWorldHome</home> ????? </session> ???? <!-- ????? <!-- Entity Beans --> ????? <!-- Message Driven Beans --> ?? </enterprise-beans> ?? <!-- Relationships --> ?? <!-- Assembly Descriptor --> ?? <assembly-descriptor > ?? <!-- method permissions --> ?? <!-- transactions --> ?? <!-- finder transactions --> ?? <!-- message destinations --> ?? <!-- exclude list -->
?? <display-name>Generated by XDoclet</display-name>
????? <session >
???????? <description><![CDATA[Description for HelloWorld]]></description>
???????? <display-name>Name for HelloWorld</display-name>
???????? <remote>com.interfaces.HelloWorld</remote>
???????? <ejb-class>com.ejb.HelloWorldSession</ejb-class>
???????? <session-type>Stateless</session-type>
???????? <transaction-type>Container</transaction-type>
?????? To add session beans that you have deployment descriptor info for, add
?????? a file to your XDoclet merge directory called session-beans.xml that contains
?????? the <session></session> markup for those beans.
???? -->
???? <!--
?????? To add entity beans that you have deployment descriptor info for, add
?????? a file to your XDoclet merge directory called entity-beans.xml that contains
?????? the <entity></entity> markup for those beans.
???? -->
???? <!--
?????? To add message driven beans that you have deployment descriptor info for, add
?????? a file to your XDoclet merge directory called message-driven-beans.xml that contains
?????? the <message-driven></message-driven> markup for those beans.
???? -->
???? <!--
?????? To specify your own assembly descriptor info here, add a file to your
?????? XDoclet merge directory called assembly-descriptor.xml that contains
?????? the <assembly-descriptor></assembly-descriptor> markup.
???? -->
???? <!--
?????? To specify additional security-role elements, add a file in the merge
?????? directory called ejb-security-roles.xml that contains them.
???? -->
???? <!--
?????? To specify additional method-permission elements, add a file in the merge
?????? directory called ejb-method-permissions.ent that contains them.
???? -->
???? <!--
?????? To specify additional container-transaction elements, add a file in the merge
?????? directory called ejb-container-transactions.ent that contains them.
???? -->
???? <!--
?????? To specify additional message-destination elements, add a file in the merge
?????? directory called ejb-message-destinations.ent that contains them.
???? -->
???? <!--
?????? To specify an exclude-list element, add a file in the merge directory
?????? called ejb-exclude-list.xml that contains it.
???? -->
?? </assembly-descriptor>
</ejb-jar>
//////////////////////////////////////////////////////////////////////////////分割線///////////////////////////////////////////////////////////////////////////
weblogic-ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>