Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In order to create Message Exchanges one needs the Delivery Channel to obtain the Message Exchange Factory to create them. This is not possible by default but can be easily added using a customer Script Helper class. The only thing to do is to create a class that extends implements the Script Exchange Helper class interface and use the Script Exchange Processor Endpoint instance to obtain the Delivery Channel from. Now you can either provide the Delivery Channel as is or you can create convenience method to create Message Exchanges for example. This is how a customer Script Helper class would look like:

Code Block
titlefoo.MyScriptHelper.java
package foo;

import javax.jbi.messaging.DeliveryChannel;

import org.apache.servicemix.script.ScriptExchangeHelper;
import org.apache.servicemix.script.ScriptExchangeProcessorEndpoint;
import org.apache.servicemix.script.ScriptHelper;

public class MyScriptExchangeHelper
   extendsimplements ScriptExchangeHelperScriptHelper
{
    protected ScriptExchangeProcessorEndpoint mEndpoint;

    public void setScriptExchangeProcessorEndpoint(
        ScriptExchangeProcessorEndpoint pEndpoint
    ) {
        mEndpoint = pEndpoint;
        super.setScriptExchangeProcessorEndpoint( pEndpoint );
    }

    public DeliveryChannel getChannel() {
        return mEndpoint.getChannel();
    }
}

...

Code Block
titlexbean.xml changes
<bean
    id="groovyExchangeHelper"
    class="com.ge.gehc.scout.xmessaging.testfoo.MyScriptExchangeHelper"
/>

<!--<script:exchangeHelper id="groovyExchangeHelper" singleton="true" />-->

The Groovy code could then look like this:

Code Block
titleGroovyExchangeProcessor.groovy

import org.apache.servicemix.common.ExchangeProcessor;
import javax.jbi.messaging.MessageExchange;
import org.apache.servicemix.jbi.jaxp.StringSource;
import org.apache.servicemix.script.ScriptHelper;
import org.apache.commons.logging.LogFactory;

class GroovyExchangeProcessor
    implements ExchangeProcessor
{
    @Property ScriptHelper exchangeHelper;

    def mLog = LogFactory.getLog( getClass() );

    def void start() {
        mLog.info( "Starting" );
    }

    def void process(MessageExchange pExchange) {
        def lChannel = exchangeHelper.getChannel();
        def lRequest = lChannel.createExchangeFactory().createInOutExchange();
        lRequest.setService( new QName( "urn:test", "target-service" ) );
        lRequest.setOperation( new QName( "MyOpertation" ) );
        def lNM = lRequest.createMessage();
        lNM.setContent( new StringSource( "<receiver><title>Zero</title><index>0</index></receiver>" ) );
        lRequest.setInMessage( lNM );
        mLog.info( "process() send new ME: " + lRequest );
        lChannel.send( lRequest );
    }

    def void stop() {
        mLog.info( "Stopping" );
    }
}

Attention:

  • if you do not extend the ScriptExchangeHelper then you need to change the type in the Groovy script to ScriptHelper for the exchangeHelper propery
  • if you extend the ScriptExchangeHelper then you have don't forget to call the super.setScriptExchangePocessorEndpointsetScriptExchangeProcessorEndpoint() to make sure that the base class does not throw Null Pointer Exceptions (because the super class has its own private member for this instance and this must be set)method in order to set the private member there too, otherwise you will experience Null Pointer Exceptions
  • you have to use the Spring Bean in the xbean.xml because the script:exchangeHelper is fixed to use the super class
  • no need to upcast the exchange helper property in the Groovy script; even though the type is specified there Groovy still is able to find the method in the sub class

...