Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Section
borderfalse
Column
width15%
Include Page
TUSCANY: SCA Java Subproject Menu
TUSCANY: SCA Java Subproject Menu
Include Page
TUSCANY: Java SCA Menu New
TUSCANY: Java SCA Menu New
Column
width85%

<binding.jms>

The Tuscany Java SCA runtime supports the Java Messaging Service using the <binding.jms> SCDL extension. New JMS based service endpoints can be provided using a <binding.jms> element within a SCA <service>, existing JMS queues can be accessed using a <binding.jms> element within a SCA <reference>.

The JMS binding is one of the SCA extensions which is being formalized in the OASIS Open Composite Services Architecture with a published specifications document.

Using the JMS binding

The simplest way to use the JMS binding is to use the URI syntax to configure the binding, for example:

Code Block
<binding.jms uri="jms:RequestQueue"/>

This tells the binding to use a JMS destination named "RequestQueue", with all the other configuration options using default values.

By default Tuscany will use a JMS connection factory named 'ConnectionFactory', this can be changed by using a query parameter in the URI, for example, to use a connection factory named 'myCF' can be done as follows:

Code Block
<binding.jms uri="jms:RequestQueue?connectionFactoryName=myCF"/>

(lightbulb) When using a SCA reference for RPC style requests and no response destination is defined in the SCDL then a temporary replyTo queue will automatically be created and used.

When using the JMS binding with SCA services the syntax can be simplified even further by letting the destination name default to the service name. For example, the following SCDL snippet creates a JMS service listening on a JMS destination named "MyService":

Code Block
<service name="MyService">
   <binding.jms />
</service>

Some examples:

HelloWorld

The helloworld-jms sample demonstrates basic RPC style operations over JMS. The sample has one component exposing a JMS service on a queue name 'HelloWorldService' and another component which invokes the service by sending JMS messages to that queue. A temporary destination is used for the response messages. The .composite file for this is shown below, see the helloworld sample README for full details.

Code Block
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
           targetNamespace="http://sample"
           xmlns:sample="http://sample"
           name="HelloWorld">

    <component name="HelloWorldClient">
        <implementation.java class="helloworld.HelloWorldClient"/>
        <reference name="helloWorldRef">
            <binding.jms uri="jms:HelloWorldService"/>
        </reference>
    </component>

    <component name="HelloWorldServiceComponent">
        <implementation.java class="helloworld.HelloWorldServiceImpl" />
	<service name="HelloWorldService">
            <binding.jms />
        </service>
    </component>

</composite>

Using SCA callbacks with the JMS binding

The Tuscany JMS binding supports using SCA callbacks for creating asynchronous clients and services.

This is done by using a <callback> element in the SCDL for the service or reference as shown in the following example.
When no destination is defined on the callback binding of a <reference> then a JMS temporary queue will be used.
See the callbacks-jms sample for a complete example of using callbacks

Code Block
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
           targetNamespace="http://sample"
           name="CallbacksComposite">

    <component name="ClientComponent">
        <implementation.java class="callbacks.OrderServiceClient" />
        <reference name="orderService" target="ServiceComponent/OrderService">
            <binding.jms uri="jms:OrderService"/>
            <callback>
                <binding.jms />
            </callback>
        </reference>
    </component>

    <component name="ServiceComponent">
        <implementation.java class="callbacks.OrderServiceImpl" />
        <service name="OrderService">
            <binding.jms />
            <callback>
                <binding.jms />
            </callback>
        </service>
    </component>

</composite>

(lightbulb) When no destination is defined on the callback binding of a <reference> then a JMS temporary queue will be used.

Configuring JMS resources

Tuscany locates all JMS resources from JNDI so the environment where Tuscany is running needs to have JNDI and JMS correctly configured in order to use the Tuscany JMS binding.

The following describes how to configure JMS in some common environments:

Tuscany J2SE standalone environment with ActiveMQ

The Tuscany standalone runtime can use an embedded Apache ActiveMQ message broker. To use ActiveMQ the application needs to include the JMS API and ActiveMQ jars in the classpath and include a jndi.properties file to configure the ActiveMQ resources in JNDI.

An example of this can be seen in the Tuscany JMS itest which uses the ActiveMQ 4.1.1 release and this jndi.properties file.

For more information on using ActiveMQ see the Apache ActiveMQ website and specifically this page for information about configuring JNDI resources.

Apache Tomcat

Tomcat does not include a JMS broker by default so you need to either embed one in each Tuscany application, install a broker into the tomcat installation, or use an external broker. Once that is done JNDI resources can be defined using the standard Tomcat facilities, see the Tomcat JNDI How-to.

The Tuscany samples that use JMS and Tomcat demonstrate how to embed a JMS broker within the application by including ActiveMQ and its dependencies within the sample WAR, and using the webapp META-INF/context.xml file to define the JMS resources in JNDI.

JEE application servers such as Apache Geronimo, WebSphere etc

JEE Application servers such as Geronimo, WebSphere, WebLogic etc come with their own JMS broker that can be used by Tuscany. All the JMS resources used by a Tuscany application must be manually defined in the application server. Usually the application server has some sort of admin console where the resources can be defined using a web browser.

The Tuscany helloworld JMS sample README describes the specific details of how to do this for some common application servers.

Note
titleClassCastExceptions when using Tuscany applications in an Application Server

Tuscany applications, specifically the JMS samples, are built to work out of-the-box on Tomcat by including a JMS broker embedded within the application. This causes incompatibilities with some Application Servers because the Java class of JMS resources may use different class loaders in the server and the application. The solution is to delete any JMS API jar included in the application WAR, for example, the geronimo-jms_1.1_spec-1.1.jar.

(tick) Using JEE resource references

When running Tuscany in a JEE environment it can be useful to use JEE resource references to map local application resource names to global JNDI names. This can be done by using <resource-ref> elements in the application deployment descriptor. If a <resource-ref> exists for a JMS binding resource then Tuscany will use that instead of looking up the resource directly in the global JNDI.

For example, adding the following definitions to the helloworld JMS sample web.xml will enable mapping the 'ConnectionFactory' and 'HelloWorldService' names used by the JMS binding into names for the actual resources used on the Application Server. This will normally occur when the application is dployed with the deploy tool asking what real resource names the resourecs should be mapped to.

Code Block
  <resource-ref>
     <res-ref-name>ConnectionFactory</res-ref-name>
     <res-type>javax.jms.ConnectionFactory</res-type>
     <res-auth>Container</res-auth>
     <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>

  <resource-ref>
     <res-ref-name>HelloWorldService</res-ref-name>
     <res-type>javax.jms.Queue</res-type>
     <res-auth>Container</res-auth>
     <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>

Using an external JMS broker

When the Tuscany environment does not include a JMS broker then an external broker may be used by specifying the initialContextFactory and jndiURL attributes on the binding.jms element. Any JMS 1.1 compatible broker should work such as Apache ActiveMQ or any other proprietary broker. The Tuscany application classpath will need to include jars for the initial context factory and all of its dependencies.

An example of using the Tuscany JMS binding with an external ActiveMQ broker is as follows:

Code Block
<binding.jms initialContextFactory="org.apache.activemq.jndi.ActiveMQInitialContextFactory" jndiURL="tcp://localhost:61616">
   <destination name="DestQueueA"/>
</binding.jms>  

Using the WebSphere client for JMS

The Tuscany JMS binding now (post Tuscany SCA 1.2) works the WebSphere client for JMS which enables Tuscany applications running outside of WebSphere to use JMS resources defined within WebSphere. You need to include the JMS client jars in the Tuscany classpath and ensure that the JMS connection factory has the "endpoint providers" field configured in WebSphere. See this email thread for more information.

JMS Message types and message body format

The SCA JMS specification only defines how to use JMS TextMessages which contain XML in the message body, however it is a common requirement to use alternative message types and body formats. Currently Tuscany defines an additional "messageProcessor" attribute on the binding.jms element to support additional message types and payload formats. For the time being this should be considered an interim solution which will be changed in furture Tuscany releases.

The messageProcessor attribute value may be "XMLTextMessage", "TextMessage", "ObjectMessage", or the name of a Java class that implements the org.apache.tuscany.sca.binding.jms.provider.JMSMessageProcessor interface. For example:

Code Block
    <reference name="serviceA" promote="HelloWorldClient/serviceA">
        <binding.jms messageProcessor="my.TestMessageProcessor" />
    </reference>

For a complete example of using the messageProcessor attribute see the MessageProcessorTestCase itest.

JMS binding schema

The complete JMS binding SCDL schema has the following format:

Code Block
<binding.jms correlationScheme="string"?
             initialContextFactory="xs:anyURI"?
             jndiURL="xs:anyURI"?
             requestConnection="QName"?
             responseConnection="QName"?
             operationProperties="QName"?
             ... >

   <destination name="xs:anyURI" type="string"? create="string"?>
      <property name="NMTOKEN" type="NMTOKEN">*
   </destination>?

   <connectionFactory name="xs:anyURI" create="string"?>
      <property name="NMTOKEN" type="NMTOKEN">*
   </connectionFactory>?

   <activationSpec name="xs:anyURI" create="string"?>
      <property name="NMTOKEN" type="NMTOKEN">*
   </activationSpec>?

   <response>
      <destination name="xs:anyURI" type="string"? create="string"?>
         <property name="NMTOKEN" type="NMTOKEN">*
      </destination>?
      <connectionFactory name="xs:anyURI" create="string"?>
         <property name="NMTOKEN" type="NMTOKEN">*
      </connectionFactory>?
      <activationSpec name="xs:anyURI" create="string"?>
         <property name="NMTOKEN" type="NMTOKEN">*
      </activationSpec>?
   </response>?

   <resourceAdapter name="NMTOKEN">?
      <property name="NMTOKEN" type="NMTOKEN">*
   </resourceAdapter>?

   <headers JMSType="string"?
            JMSCorrelationId="string"?
            JMSDeliveryMode="string"?
            JMSTimeToLive="int"?
            JMSPriority="string"?>
      <property name="NMTOKEN" type="NMTOKEN">*
   </headers>?

   <operationProperties name="string" nativeOperation="string"?>
      <property name="NMTOKEN" type="NMTOKEN">*
      <headers JMSType="string"?
               JMSCorrelationId="string"?
               JMSDeliveryMode="string"?
               JMSTimeToLive="int"?
               JMSPriority="string"?>
         <property name="NMTOKEN" type="NMTOKEN">*
      </headers>?
   </operationProperties>*

</binding.jms>

(question) See the JMS Binding Specification 1.0 for full details of each of these configuration options.

(warning) Not all these elements are supported by Tuscany. Specifically, the <activationSpec> and <resourceAdapter> elements are not supported as Tuscany does not use JCA or MDBs for its JMS support. Additionally, support for the requestConnection, responseConnection, and operationProperties attributes has not yet been implemented but this should get done shortly.

(warning) The create attribute on the destination element is not supported in most environments and all JMS resources (connection factories, queues and topics) need to be pre-configured. An exception to this is when using Apache ActiveMQ as the JMS broker then Tuscany may be able to dynamically create queue and topic resources. This is mainly only useful for unit testing and it is recommended that user applications are designed with the expectation that JMS resources need to be preconfigured.