Use Java annotations
for Service Component Architecture (SCA) to identify existing Java
Platform, Enterprise Edition (Java EE) components, such as message-driven
beans, as SCA components that are a part of an SCA composite.
About this task
The SCA programming model supports Java EE integration.
As a result, you 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, you 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 you 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. You must not use a message-driven
bean as a service component implementation more than one time within
the SCA assembly of the application package.
You can configure
a message-driven bean that is 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 by using
the @Reference annotation. You can also use annotations when you want
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 that you want within
your message-driven beans. Based on your needs, use the
supported annotations to inject SCA information into your 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. You 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, you 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 that contains the composite
file is an application enterprise archive (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 that contains 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 Enterprise JavaBeans (EJB) module, omit the path name.
For
example, you 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);
}
}
}
What to do next
Deploy the components to a business-level application.