Search in sources :

Example 1 with ExpressionFactoryBean

use of org.springframework.integration.config.ExpressionFactoryBean in project spring-integration by spring-projects.

the class ExpressionEvaluatingMessageSourceIntegrationTests method test.

@Test
public void test() throws Exception {
    QueueChannel channel = new QueueChannel();
    String payloadExpression = "'test-' + T(org.springframework.integration.endpoint.ExpressionEvaluatingMessageSourceIntegrationTests).next()";
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.afterPropertiesSet();
    Map<String, Expression> headerExpressions = new HashMap<String, Expression>();
    headerExpressions.put("foo", new LiteralExpression("x"));
    headerExpressions.put("bar", new SpelExpressionParser().parseExpression("7 * 6"));
    ExpressionFactoryBean factoryBean = new ExpressionFactoryBean(payloadExpression);
    factoryBean.afterPropertiesSet();
    Expression expression = factoryBean.getObject();
    ExpressionEvaluatingMessageSource<Object> source = new ExpressionEvaluatingMessageSource<Object>(expression, Object.class);
    source.setBeanFactory(mock(BeanFactory.class));
    source.setHeaderExpressions(headerExpressions);
    SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
    adapter.setSource(source);
    adapter.setTaskScheduler(scheduler);
    adapter.setMaxMessagesPerPoll(3);
    adapter.setTrigger(new PeriodicTrigger(60000));
    adapter.setOutputChannel(channel);
    adapter.setErrorHandler(t -> {
        throw new IllegalStateException("unexpected exception in test", t);
    });
    adapter.start();
    List<Message<?>> messages = new ArrayList<Message<?>>();
    for (int i = 0; i < 3; i++) {
        messages.add(channel.receive(1000));
    }
    scheduler.destroy();
    Message<?> message1 = messages.get(0);
    assertEquals("test-1", message1.getPayload());
    assertEquals("x", message1.getHeaders().get("foo"));
    assertEquals(42, message1.getHeaders().get("bar"));
    Message<?> message2 = messages.get(1);
    assertEquals("test-2", message2.getPayload());
    assertEquals("x", message2.getHeaders().get("foo"));
    assertEquals(42, message2.getHeaders().get("bar"));
    Message<?> message3 = messages.get(2);
    assertEquals("test-3", message3.getPayload());
    assertEquals("x", message3.getHeaders().get("foo"));
    assertEquals(42, message3.getHeaders().get("bar"));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) HashMap(java.util.HashMap) LiteralExpression(org.springframework.expression.common.LiteralExpression) ArrayList(java.util.ArrayList) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) ExpressionFactoryBean(org.springframework.integration.config.ExpressionFactoryBean) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 2 with ExpressionFactoryBean

use of org.springframework.integration.config.ExpressionFactoryBean in project spring-integration by spring-projects.

the class StoredProcMessageHandlerDerbyIntegrationTests method testDerbyStoredProcInsertWithDefaultSqlSourceAndDynamicProcName.

@Test
public void testDerbyStoredProcInsertWithDefaultSqlSourceAndDynamicProcName() throws Exception {
    StoredProcExecutor storedProcExecutor = new StoredProcExecutor(embeddedDatabase);
    StoredProcMessageHandler messageHandler = new StoredProcMessageHandler(storedProcExecutor);
    final ExpressionFactoryBean efb = new ExpressionFactoryBean("headers['stored_procedure_name']");
    efb.afterPropertiesSet();
    final Expression expression = efb.getObject();
    storedProcExecutor.setStoredProcedureNameExpression(expression);
    storedProcExecutor.setBeanFactory(mock(BeanFactory.class));
    storedProcExecutor.afterPropertiesSet();
    messageHandler.setBeanFactory(mock(BeanFactory.class));
    messageHandler.afterPropertiesSet();
    MessageBuilder<User> message = MessageBuilder.withPayload(new User("username", "password", "email"));
    message.setHeader("stored_procedure_name", "CREATE_USER");
    messageHandler.handleMessage(message.build());
    Map<String, Object> map = jdbcTemplate.queryForMap("SELECT * FROM USERS WHERE USERNAME=?", "username");
    assertEquals("Wrong username", "username", map.get("USERNAME"));
    assertEquals("Wrong password", "password", map.get("PASSWORD"));
    assertEquals("Wrong email", "email", map.get("EMAIL"));
}
Also used : ExpressionFactoryBean(org.springframework.integration.config.ExpressionFactoryBean) User(org.springframework.integration.jdbc.storedproc.User) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 3 with ExpressionFactoryBean

use of org.springframework.integration.config.ExpressionFactoryBean in project spring-integration by spring-projects.

the class StoredProcExecutorTests method testSetJdbcCallOperationsCacheSize.

@Test
public void testSetJdbcCallOperationsCacheSize() throws Exception {
    final DataSource datasource = mock(DataSource.class);
    final StoredProcExecutor storedProcExecutor = new StoredProcExecutor(datasource);
    storedProcExecutor.setJdbcCallOperationsCacheSize(0);
    final ExpressionFactoryBean efb = new ExpressionFactoryBean("headers['stored_procedure_name']");
    efb.afterPropertiesSet();
    final Expression expression = efb.getObject();
    storedProcExecutor.setStoredProcedureNameExpression(expression);
    storedProcExecutor.setBeanFactory(mock(BeanFactory.class));
    storedProcExecutor.afterPropertiesSet();
    this.mockTheOperationsCache(storedProcExecutor);
    for (int i = 1; i <= 10; i++) {
        storedProcExecutor.executeStoredProcedure(MessageBuilder.withPayload("test").setHeader("stored_procedure_name", "123").build());
    }
    final CacheStats stats = (CacheStats) storedProcExecutor.getJdbcCallOperationsCacheStatistics();
    LOGGER.info(stats);
    assertEquals("Expected a cache misscount of 10", 10, stats.missCount());
}
Also used : ExpressionFactoryBean(org.springframework.integration.config.ExpressionFactoryBean) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) CacheStats(com.google.common.cache.CacheStats) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 4 with ExpressionFactoryBean

use of org.springframework.integration.config.ExpressionFactoryBean in project spring-integration by spring-projects.

the class ContentEnricherTests method replyChannelReplyTimingOut.

/**
 * In this test a {@link Target} message is passed into a {@link ContentEnricher}.
 * The Enricher passes the message to a "request-channel" that is backed by a
 * {@link QueueChannel}. The consumer of the "request-channel" takes a long
 * time to execute, longer actually than the specified "replyTimeout" set on
 * the {@link ContentEnricher}.
 *
 * Due to the occurring replyTimeout, a Null replyMessage is returned and because
 * "requiresReply" is set to "true" on the {@link ContentEnricher}, a
 * {@link ReplyRequiredException} is raised.
 */
@Test
public void replyChannelReplyTimingOut() throws Exception {
    final long requestTimeout = 500L;
    final long replyTimeout = 100L;
    final DirectChannel replyChannel = new DirectChannel();
    final QueueChannel requestChannel = new QueueChannel(1);
    final ContentEnricher enricher = new ContentEnricher();
    enricher.setRequestChannel(requestChannel);
    enricher.setReplyChannel(replyChannel);
    enricher.setOutputChannel(new NullChannel());
    enricher.setRequestTimeout(requestTimeout);
    enricher.setReplyTimeout(replyTimeout);
    final ExpressionFactoryBean expressionFactoryBean = new ExpressionFactoryBean("payload");
    expressionFactoryBean.setSingleton(false);
    expressionFactoryBean.afterPropertiesSet();
    final Map<String, Expression> expressions = new HashMap<String, Expression>();
    expressions.put("name", new LiteralExpression("cartman"));
    expressions.put("child.name", expressionFactoryBean.getObject());
    enricher.setPropertyExpressions(expressions);
    enricher.setRequiresReply(true);
    enricher.setBeanName("Enricher");
    enricher.setBeanFactory(mock(BeanFactory.class));
    enricher.afterPropertiesSet();
    final AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }
            return new Target("child");
        }
    };
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    final PollingConsumer consumer = new PollingConsumer(requestChannel, handler);
    final TestErrorHandler errorHandler = new TestErrorHandler();
    consumer.setTrigger(new PeriodicTrigger(0));
    consumer.setErrorHandler(errorHandler);
    consumer.setTaskScheduler(taskScheduler);
    consumer.setBeanFactory(mock(BeanFactory.class));
    consumer.afterPropertiesSet();
    consumer.start();
    final Target target = new Target("replace me");
    Message<?> requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build();
    try {
        enricher.handleMessage(requestMessage);
    } catch (ReplyRequiredException e) {
        assertEquals("No reply produced by handler 'Enricher', and its 'requiresReply' property is set to true.", e.getMessage());
        return;
    }
    fail("ReplyRequiredException expected.");
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) ReplyRequiredException(org.springframework.integration.handler.ReplyRequiredException) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) HashMap(java.util.HashMap) TestErrorHandler(org.springframework.integration.config.TestErrorHandler) LiteralExpression(org.springframework.expression.common.LiteralExpression) Matchers.containsString(org.hamcrest.Matchers.containsString) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) ExpressionFactoryBean(org.springframework.integration.config.ExpressionFactoryBean) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) NullChannel(org.springframework.integration.channel.NullChannel) Test(org.junit.Test)

Example 5 with ExpressionFactoryBean

use of org.springframework.integration.config.ExpressionFactoryBean in project spring-integration by spring-projects.

the class StoredProcMessageHandlerDerbyIntegrationTests method testDerbyStoredProcInsertWithDefaultSqlSourceAndSpelProcName.

@Test
public void testDerbyStoredProcInsertWithDefaultSqlSourceAndSpelProcName() throws Exception {
    StoredProcExecutor storedProcExecutor = new StoredProcExecutor(embeddedDatabase);
    StoredProcMessageHandler messageHandler = new StoredProcMessageHandler(storedProcExecutor);
    ExpressionFactoryBean efb = new ExpressionFactoryBean("headers.headerWithProcedureName");
    efb.afterPropertiesSet();
    Expression expression = efb.getObject();
    storedProcExecutor.setStoredProcedureNameExpression(expression);
    storedProcExecutor.setBeanFactory(mock(BeanFactory.class));
    storedProcExecutor.afterPropertiesSet();
    messageHandler.setBeanFactory(mock(BeanFactory.class));
    messageHandler.afterPropertiesSet();
    MessageBuilder<User> message = MessageBuilder.withPayload(new User("username", "password", "email"));
    message.setHeader("headerWithProcedureName", "CREATE_USER");
    messageHandler.handleMessage(message.build());
    Map<String, Object> map = jdbcTemplate.queryForMap("SELECT * FROM USERS WHERE USERNAME=?", "username");
    assertEquals("Wrong username", "username", map.get("USERNAME"));
    assertEquals("Wrong password", "password", map.get("PASSWORD"));
    assertEquals("Wrong email", "email", map.get("EMAIL"));
}
Also used : ExpressionFactoryBean(org.springframework.integration.config.ExpressionFactoryBean) User(org.springframework.integration.jdbc.storedproc.User) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 BeanFactory (org.springframework.beans.factory.BeanFactory)8 Expression (org.springframework.expression.Expression)8 ExpressionFactoryBean (org.springframework.integration.config.ExpressionFactoryBean)8 DataSource (javax.sql.DataSource)4 CacheStats (com.google.common.cache.CacheStats)2 HashMap (java.util.HashMap)2 LiteralExpression (org.springframework.expression.common.LiteralExpression)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 User (org.springframework.integration.jdbc.storedproc.User)2 Message (org.springframework.messaging.Message)2 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)2 ArrayList (java.util.ArrayList)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1 NullChannel (org.springframework.integration.channel.NullChannel)1 TestErrorHandler (org.springframework.integration.config.TestErrorHandler)1 PollingConsumer (org.springframework.integration.endpoint.PollingConsumer)1 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)1