定时服务、拦截器和WebServ.ppt

上传人:tia****nde 文档编号:12671762 上传时间:2020-05-13 格式:PPT 页数:18 大小:1.97MB
返回 下载 相关 举报
定时服务、拦截器和WebServ.ppt_第1页
第1页 / 共18页
定时服务、拦截器和WebServ.ppt_第2页
第2页 / 共18页
定时服务、拦截器和WebServ.ppt_第3页
第3页 / 共18页
点击查看更多>>
资源描述
第8章,定时服务、拦截器和WebService,-2-,本章目标,熟悉EJB定时服务的API掌握使用EJB定时服务了解EJB定时服务的局限理解AOP的概念掌握创建EJB拦截器掌握使用EJB拦截器了解EJB默认拦截器和生命周期拦截器熟悉使用EJB发布WebService熟悉在EJB中访问WebService,-3-,Timer接口,EJB定时服务主要涉及两个接口和一个注解:javax.ejb.Timer接口javax.ejb.TimerService接口javax.ejb.Timeout注解Timer接口代表定时器,封装了使用定时服务的EJB中设置的定时事件,-4-,TimerService接口-1,TimerService接口为访问EJB的定时服务提供了支持,利用TimerService接口可以创建新的定时器,也可以列出已有的定时器,使用TimerService接口可以创建一次性的和间隔的两种定时器一次性的定时器只会触发一次间隔定时器会每隔指定的时间就触发一次,-5-,TimerService接口-2,Calendarcalendar=Calendar.getInstance();calendar.set(2011,Calendar.OCTOBER,1);/2011年国庆节Datedate=calendar.getTime();/2011年国庆节longaWeek=7*24*60*60*1000;/一星期longaDay=24*60*60*1000;/一天/使用TimerService创建定时器TimerServicets=./某种方式获取的TimerService对象ts.createTimer(date,一次性定时器,2011年国庆节到期);ts.createTimer(aWeek,一次性定时器,一星期后到期);ts.createTimer(date,aDay,间隔定时器,2011年国庆节开始运行,每隔一天执行一次);ts.createTimer(aWeek,aDay,间隔定时器,一星期后开始运行,每隔一天执行一次);,当创建好一个定时器后,EJB容器会将其存储在某种持久化介质上,因此,即使系统崩溃,计时器仍然可以保存下来,当容器重新启动后,计时器便可以恢复。,TimerServicets=./某种方式获取的TimerService对象for(Objectt:ts.getTimers()(Timer)t).cancel();,-6-,Timeout注解,当定时器到期时,EJB定时服务会调用关联此定时器的EJB中的超时方法,超时方法需要使用javax.ejb.Timeout注解标记使用Timeout注解标记的方法应该遵循一些约定,即返回类型为void并且有且只有一个Timer类型的参数,TimeoutpublicvoidsomeMethod(Timertimer).,一个EJB中只能有一个方法被Timeout注解标记为超时方法,超时方法中可以利用Timer参数的getInfo()方法获得计时器携带的信息,TimeoutpublicvoidsomeMethod(Timertimer)SomeBusinessObjectbo=(SomeBusinessObject)timer.getInfo();.,-7-,获取TimerService,只能在无状态会话Bean和消息驱动Bean中使用EJB定时服务获取TimerService实例标注超时方法创建定时器Timer通过Resource注解注入和EJBContext的getTimerService()方法可以获得TimerService的实例,StatelesspublicclassSomeBeanimplementsSomeBusinessInterfaceResourceTimerServicetimerService;,StatelesspublicclassSomeBeanimplementsSomeBusinessInterfaceResourceEJBContextejbContext;publicvoidsomeMethod()TimerServicetimerService=ejbContext.getTimerService();.,-8-,使用定时服务,StatelesspublicclassReportTimerBeanimplementsReportTimerService/注入TimerServiceResourceprivateTimerServicetimerService;publicvoidstart()Calendarcalendar=./计算定时器开始时间timerService.createTimer(calendar.getTime(),calendar);/取消定时器publicvoidstop()for(Objecttimer:timerService.getTimers()(Timer)timer).cancel();TimeoutpublicvoidgenerateReport(Timertimer)Objectinfo=timer.getInfo();/获得定时起携带的信息/业务操作/开始一个新的计时器start();,/定时起无法自动启动,需要客户端主动调用publicclassReportListenerimplementsServletContextListenerEJBReportTimerServicereportTimerService;OverridepublicvoidcontextInitialized(ServletContextEventarg0)reportTimerService.start();OverridepublicvoidcontextDestroyed(ServletContextEventarg0),任务8.D.2,-9-,AOP和EJB拦截器,classSomeBean1implementsSomeInterface1voidsomeMethod1()beginTransaction();commit();,classSomeBean2implementsSomeInterface2voidsomeMethod()2beginTransaction();commit();,classSomeBean3implementsSomeInterface3voidsomeMethod()3beginTransaction();commit();,面向方面程序设计(aspect-orientedprogramming,AOP)使用AOP后,这些重复的代码可以在一个统一的位置只编写一次,而业务方法中只需简单的配置即可自动调用这些代码EJB通过拦截器机制提供了方法级的AOP功能编写拦截器后,只需将其注册到需要被增强的业务方法,则这个业务方法被调用时将自动执行拦截器中的相关方法,代码重复,无法以一种简单的方式将其抽取到合适的位置,-10-,创建拦截器,使用javax.interceptor.AroundInvoke注解创建拦截器要求返回类型为Object并且有且只有一个InvocationContext类型参数,publicclassSomeInterceptorAroundInvokepublicObjectsomeMethod(InvocationContextcontext).,publicclassLogInterceptorAroundInvokepublicObjectlog(InvocationContextcontext)throwsExceptionStringejbName=context.getTarget().getClass().getName();StringmethodName=context.getMethod().getName();StringBuildersb=newStringBuilder();sb.append(调用了).append(ejbName).append(的).append(methodName).append(方法,参数为:);for(Objectparameter:context.getParameters()sb.append(parameter).append(,);System.out.println(sb);returncontext.proceed();,-11-,使用拦截器,使用javax.interceptor.Interceptors注解可以将EJB与拦截器关联,StatelessInterceptors(Interceptor1.class,Interceptor2.class)publicclassSomeBeanimplementsSomeBusinessInterfacepublicvoidmethod1().publicvoidmethod2().,StatelesspublicclassSomeBeanimplementsSomeBusinessInterfaceInterceptors(Interceptor1.class,Interceptor2.class)publicvoidmethod1().publicvoidmethod2().,StatelesspublicclassInventoryServiceBeanimplementsInventoryServiceLocal/注入EJB容器的持久化上下文PersistenceContext(unitName=ejb3theory)privateEntityManagerentityManager;Interceptors(LogInterceptor.class)publicListgetInventories(StringproductName,StringproductCode)Stringjpql=“;Queryquery=entityManager.createQuery(jpql);if(productName=null)productName=;if(productCode=null)productCode=;query.setParameter(productName,%+productName+%);query.setParameter(productCode,%+productCode+%);returnquery.getResultList();,-12-,默认拦截器,使用部署描述文件可以配置默认拦截器,*pkg1.pkg2.SomeInterceptor,只有在部署描述文件中才能使用通配符配置默认拦截器,使用注解无法指定当存在多种拦截器时,EJB会按照默认拦截器类拦截器方法拦截器的顺序执行,而每种类型中的多个拦截器则按照声明的顺序执行使用javax.interceptor包下的ExcludeDefaultInterceptors和ExcludeClassInterceptors注解可以禁用默认拦截器和类上的拦截器,-13-,生命周期拦截器,当EJB的生命周期状态改变时,会触发生命周期拦截器的拦截方法生命周期拦截器的定义方式与业务方法拦截器类似,但是不再使用AroundInvoke注解,而是使用EJB生命周期回调的相关注解,并且生命周期拦截方法必须返回void,而不是Object,publicclassSomeInterceptorPostConstructpublicvoidmethod1(InvocationContextcontext).PreDestroypublicvoidmethod2(InvocationContextcontext).,StatelessInterceptors(SomeInterceptor.class)publicclassSomeBeanimplementsSomeBusinessInterface.,-14-,WebService注解,使用javax.jws.WebService注解标注业务接口或无状态会话Bean即可将其所有业务方法发布为WebService,Target(TYPE)Retention(RUNTIME)publicinterfaceWebServiceStringname()default;StringtargetNamespace()default;StringserviceName()default;StringwsdlLocation()default;StringportName()default;StringendpointInterface()default;,WebServicepublicinterfaceInventoryServiceWSdoublegetInventory(StringproductCode);,StatelessWebServicepublicclassInventoryServiceBeanimplementsInventoryServiceLocalpublicdoublegetInventory(StringproductCode).publicvoidotherMethod().,-15-,WebMethod注解,使用自动生成的端点接口时,无状态会话Bean中的所有public方法都将被暴露为WebService,如果不希望如此,可以使用javax.jws.WebMethod注解,Target(METHOD)Retention(RUNTIME)publicinterfaceWebMethodStringoperationName()default;Stringaction()default;booleanexclude()defaultfalse;,StatelessWebServicepublicclassInventoryServiceBeanimplementsInventoryServiceLocalpublicdoublegetInventory(StringproductCode).WebMethod(exclude=true)publicvoidotherMethod().,-16-,WebServiceRef注解,使用javax.xml.ws.WebServiceRef注解可以访问WebService,Target(TYPE,METHOD,FIELD)Retention(RUNTIME)publicinterfaceWebServiceRefStringname()default;StringwsdlLocation()default;Classtype()defaultObject.class;Classvalue()defaultObject.class;StringmappedName()default;,StatelesspublicclassTestWebServiceClientBeanimplementsTestWebServiceClientInterfaceWebServiceRef(wsdlLocation=http:/127.0.0.1:8080/ch08-ch08EJB/InventoryServiceBean?wsdl)InventoryServiceWSinventoryService;Overridepublicvoidtest()System.out.println(inventoryService.getInventory(1111);,-17-,小结,EJB定时服务使用Timer、TimerService和Timeout定义Timer接口表示定时期TimerService接口用于访问EJB的定时服务Timeout注解用于标识超时方法EJB通过拦截器机制提供了方法级的AOP功能使用AroundInvoke注解可以创建EJB拦截器使用Interceptors注解可以将EJB与拦截器关联EJB还支持默认拦截器和生命周期拦截器使用WebService注解标注业务接口或无状态会话Bean可以发布WebServiceWebMethod注解用于详细定义WebService暴露的方法使用WebServiceRef注解可以访问WebService,-18-,谢谢!,
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!