时间: 2021-07-31 作者:daque
在ejb2.1的典型中须要实行ejbtimeout本领,固然再有ejbpassivate、ejbremove等本领。在ejb3.0中,惟有你想用它们的功夫,你才必需创造它们,要不不用实行。 这个例子重要有5个文献,这个例子的bean是一个无状况对话bean: newstimer.java:交易接口。 newstimer.java:交易实行类。未来咱们开拓的ejb也都是如许定名(在接口名上加上bean)。 client.java:尝试ejb的存户端类。 jndi.properties:jndi属性文献,供给考察jdni的基础摆设属性。 build.xml:ant 摆设文献,用以编写翻译、颁布、尝试、废除ejb。 底下对准每个文献的实质做一个引见。 newstimer.java package com.kuaff.ejb3.schedule; import javax.ejb.remote; @remote public interface newstimer { public void fivenews(); } 这个接口设置了fivenews本领,即使这个本领被挪用,5秒钟后将往遏制台上输入一条消息。 你不用摆设它的jndi称呼,也不用写它的摆设文献。在jboss实行的ejb3.0中,你不用写任何的ejb安置文献和jboss安置文献。jboss默许运用接口的全称动作它的jndi名。在上头的例子中,它的全称不妨经过newstimerclass.forname()获得。 newstimerbean.java package com.kuaff.ejb3.schedule; import java.util.date; import javax.ejb.inject; import javax.ejb.sessioncontext; import javax.ejb.stateless; import javax.ejb.timer; @stateless public class newstimerbean implements newstimer { private @inject sessioncontext ctx; public void fivenews() { ctx.gettimerservice().createtimer(new date(new date().gettime() + 300000), "海市蜃楼电视台5秒钟消息栏目:此刻过5秒钟,又到立即消息节手段功夫了。"); } public void ejbtimeout(timer timer) { system.out.printf("功夫到:%n%s%n" , timer.getinfo()); timer.cancel(); } } client.java package com.kuaff.ejb3.schedule; import javax.naming.initialcontext; import javax.naming.namingexception; public class client { public static void main(string[] args) throws namingexception { initialcontext ctx = new initialcontext(); newstimer timer = (newstimer) ctx.lookup(newstimer.class.getname()); timer.fivenews(); } } 这个类用来尝试咱们颁布的计数器ejb。开始经过 ctx = new initialcontext(); 获得左右文,而后经过lookup搜索newstimer,而后启用计时。。 请运转{$jboss_home}/bin目次下的run.bat: run –c all,启用jboss。 在eclipse的ant视图中实行ejbjar target。大概在吩咐行下,加入到此工程目次下,实行ant ejbjar,将编写翻译打包颁布此ejb。 在eclipse的ant视图中实行run target。大概在吩咐行下,加入到此工程目次下,实行ant run,尝试这个ejb。