Search in sources :

Example 1 with Transformer

use of org.springframework.integration.transformer.Transformer in project spring-integration by spring-projects.

the class TransformerFactoryBean method createExpressionEvaluatingHandler.

@Override
protected MessageHandler createExpressionEvaluatingHandler(Expression expression) {
    Transformer transformer = new ExpressionEvaluatingTransformer(expression);
    MessageTransformingHandler handler = this.createHandler(transformer);
    handler.setPrimaryExpression(expression);
    return handler;
}
Also used : ExpressionEvaluatingTransformer(org.springframework.integration.transformer.ExpressionEvaluatingTransformer) MessageTransformingHandler(org.springframework.integration.transformer.MessageTransformingHandler) ExpressionEvaluatingTransformer(org.springframework.integration.transformer.ExpressionEvaluatingTransformer) MethodInvokingTransformer(org.springframework.integration.transformer.MethodInvokingTransformer) Transformer(org.springframework.integration.transformer.Transformer)

Example 2 with Transformer

use of org.springframework.integration.transformer.Transformer in project spring-integration by spring-projects.

the class TransformerFactoryBean method createMethodInvokingHandler.

@Override
protected MessageHandler createMethodInvokingHandler(Object targetObject, String targetMethodName) {
    Assert.notNull(targetObject, "targetObject must not be null");
    Transformer transformer = null;
    if (targetObject instanceof Transformer) {
        transformer = (Transformer) targetObject;
    } else {
        this.checkForIllegalTarget(targetObject, targetMethodName);
        if (StringUtils.hasText(targetMethodName)) {
            transformer = new MethodInvokingTransformer(targetObject, targetMethodName);
        } else {
            transformer = new MethodInvokingTransformer(targetObject);
        }
    }
    return this.createHandler(transformer);
}
Also used : ExpressionEvaluatingTransformer(org.springframework.integration.transformer.ExpressionEvaluatingTransformer) MethodInvokingTransformer(org.springframework.integration.transformer.MethodInvokingTransformer) Transformer(org.springframework.integration.transformer.Transformer) MethodInvokingTransformer(org.springframework.integration.transformer.MethodInvokingTransformer)

Example 3 with Transformer

use of org.springframework.integration.transformer.Transformer in project spring-integration by spring-projects.

the class GroovyTransformerTests method testInt2433VerifyRiddingOfMessageProcessorsWrapping.

@Test
public void testInt2433VerifyRiddingOfMessageProcessorsWrapping() {
    assertTrue(this.groovyTransformerMessageHandler instanceof MessageTransformingHandler);
    Transformer transformer = TestUtils.getPropertyValue(this.groovyTransformerMessageHandler, "transformer", Transformer.class);
    assertTrue(transformer instanceof AbstractMessageProcessingTransformer);
    @SuppressWarnings("rawtypes") MessageProcessor messageProcessor = TestUtils.getPropertyValue(transformer, "messageProcessor", MessageProcessor.class);
    // before it was MethodInvokingMessageProcessor
    assertTrue(messageProcessor instanceof GroovyScriptExecutingMessageProcessor);
}
Also used : AbstractMessageProcessingTransformer(org.springframework.integration.transformer.AbstractMessageProcessingTransformer) MessageTransformingHandler(org.springframework.integration.transformer.MessageTransformingHandler) Transformer(org.springframework.integration.transformer.Transformer) AbstractMessageProcessingTransformer(org.springframework.integration.transformer.AbstractMessageProcessingTransformer) MessageProcessor(org.springframework.integration.handler.MessageProcessor) GroovyScriptExecutingMessageProcessor(org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor) GroovyScriptExecutingMessageProcessor(org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor) Test(org.junit.Test)

Example 4 with Transformer

use of org.springframework.integration.transformer.Transformer in project spring-integration by spring-projects.

the class InboundEndpointTests method testInt2809JavaTypePropertiesToAmqp.

@Test
public void testInt2809JavaTypePropertiesToAmqp() throws Exception {
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container);
    adapter.setMessageConverter(new Jackson2JsonMessageConverter());
    PollableChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    Object payload = new Foo("bar1");
    Transformer objectToJsonTransformer = new ObjectToJsonTransformer();
    Message<?> jsonMessage = objectToJsonTransformer.transform(new GenericMessage<Object>(payload));
    MessageProperties amqpMessageProperties = new MessageProperties();
    amqpMessageProperties.setDeliveryTag(123L);
    org.springframework.amqp.core.Message amqpMessage = new SimpleMessageConverter().toMessage(jsonMessage.getPayload(), amqpMessageProperties);
    DefaultAmqpHeaderMapper.inboundMapper().fromHeadersToRequest(jsonMessage.getHeaders(), amqpMessageProperties);
    ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
    Channel rabbitChannel = mock(Channel.class);
    listener.onMessage(amqpMessage, rabbitChannel);
    Message<?> result = channel.receive(1000);
    assertEquals(payload, result.getPayload());
    assertSame(rabbitChannel, result.getHeaders().get(AmqpHeaders.CHANNEL));
    assertEquals(123L, result.getHeaders().get(AmqpHeaders.DELIVERY_TAG));
}
Also used : ObjectToJsonTransformer(org.springframework.integration.json.ObjectToJsonTransformer) Transformer(org.springframework.integration.transformer.Transformer) JsonToObjectTransformer(org.springframework.integration.json.JsonToObjectTransformer) ObjectToJsonTransformer(org.springframework.integration.json.ObjectToJsonTransformer) QueueChannel(org.springframework.integration.channel.QueueChannel) PollableChannel(org.springframework.messaging.PollableChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) Channel(com.rabbitmq.client.Channel) QueueChannel(org.springframework.integration.channel.QueueChannel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) Jackson2JsonMessageConverter(org.springframework.amqp.support.converter.Jackson2JsonMessageConverter) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) MessageProperties(org.springframework.amqp.core.MessageProperties) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) Test(org.junit.Test)

Example 5 with Transformer

use of org.springframework.integration.transformer.Transformer in project spring-integration by spring-projects.

the class IntegrationFlowDefinition method transform.

/**
 * Populate the {@link MessageTransformingHandler} instance for the provided {@link GenericTransformer}
 * for the specific {@code payloadType} to convert at runtime.
 * In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
 * @param payloadType the {@link Class} for expected payload type.
 * @param genericTransformer the {@link GenericTransformer} to populate.
 * @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
 * @param <P> the payload type - 'transform from'.
 * @param <T> the target type - 'transform to'.
 * @return the current {@link IntegrationFlowDefinition}.
 * @see MethodInvokingTransformer
 * @see LambdaMessageProcessor
 * @see GenericEndpointSpec
 */
public <P, T> B transform(Class<P> payloadType, GenericTransformer<P, T> genericTransformer, Consumer<GenericEndpointSpec<MessageTransformingHandler>> endpointConfigurer) {
    Assert.notNull(genericTransformer, "'genericTransformer' must not be null");
    Transformer transformer = genericTransformer instanceof Transformer ? (Transformer) genericTransformer : (isLambda(genericTransformer) ? new MethodInvokingTransformer(new LambdaMessageProcessor(genericTransformer, payloadType)) : new MethodInvokingTransformer(genericTransformer));
    return addComponent(transformer).handle(new MessageTransformingHandler(transformer), endpointConfigurer);
}
Also used : LambdaMessageProcessor(org.springframework.integration.handler.LambdaMessageProcessor) MessageTransformingHandler(org.springframework.integration.transformer.MessageTransformingHandler) Transformer(org.springframework.integration.transformer.Transformer) MethodInvokingTransformer(org.springframework.integration.transformer.MethodInvokingTransformer) ClaimCheckOutTransformer(org.springframework.integration.transformer.ClaimCheckOutTransformer) GenericTransformer(org.springframework.integration.transformer.GenericTransformer) ExpressionEvaluatingTransformer(org.springframework.integration.transformer.ExpressionEvaluatingTransformer) ClaimCheckInTransformer(org.springframework.integration.transformer.ClaimCheckInTransformer) MethodInvokingTransformer(org.springframework.integration.transformer.MethodInvokingTransformer)

Aggregations

Transformer (org.springframework.integration.transformer.Transformer)5 ExpressionEvaluatingTransformer (org.springframework.integration.transformer.ExpressionEvaluatingTransformer)3 MessageTransformingHandler (org.springframework.integration.transformer.MessageTransformingHandler)3 MethodInvokingTransformer (org.springframework.integration.transformer.MethodInvokingTransformer)3 Test (org.junit.Test)2 Channel (com.rabbitmq.client.Channel)1 MessageProperties (org.springframework.amqp.core.MessageProperties)1 Connection (org.springframework.amqp.rabbit.connection.Connection)1 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)1 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)1 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)1 Jackson2JsonMessageConverter (org.springframework.amqp.support.converter.Jackson2JsonMessageConverter)1 SimpleMessageConverter (org.springframework.amqp.support.converter.SimpleMessageConverter)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 GroovyScriptExecutingMessageProcessor (org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor)1 LambdaMessageProcessor (org.springframework.integration.handler.LambdaMessageProcessor)1 MessageProcessor (org.springframework.integration.handler.MessageProcessor)1 JsonToObjectTransformer (org.springframework.integration.json.JsonToObjectTransformer)1