Appendix I. Interceptors: TV Channel Service Example

Description

As we’ve seen with security and transactions, often we have business logic that is not a part of our core concerns. Quite the opposite; applications are likely to have rules that need to be universal across modules or EJBs, and baking this logic into our implementation mixes concerns and makes for an unmaintainable system over time.

Much like Servlet Filters or AOP aspects, the EJB Interceptor model provides a mechanism for application developers to apply logic to incoming requests. The @Interceptors annotation quickly acts as a mapping marker for the EJB container to know to apply a particular interceptor for a given invocation.

Our example represents a television server which is capable of returning channel streams when a channel number is requested by the client. We apply a custom security policy whereby Channel 2 may be closed by an administrator; if a client requests Channel 2 while access is disallowed, the invocation will return a “Channel2ClosedException”. All of this logic is separated from the core code to return the channel requested, and therefore may be applied to other modules or unplugged from the system without need to alter the existing application in any way.

Get Enterprise JavaBeans 3.1, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.