...
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
Here is an example of a SU deploymennt. The SU is only a zip of these two files:
...
Code Block | ||
---|---|---|
| ||
import org.apache.servicemix.common.ExchangeProcessor; import javax.jbi.messaging.MessageExchange; import javax.jbi.messaging.NormalizedMessage; import org.apache.servicemix.jbi.jaxp.StringSource; import org.apache.servicemix.script.ScriptExchangeHelper; import org.apache.servicemix.jbi.jaxp.SourceTransformer; class GroovyExchangeProcessor implements ExchangeProcessor { @Property ScriptExchangeHelper exchangeHelper; def void start() { println "Starting"; } def void process(MessageExchange exchange) { def inputMessage = new SourceTransformer().toString(exchange.getInMessage().getContent()); println "Hello, I got an input message "+inputMessage; NormalizedMessage out = exchange.createMessage(); out.setContent(new StringSource("<world>hello</world>")); exchange.setMessage(out, "out"); exchangeHelper.sendExchange(exchange); } def void stop() { println "Stopping"; } } |
Tip | |||||
---|---|---|---|---|---|
| |||||
Alternatively, you can inline the script in the
|
...
Info | ||
---|---|---|
| ||
The script defines a class that will be used to process incoming exchanges, but only a single instance of this class will be used. This means that you need to make sure the script is thread safe and can be used to process requests concurrently. On the other side, this means that maintaining state is just a matter of defining a property on the class and using it to read / store any state that will be maintained. |
Script Helpers
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 implements the Script Helper 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 | ||
---|---|---|
| ||
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:
...
Warning | ||
---|---|---|
| ||
When acting as a Consumer (i.e. creating and sending an exchange into the bus), this Endpoint is currently limited to sending InOnly requests. |
Tip | ||
---|---|---|
| ||
|
...
|
...
|
...
|
...
|
...
|