INT-1727, INT-1784, added customizer test to the Groovy ControlBus, updated Groovy documentation with customizer notes

This commit is contained in:
Oleg Zhurakousky
2011-02-04 19:57:48 -05:00
parent a29bd4ff6b
commit 6a29c50f21
3 changed files with 55 additions and 1 deletions

View File

@@ -116,6 +116,21 @@
not be used when using inline script, only when pointing to the script via <code>location</code> attribute.
</important>
If you need control over customization of the Groovy object itself which goes beyond setting variables
(e.g., properties, MetaObject etc.) you can also register a GroovyObjectCustomizer which is an implementation of
<code>org.springframework.scripting.groovy.GroovyObjectCustomizer</code> via 'customizer' attribute.
<programlisting language="xml"><![CDATA[<service-activator input-channel="withScriptVariableGenerator">
<groovy:script location="org/springframework/integration/groovy/config/GroovyServiceActivatorTests.groovy"
script-variable-generator="scriptVarSource" customizer="groovyCustomizer"/>
</service-activator>
<beans:bean id="groovyCustomizer"
class="ofoo.bar.MyGroovyCustomizer"/>]]></programlisting>
Setting custom GroovyObjectCustomizer is NOT mutually exclusive to &ly;variable&gt; sub-element and/or <code>script-variable-generator</code>
and is allowed with inline scripting.
</para>
</section>
@@ -142,5 +157,19 @@
that are annotated with @ManagedResource, implement Spring's
Lifecycle interface or extend Spring's CustomizableThreadCreator base class
(e.g. several of the TaskExecutor and TaskScheduler implementations).</para>
<para>
If you need control over customization of the Groovy object itself which goes beyond setting variables
(e.g., properties, MetaObject etc.) you can also register a GroovyObjectCustomizer which is an implementation of
<code>org.springframework.scripting.groovy.GroovyObjectCustomizer</code> via 'customizer' attribute.
<programlisting language="xml"><![CDATA[<service-activator input-channel="withScriptVariableGenerator">
<groovy:script location="org/springframework/integration/groovy/config/GroovyServiceActivatorTests.groovy"
script-variable-generator="scriptVarSource" customizer="groovyCustomizer"/>
</service-activator>
<beans:bean id="groovyCustomizer"
class="ofoo.bar.MyGroovyCustomizer"/>]]></programlisting>
</para>
</section>
</section>

View File

@@ -12,8 +12,13 @@
<queue/>
</channel>
<groovy:control-bus input-channel="input" output-channel="output" send-timeout="100" order="1" auto-startup="true"/>
<groovy:control-bus input-channel="input" output-channel="output" send-timeout="100" order="1" auto-startup="true"
customizer="groovyCustomizer"/>
<beans:bean id="service" class="org.springframework.integration.groovy.config.GroovyControlBusTests$Service" />
<beans:bean id="groovyCustomizer"
class="org.springframework.integration.groovy.config.GroovyControlBusTests.MyGroovyCustomizer"/>
</beans:beans>

View File

@@ -16,9 +16,14 @@
package org.springframework.integration.groovy.config;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import groovy.lang.GroovyObject;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -29,6 +34,7 @@ import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.scripting.groovy.GroovyObjectCustomizer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -45,13 +51,18 @@ public class GroovyControlBusTests {
@Autowired
private PollableChannel output;
@Autowired
private MyGroovyCustomizer groovyCustomizer;
@Test
public void testOperationOfControlBus() { // long is > 3
this.groovyCustomizer.executed = false;
Message<?> message = MessageBuilder.withPayload("def result = service.convert('aardvark'); def foo = headers.foo; result+foo").setHeader("foo", "bar").build();
this.input.send(message);
assertEquals("catbar", output.receive(0).getPayload());
assertNull(output.receive(0));
assertTrue(this.groovyCustomizer.executed);
}
@ManagedResource
@@ -62,5 +73,14 @@ public class GroovyControlBusTests {
return "cat";
}
}
public static class MyGroovyCustomizer implements GroovyObjectCustomizer{
private volatile boolean executed;
public void customize(GroovyObject goo) {
this.executed = true;
}
}
}