分类:
2008-11-16 22:30:05
… @Remote @Local @Stateless public class CounterBean { …… } |
Counter.java package com.kuaff.ejb3.stateless; import javax.ejb.Remote; @Remote public interface Counter { public int add(int i); public int getNumber(); } |
CounterBean.java package com.kuaff.ejb3.stateless; import javax.ejb.Stateless; @Stateless public class CounterBean implements Counter { private int number = 0; //给计数器加上i public int add(int i) { number += i; return number; } //得到当前的计数 public int getNumber() { return number; } } |
Client.java package com.kuaff.ejb3.stateless; import javax.naming.InitialContext; import javax.naming.NamingException; public class Client { public static void main(String[] args) { InitialContext ctx; try ctx = new InitialContext(); Counter counter = (Counter) ctx.lookup(Counter.class.getName()); counter.add(10); System.out.println("当前的number:" + counter.getNumber()); counter.add(10); System.out.println("当前的number:" + counter.getNumber()); Counter counter2 = (Counter) ctx.lookup(Counter.class.getName()); counter2.add(10); System.out.println("当前的number:" + counter2.getNumber()); catch (NamingException e) { e.printStackTrace(); } } } |
jndi.properties java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces java.naming.provider.url=localhost |
<project name="kuaff_ejb3_tutorial" default="ejbjar" basedir="."> <property environment="env"/> <property name="src.dir" value="${basedir}/src"/> <property name="jboss.home" value="${env.JBOSS_HOME}"/> <property name="build.dir" value="${basedir}/build"/> <property name="build.classes.dir" value="${build.dir}/classes"/> <path id="classpath"> <fileset dir="${jboss.home}/lib"> <include name="**/*.jar"/> </fileset> <fileset dir="${jboss.home}/server/all/lib"> <include name="**/*.jar"/> </fileset> <fileset dir="${jboss.home}/server/all/deploy/ejb3.deployer"> <include name="*.jar"/> </fileset> <fileset dir="${jboss.home}/server/all/deploy/jboss-aop.deployer"> <include name="*.jar"/> </fileset> <pathelement location="${build.classes.dir}"/> <pathelement location="${basedir}"/> </path> <property name="build.classpath" refid="classpath"/> <target name="prepare"> <mkdir dir="${build.dir}"/> <mkdir dir="${build.classes.dir}"/> </target> <target name="compile" depends="prepare"> <javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on" deprecation="on" optimize="off" includes="**"> <classpath refid="classpath"/> </javac> </target> <target name="ejbjar" depends="compile"> <jar jarfile="build/StatelessSample.ejb3"> <fileset dir="${build.classes.dir}"> <include name="**/*.class"/> </fileset> </jar> <copy file="build/StatelessSample.ejb3" todir="${jboss.home}/server/all/deploy"/> </target> <target name="run" depends="ejbjar"> <java classname="com.kuaff.ejb3.stateless.Client" fork="yes" dir="."> <classpath refid="classpath"/> </java> </target> <target name="clean.db"> <delete dir="${jboss.home}/server/all/data/hypersonic"/> </target> <target name="clean"> <delete dir="${build.dir}"/> <delete file="${jboss.home}/server/all/deploy/StatelessSample.ejb3"/> </target> </project> |