Use SCA annotations with message-driven beans
Use Java annotations for Service Component Architecture (SCA) to identify existing Java EE components, such as message-driven beans, as SCA components that are a part of an SCA composite.
Identify and obtain the message-driven beans that represent your business logic to enable within an SCA environment.
The SCA programming model supports Java EE integration. As a result, we can take advantage of SCA annotations to enable Java EE web components such as message-driven beans to consume SCA services. By using Java annotations that apply to SCA, we can enable existing message-driven beans to be recognized as an SCA component and participate in an SCA composite.
Message-driven beans can only participate in SCA assembly as the implementation type of a component that does not offer services, even though we can configure or wire the component to other services. Because of the association with endpoints that are not controlled by SCA such as Java Message Service (JMS), do not instantiate message-driven beans arbitrarily. We must not use a message-driven bean as a service component implementation more than one time within the SCA assembly of the application package.
We can configure a message-driven bean defined as an implementation type of an SCA component with annotations in order to obtain references to services that are wired to the component by the SCA assembly using the @Reference annotation. We can also use annotations when to obtain the value of a property using the @Property annotation, to inject a handle to the SCA component context using the @Context annotation or to obtain the component name using the @ComponentName annotation.
For a list of supported annotations for message-driven beans, see the SCA specifications and APIs documentation.
- Add SCA annotations to the components we want within our message-driven beans. Based on the needs, use the supported annotations to inject SCA information into the message-driven beans.
- Edit the application.composite in the META-INF directory of the Java EE JAR file.
Define a component within the application.composite whose implementation is defined by an implementation.ejb element and specifies a message-driven bean within the module. We can define multiple components, each with an implementation.ejb link that points to a message-driven bean.
Because message-driven beans and session beans are enterprise beans, we can uniquely refer to both bean types in an ejb-link element.
The implementation.ejb element is used to declare a service component that is implemented by the message-driven bean component. The component contains information for the annotations. To configure this component implementation, use the following schema:
<implementation.ejb ejb-link="<ejb_link_name>"/>
The enterprise bean that serves as the component implementation is uniquely identified by the <ejb_link_name> attribute. The format of the <ejb_link_name> attribute is identical to the format of the ejb-link element in a Java EE deployment descriptor.
If the Java EE archive containing the composite file is an application EAR file, it is possible that multiple message-driven beans have the same name. In this case, the value of the ejb-link element must be composed of a path name specifying the ejb-jar containing the referenced enterprise bean with the ejb-name of the referenced enterprise bean appended and separated from the path name with the # symbol. The path name is relative to the root of the EAR file. For the case where the Java EE archive is a JAR file for the EJB module, omit the path name.
For example, we can have a JAR module that has the following component defined in the application.composite:
<component name="AnnotationTest"> <implementation.ejb ejb-link="SCA_JEE_Injection.jar#AnnotationTest"/> <property name="property" type="xsd:string">Right</property> <reference name="getServerDateReference" target="GetServerDateServiceComponent"> <interface.java interface="sca.injection.test.GetServerDateService"/> </reference> </component>In the following example, the message-driven bean, AnnotationTestMDB, is consuming an SCA service exposed by the GetServerDataServiceComponent reference. The AnnotationTestMDB message driven bean includes the @Property and @Reference annotations.
@MessageDriven public class AnnotationTestMDB implements MessageListener { //Property Annotations @Property protected String property; //Reference Annotation @Reference protected GetServerDateService getServerDateReference; /** * Default constructor. */ public AnnotationTestMDB() { // TODO Auto-generated constructor stub } private static final String JMSCF_JNDI_NAME = "jms/AnnotationQueueFactory"; private static final String JMSResponseQ_JNDI_NAME = "jms/AnnotationResponseQueue"; /** * @see MessageListener#onMessage(Message) */ public void onMessage(Message message) { String strDefaultReference = null; System.out.println("Inside onMessage()"); try { System.out.println("onMessage: " + "Exercising annotations"); if ( getServerDateReference != null) strDefaultReference = getServerDateReference.getString(); } catch (RuntimeException e) { strError = "Error - Failed WebAnnotationTestServlet.service()!"; e.printStackTrace(); } if (strError != null){ System.out.println("onMessage: " + "Encountered an error while annotation work"); outSB.append("@FINALERROR" + strError); } else { System.out.println("onMessage: " + "Annotations successful: now creating reply message"); outSB.append("@PropertyDefault:" + propertyDefault); outSB.append("@ReferenceDefault:" + strDefaultReference); } } }
Results
You now have SCA-enabled Java EE message-driven beans to take advantage of the SCA programming model.
What to do next
Deploy the components to a business-level application.
Related concepts
Ways to install enterprise applications or modules
Related tasks
Create SCA business-level applications Start business-level applications Use SCA annotations with session beans Use existing Java EE modules and components as SCA implementations
SCA annotations Service Component Architecture specifications and APIs