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"));
}
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"));
}
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());
}
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.");
}
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"));
}
Aggregations