Context properties and attachments in bean services
kcbabo May 1, 2013 10:44 AMFollowing up on three recent observations by Magesh:
- Bean services cannot set out/response context properties after the removal of Scope.IN and Scope.OUT.
- Bean services can accept an attachment, but not return an attachment.
- Bean references cannot access context properties or attachments for requests and replies.
I think we can all agree that bean services should be able to do all three of the above. Personally, I tend to think that using context properties in service implementations should be avoided whenever possible as a best practice, we all know there are occasions when you just have to do it. Let's look at what machinery is in place right now for bean implementations:
@Inject Context context
This allows you to inject a Context reference for the current Message instance for an invocation, which provides access to the request message context and exchange context. It does not provide access to context for the reply message. The context instance is only valid for invocations of the bean service and is not related to "outbound" invocations via @Reference injections.
@Inject Message message
Message injection makes a reference to the request message available to a bean service implementation. The reply message reference, if applicable, is not available. Message instances used by @Reference invocations are not available.
Possible solutions:
#1) Add an annotation which allows Context and Message injection to be customized to point to input or output message. It would need to support further parameterization to indicate if it applies to a reference. This could be done by the Java type of a reference. Examples:
// Injection of out context @Inject @Target(message="out") Context context;
// Injection of in context for reference invocation @Inject @Target(message="in", reference="myReference") Context context; @Inject @Reference MyReference myReference;
#2) Create a bean-specific interface for accessing context and message instances. For example:
// This is the special interface @Inject BeanSauce beanSauce; @Inject @Reference OtherService otherService; public Object myServiceMethod(Object stuff) { // Get request context Context inCtx = beanSauce.getInContext(); // Get request message Message outMsg = beanSauce.getOutMessage(); // Get request context for reference Context refContext = beanSauce.getInContext(otherService); }
#3) Similar to #2, but don't return Context and Message from BeanSauce. Instead, it should hide Context and Message and simply provide access to set/get properties and set/get attachments. Advantage is that it abstracts away core API details. Disadvantage is that it could be a pain to maintain and keep in sync with core API.
Thoughts?