1 / 37

Nuts and Bolts of Java EE 7 Interceptors

Nuts and Bolts of Java EE 7 Interceptors. Marina Vatkina, Oracle E mmanuel Bernard , Red Hat. Program Agenda. Overview Changes Why Bean Validation uses interceptors New features deep dive JTA declarative transactions with interceptors. Interceptors 1.2. Overview.

lindley
Download Presentation

Nuts and Bolts of Java EE 7 Interceptors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Nuts and Bolts of Java EE 7 Interceptors Marina Vatkina, Oracle Emmanuel Bernard, Red Hat

  2. Program Agenda • Overview • Changes • Why Bean Validation uses interceptors • New features deep dive • JTA declarative transactions with interceptors

  3. Interceptors 1.2 Overview • Maintenance update to the Interceptors 1.1 (JSR-318) • Part of Java EE 7 • Contains common rules for Java EE interceptors • All definitions and rules in one place • Used by developers and container providers

  4. Interceptors 1.2 Feature Summary • Interceptor Bindings are now part of the Interceptors spec • Specification covers interceptors associated using @Interceptorsand @InterceptorBindingannotation • Specification document has better flow and many examples • Added AroundConstructLifecycle interceptor • Introduced standard Priorityranges • Simplified rules for interceptor method signatures

  5. Interceptors 1.2 Interceptor Bindings are now part of the Interceptors spec • Moved Interceptor Bindings chapter from the CDI spec into the Interceptors spec • Improved document flow: common rules and rules specific to binding type • Moved ejb-jar.xml deployment descriptor elements back to the EJB spec • No need to search in 2 specifications for answers • Added examples

  6. Why Bean Validationneeds interceptors

  7. Bean Validation What is Bean Validation • Define and declare constraints on objects • Annotation based • Extensible classUser { @NotNull@Size(max=100) public String getFirstname() {...} @SSN//custom constraint public String getSocialSecurityNumber() { ... } }

  8. Bean Validation What’s new in Bean Validation 1.1 • Validation of parameters and return values upon method call @RequestScoped classUserService { @NotNull List<User> getPersonAboveAge(@Min(18) int age) { ... } }

  9. Bean Validation What’s new in Bean Validation 1.1

  10. Bean Validation The inside story of method validation • Bean Validation offers contracts to validate methods • Some interception technology needs to call them • Use a CDI portable extension to add interceptor to constrained methods • No proprietary hook • Every integration points are standard Java EE

  11. Bean Validation Interceptors: need for improvement • Java EE was missing a thing or two on interceptors • Cross expert group work • Bean Validation, CDI, Interceptor, EJB, Java EE

  12. Bean Validation Validating constructor • Bean Validation also validates constructors • CDI calls Bean Validation when it calls new • Need for an @AroundConstruct

  13. Bean Validation Ordering validators • Validation should happen as late as possible • after security and transaction checks • limit risk of parameter value mutation • Introduction of @Priority

  14. Bean Validation @Priority • Salient (aka number based priority) • Defines ranges • Application and libraries put their interceptors at the right place • Considered alternatives • e.g. partial ordering • Bean Validation priority • Interceptor.Priority.PLATFORM_AFTER+800

  15. Bean Validation Putting it all together • Use CDI portable extension • Attach method and constructor interceptor programmatically • Interceptor has expected priority • Same logic implementable by other platforms

  16. New features deep dive

  17. Interceptors 1.2 AroundConstruct interceptor • New lifecycle callback interceptor type • Can be declared only on an interceptor class • When invoked: • Interceptor instances are created and injection is completed on them • The last InvocationContext.proceed creates the target instance • Can access it afterwards via InvocationContext.getTarget • AroundConstruct completes • Injection is completed on the target instance • PostConstruct is invoked (if defined)

  18. Interceptors 1.2 AroundConstruct interceptor @InterceptorBinding @Target(CONSTRUCTOR) public @interface ValidateConstructor {} @ValidateConstructor @Interceptor public class ValidationInterceptor { @AroundConstruct public void validate_constructor(InvocationContext ctx){...} ... }

  19. Interceptors 1.2 AroundConstruct interceptor public class SomeBean { @ValidateConstructor @Inject SomeBean(...) { ... } public void someMethod() { ... } }

  20. Interceptors 1.2 Ordering Interceptors using the Priority Annotation • Priority annotation enables and orders interceptors bound to a component using interceptor binding • Interceptors with smaller priority values are called first • Global ordering of interceptors bound to a component using interceptor binding • Undefined if more than one interceptor has the same priority • Ignored if used on interceptors bound to a component using the Interceptors annotation.

  21. Interceptors 1.2 Standard Priority ranges • Predefined values: • Interceptor.Priority.PLATFORM_BEFORE = 0 • Interceptor.Priority.LIBRARY_BEFORE = 1000 • Interceptor.Priority.APPLICATION = 2000 • Interceptor.Priority.LIBRARY_AFTER = 3000 • Interceptor.Priority.PLATFORM_AFTER = 4000 • Define ranges between the values

  22. Interceptors 1.2 Ordering Interceptors using the Priority Annotation @Priority(Interceptor.Priority.LIBRARY_BEFORE+10) @Validation @Interceptor public class ValidationInterceptor { ... } OR @Monitored @Interceptor @Priority(2100) public class MonitoringInterceptor{ ... }

  23. Interceptors 1.2 Ordering Interceptors using theInterceptors Annotation @Interceptors({InterceptorA.class, InterceptorB.class}) @Stateless public classMyBean{ public void method1(…) {…} @Interceptors({InterceptorC.class, InterceptorD.class}) public void method2(…) {…} }

  24. Interceptors 1.2 Ordering Interceptors using theInterceptors Annotation @Interceptors({InterceptorA.class, InterceptorB.class}) @Stateless public classMyBean{ public void method1(…) {…} @ExcludeClassInterceptors @Interceptors({InterceptorC.class, InterceptorD.class}) public void method2(…) {…} }

  25. Interceptors 1.2 Common Ordering Rules • @Interceptors interceptors (if any) • Ordered as specified • @InterceptorBindinginterceptors (if any) • Ordered using @Priority • Interceptor methods of the target bean class (if any) • Superclass first • Target bean method or constructor (if applicable)

  26. Interceptors 1.2 Simplified rules for interceptor method signatures • Around-invoke or around-timeout are simple: @AroundInvoke @AroundTimeout public Object monitor(InvocationContext ctx) throws Exception{ // do something return ctx.proceed(); }

  27. Interceptors 1.2 Simplified rules for interceptor method signatures • Lifecycle callback required boilerplate code: @PostConstruct public void monitor_create(InvocationContext ctx) { try { // do something ctx.proceed(); // InvocationContext.proceed() throws Exception } catch (Exception e) { throw new RuntimeException(e); }

  28. Interceptors 1.2 Simplified rules for interceptor method signatures • Any interceptor @AroundInvoke @PostConstruct @PreDestroy public Object monitor(InvocationContext ctx) throws Exception { // do something return ctx.proceed(); // lifecycle callback returns null }

  29. JTA declarative transactions with interceptors

  30. JTA declarative transactions with interceptors @Transactional • Container managed transaction demarcation on CDI managed beans • Modeled after EJB CMT • Declarative transaction demarcation via simple annotation • Default TxType.REQUIRED • Ability to specify exception handling behavior for marking transactions for rollback declaratively

  31. JTA declarative transactions with interceptors @Transactional • An interceptor • Can be applied to a class or a method of a CDI managed bean • Not allowed on EJBs • Priority(Interceptor.Priority.PLATFORM_BEFORE + 200)

  32. JTA declarative transactions with interceptors @Transactional @Transactional public class Account { public int get(String param1, int param2) { } @Transactional(value = Transactional.TxType.REQUIRES_NEW, rollbackOn ={SQLException.class}, dontRollbackOn ={SQLWarning.class}) public void update(String param1, int param2) { }

  33. The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract.It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

  34. Q&A

  35. Graphic Section Divider

More Related