Announcement Announcement Module
Collapse
No announcement yet.
Using transformer element to create a standalone transformer bean instead of endpoit Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using transformer element to create a standalone transformer bean instead of endpoit

    Hello.

    Is there any way to use "transformer" element to create a standalone transformer bean but not an end point connected to channels?

    Consider I'd like to replace character 'A' with 'B' in string payload. When I'm in <chain> i can do it like this:
    Code:
    <int:chain input-channel="..." output-channel="...">
        ...
        <int:transformer expression="payload.replace('A', 'B')" />
        ...
    </int:chain>
    But if I want to reuse that transformer in several chains, I have to create manually transformer and SpEL expression instance! It looks awful :
    Code:
    <bean id="spelParser" class="org.springframework.expression.spel.standard.SpelExpressionParser" />
    <bean id="replaceABtransformer" class="org.springframework.integration.transformer.ExpressionEvaluatingTransformer">
        <constructor-arg name="expression">
            <bean factory-bean="spelParser" factory-method="parseExpression">
                <constructor-arg name="expressionString" value="payload.replace('A', 'B')" />
            </bean>
        </constructor-arg>
    </bean>
    
    <int:chain input-channel="..." output-channel="...">
        ...
        <int:transformer ref="replaceABtransformer" />
        ...
    </int:chain>
    Regards,
    -- Alexey
    Last edited by Alexey Pomelov; Jan 11th, 2013, 08:53 AM.

  • #2
    Hi!

    In general you should understand Messaging Archtecture.
    I recommend to read the books: http://martinfowler.com/books/eip.html & http://www.manning.com/fisher.
    Now how it works.
    <transformer> is a custom tag who produces a POJO handler and an Messaging Endpoint. So, it requires an input-channel attribute.
    The <chain> make the same, but it produces its own Messaging Endpoint and is a container for nested handlers.
    To reduce config and reuse solution you should look at this as an architect who just connects endpoints via channels to achieve the best loosely coupling.
    So, how about this one:
    HTML Code:
    <transformer input-channel="replaceTransformerChannel" expression="payload.replace('A', 'B')" />
    
    <chain input-channel="..." output-channel="...">
        ...
      <gateway request-channel="replaceTransformerChannel"/>
        ...
    </chain>
    So, in this case you just reuse a channel.

    From other side, there is no any shame to configure beans as show above

    Take care,
    Artem

    Comment


    • #3
      Thanks for your reply!
      I understand the difference between transformer POJO and end point POJO.
      In fact my sample is synthetic, the original purpose is to reuse the transformer in channel interceptors. So I'm creating the bean of class MessageTransformingChannelInterceptor and it wants to get a transformer as constructor argument.

      Comment


      • #4
        MessageTransformingChannelInterceptor and it wants to get a transformer as constructor argument

        something like this:
        HTML Code:
        <channel id="someChannel">
        		<interceptors>
        			<beans:bean class="org.springframework.integration.transformer.MessageTransformingChannelInterceptor">
        				<beans:constructor-arg>
        					<beans:bean class="org.springframework.integration.transformer.MethodInvokingTransformer">
        						<beans:constructor-arg>
        							<beans:bean class="org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor">
        								<beans:constructor-arg>
        									<beans:bean class="org.springframework.scripting.support.StaticScriptSource">
        										<beans:constructor-arg>
        											<beans:value>
        												<![CDATA[
        													[key1: payload['value1'],
        													 key2: payload['value2'],
        													 key3: payload['value3']
        													]]>
        											</beans:value>
        										</beans:constructor-arg>
        									</beans:bean>
        								</beans:constructor-arg>
        							</beans:bean>
        						</beans:constructor-arg>
        					</beans:bean>
        				</beans:constructor-arg>
        			</beans:bean>
        		</interceptors>
        	</channel>
        IMHO MessageTransformingChannelInterceptor is anti-pattern. It breaks some principles of Messaging Architecture...

        Comment

        Working...
        X