Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

ServiceMix Script

Info

The ServiceMix Script component is deprecated. Please use the ServiceMix Scripting component instead.

The ServiceMix Script component provides JBI integration with scripting engines.Note that this component is only available in releases >= 3.1 and older scripting component will be deprecated in future releases.

This component leverages Spring support for dynamic languages, so you will find useful informations on the Spring site.

Simple example

Maven Archetype

You can use Maven servicemix-script-service-unit archetype to create Script service unit:

Code Block

mvn archetype:create \
  -DarchetypeGroupId=org.apache.servicemix.tooling \
  -DarchetypeArtifactId=servicemix-script-service-unit \
  -DarchetypeVersion=2010.01 \
  -DgroupId=your.group.id \
  -DartifactId=your.artifact.id \
  -Dversion=your-version

Endpoint Configuration

The Script endpoint is able to run Groovy script to process messages.Here is an example of a SU deploymennt. The SU is only a zip of these two files:

Code Block
titlexbean.xml
<beans xmlns:script="http://org.apache.servicemix/script/1.0"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:test="urn:test">
  <script:exchangeProcessor service="test:groovy" endpoint="endpoint">
    <property name="helpers">
      <list>
        <ref bean="groovyExchangeHelper" />
      </list>
    </property>
    <property name="implementation" ref="groovyExchangeProcessor" />
  </script:exchangeProcessor>
  <script:exchangeHelper id="groovyExchangeHelper" singleton="true" />
  <lang:groovy id="groovyExchangeProcessor"
               script-source="classpath:GroovyExchangeProcessor.groovy">
    <lang:property name="exchangeHelper" ref="groovyExchangeHelper" />
  </lang:groovy>
</beans>

...

Warning
titleSending exchanges

When acting as a Consumer (i.e. creating and sending an exchange into the bus), this Endpoint is currently limited to sending InOnly requests. This limitation is solved for versions >= 3.2.2

Tip
titleTips when creating Script Helpers
  • When defining your own helper, if you do not extend the ScriptExchangeHelper then you will need to change the type in the Groovy script to ScriptHelper for the exchangeHelper propery
  • if you extend the ScriptExchangeHelper then you have to call the super.setScriptExchangeProcessorEndpoint() method in order to set the private member there too, otherwise you will experience Null Pointer Exceptions
  • to use your own helper, you have to use the Spring <bean /> element in the xbean.xml as shown in the above example because the script:exchangeHelper is fixed to use the ScriptExchangeHelper class
  • as Groovy is a dynamic language, there is no need to upcast the exchange helper property in the Groovy script: even though the ScriptHelper type is specified in the script, Groovy will be able to find the methods in the sub class by introspecting the object