380 likes | 607 Views
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.
E N D
Nuts and Bolts of Java EE 7 Interceptors Marina Vatkina, Oracle Emmanuel 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 • 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
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
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
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() { ... } }
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) { ... } }
Bean Validation What’s new in Bean Validation 1.1
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
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
Bean Validation Validating constructor • Bean Validation also validates constructors • CDI calls Bean Validation when it calls new • Need for an @AroundConstruct
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
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
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
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)
Interceptors 1.2 AroundConstruct interceptor @InterceptorBinding @Target(CONSTRUCTOR) public @interface ValidateConstructor {} @ValidateConstructor @Interceptor public class ValidationInterceptor { @AroundConstruct public void validate_constructor(InvocationContext ctx){...} ... }
Interceptors 1.2 AroundConstruct interceptor public class SomeBean { @ValidateConstructor @Inject SomeBean(...) { ... } public void someMethod() { ... } }
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.
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
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{ ... }
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(…) {…} }
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(…) {…} }
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)
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(); }
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); }
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 }
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
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)
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) { }
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.