| package org.jboss.docs.interest; import javax.ejb.EJBObject; import java.rmi.RemoteException; /** This interface defines the `Remote' interface for the `Interest' EJB. Its single method is the only method exposed to the outside world. The class InterestBean implements the method. */ public interface Interest extends EJBObject { /** Calulates the compound interest on the sum `principle', with interest rate per period `rate' over `periods' time periods. This method also prints a message to standard output; this is picked up by the EJB server and logged. In this way we can demonstrate that the method is actually being executed on the server, rather than the client. */ public double calculateCompoundInterest(double principle, double rate, double periods) throws RemoteException; } |
| package org.jboss.docs.interest; import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; /** This interface defines the 'home' interface for the 'Interest' EJB. */ public interface InterestHome extends EJBHome { /** Creates an instance of the `InterestBean' class on the server, and returns a remote reference to an Interest interface on the client. */ Interest create() throws RemoteException, CreateException; } |
| package org.jboss.docs.interest; import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; /** This class contains the implementation for the 'calculateCompoundInterest' method exposed by this Bean. It includes empty method bodies for the methods prescribe by the SessionBean interface; these don't need to do anything in this simple example. */public class InterestBean implements SessionBean { /** Calulates the compound interest on the sum `principle', with interest rate per period `rate' over `periods' time periods. This method also prints a message to standard output; this is picked up by the EJB server and logged. In this way we can demonstrate that the method is actually being executed on the server, rather than the client. */ public double calculateCompoundInterest(double principle, double rate, double periods) { System.out.println("Someone called `calculateCompoundInterest!'"); return principle * Math.pow(1+rate, periods) - principle; } /** Empty method body */ public void ejbCreate() {} /** Every ejbCreate() method ALWAYS needs a corresponding ejbPostCreate() method with exactly the same parameter types. */ public void ejbPostCreate() {} /** Empty method body */ public void ejbRemove() {} /** Empty method body */ public void ejbActivate() {} /** Empty method body */ public void ejbPassivate() {} /** Empty method body */ public void setSessionContext(SessionContext sc) {} } |
| <?xml version="1.0" encoding="UTF-8"?> <ejb-jar> <description>JBoss Interest Sample Application</description> <display-name>Interest EJB</display-name> <enterprise-beans> <session> <ejb-name>Interest</ejb-name> <home>org.jboss.docs.interest.InterestHome</home> <remote>org.jboss.docs.interest.Interest</remote> <ejb-class>org.jboss.docs.interest.InterestBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> </session> </enterprise-beans> </ejb-jar> |
| <?xml version="1.0" encoding="UTF-8"?> <jboss> <enterprise-beans> <session> <ejb-name>Interest</ejb-name> <jndi-name>interest/Interest</jndi-name> </session> </enterprise-beans> </jboss> |
| <target name="validate-servlet"> <!-- Override with your web server servlet jar location. The default assumes that JBOSS_DIST points to a JBoss/Tomcat bundle distribution --> <available property="servlet.jar" value="${env.JBOSS_DIST}/../tomcat/lib/servlet.jar" file="${env.JBOSS_DIST}/../tomcat/lib/servlet.jar"/> <available property="servlet.jar" value="${env.JBOSS_DIST}/../jetty/lib/javax.servlet.jar" file="${env.JBOSS_DIST}/../jetty/lib/javax.servlet.jar"/> <available property="servlet.jar" value="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar" file="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar"/> <available property="servlet.jar" value="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar" file="${env.TOMCAT_HOME}/lib/servlet.jar"/> <property name="servlet.jar" value="COULD_NOT_FIND_SERVLET_JAR"/> <path id="base.path_22"> <pathelement location="${jboss.dist}/client/ejb.jar"/> <pathelement location="${jboss.dist}/client/jaas.jar"/> <pathelement location="${jboss.dist}/client/jbosssx-client.jar"/> <pathelement location="${jboss.dist}/client/jboss-client.jar"/> <pathelement location="${jboss.dist}/client/jnp-client.jar"/> <pathelement location="${servlet.jar}"/> </path> <path id="base.path_24"> <pathelement location="${jboss.dist}/client/jboss-j2ee.jar"/> <pathelement location="${jboss.dist}/client/jaas.jar"/> <pathelement location="${jboss.dist}/client/jbosssx-client.jar"/> <pathelement location="${jboss.dist}/client/jboss-client.jar"/> <pathelement location="${jboss.dist}/client/jnp-client.jar"/> <pathelement location="${servlet.jar}"/> </path> </target> <target name="validate-jboss" depends="validate-servlet"> <available property="classpath_id" value="base.path_22" file="${jboss.dist}/client/ejb.jar" /> <available property="classpath_id" value="base.path_24" file="${jboss.dist}/client/jboss-j2ee.jar" /> </target> |
| <target name="validate-servlet"> <!-- Override with your web server servlet jar location. The default assumes that JBOSS_DIST points to a JBoss/Tomcat bundle distribution --> <available property="servlet.jar" value="${env.JBOSS_DIST}/tomcat-4.1.x/common/lib/servlet.jar" file="${env.JBOSS_DIST}/tomcat-4.1.x/common/lib/servlet.jar"/> <available property="servlet.jar" value="${env.JBOSS_DIST}/../jetty/lib/javax.servlet.jar" file="${env.JBOSS_DIST}/../jetty/lib/javax.servlet.jar"/> <available property="servlet.jar" value="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar" file="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar"/> <available property="servlet.jar" value="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar" file="${env.TOMCAT_HOME}/lib/servlet.jar"/> <property name="servlet.jar" value="COULD_NOT_FIND_SERVLET_JAR"/> <path id="base.path_22"> <pathelement location="${jboss.dist}/client/ejb.jar"/> <pathelement location="${jboss.dist}/client/jaas.jar"/> <pathelement location="${jboss.dist}/client/jbosssx-client.jar"/> <pathelement location="${jboss.dist}/client/jboss-client.jar"/> <pathelement location="${jboss.dist}/client/jnp-client.jar"/> <pathelement location="${servlet.jar}"/> </path> <path id="base.path_306"> <pathelement location="${jboss.dist}/client/jboss-j2ee.jar"/> <pathelement location="${jboss.dist}/client/jboss-jaas.jar"/> <pathelement location="${jboss.dist}/client/jbosssx-client.jar"/> <pathelement location="${jboss.dist}/client/jboss-client.jar"/> <pathelement location="${jboss.dist}/client/jnp-client.jar"/> <pathelement location="${jboss.dist}/client/jbossall-client.jar"/> <pathelement location="${jboss.dist}/client/log4j.jar"/> <pathelement location="${servlet.jar}"/> </path> </target> <target name="validate-jboss" depends="validate-servlet"> <available property="classpath_id" value="base.path_22" file="${jboss.dist}/client/ejb.jar" /> <available property="classpath_id" value="base.path_306" file="${jboss.dist}/client/jboss-j2ee.jar" /> </target> |
| package org.jboss.docs.interest; import javax.naming.InitialContext; import javax.naming.Context; import javax.rmi.PortableRemoteObject; import org.jboss.docs.interest.Interest; import org.jboss.docs.interest.InterestHome; /** This simple application tests the 'Interest' Enterprise JavaBean which is implemented in the package 'org.jboss.docs.interest'. For this to work, the Bean must be deployed on an EJB server. */ public class InterestClient { /** This method does all the work. It creates an instance of the Interest EJB on the EJB server, and calls its `calculateCompoundInterest()' method, then prints the result of the calculation. */ public static void main(String[] args) { // Enclosing the whole process in a single `try' block is not an ideal way // to do exception handling, but I don't want to clutter the program up // with catch blocks try { // Get a naming context InitialContext jndiContext = new InitialContext(); System.out.println("Got context"); // Get a reference to the Interest Bean Object ref = jndiContext.lookup("interest/Interest"); System.out.println("Got reference"); // Get a reference from this to the Bean's Home interface InterestHome home = (InterestHome) PortableRemoteObject.narrow(ref, InterestHome.class); // Create an Interest object from the Home interface Interest interest = home.create(); // call the calculateCompoundInterest() method to do the calculation System.out.println("Interest on 1000 units, at 10% per period, compounded over 2 periods is:"); System.out.println(interest.calculateCompoundInterest(1000, 0.10, 2)); } catch(Exception e) { System.out.println(e.toString()); } } } |
| java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.provider.url=localhost:1099 java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces |
| <target name="deploy-ejb-jar" depends="ejb-jar"> <copy file="${build.interest.dir}/interest.jar" todir="${jboss.dist}/deploy" /> </target> <target name="deploy-ear" depends="ear"> <copy file="${build.interest.dir}/interest.ear" todir="${jboss.dist}/deploy" /> </target> |
| <target name="deploy-ejb-jar" depends="ejb-jar"> <copy file="${build.interest.dir}/interest.jar" todir="${jboss.dist}/server/default/deploy" /> </target> <target name="deploy-ear" depends="ear"> <copy file="${build.interest.dir}/interest.ear" todir="${jboss.dist}/server/default/deploy" /> </target> |
- 作者: neitbe 2004年09月2日, 星期四 16:05
你可以使用这个链接引用该篇日志 http://publishblog.blogdriver.com/blog/tb.b?diaryID=308368