use of org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler 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.scheduling.concurrent.ThreadPoolTaskScheduler in project spring-integration by spring-projects.
the class SourcePollingChannelAdapterFactoryBeanTests method testInterrupted.
@Test
public void testInterrupted() throws Exception {
final CountDownLatch startLatch = new CountDownLatch(1);
MessageSource<Object> ms = () -> {
startLatch.countDown();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new MessagingException("Interrupted awaiting stopLatch", e);
}
return null;
};
SourcePollingChannelAdapter pollingChannelAdapter = new SourcePollingChannelAdapter();
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
taskScheduler.setAwaitTerminationSeconds(1);
taskScheduler.afterPropertiesSet();
pollingChannelAdapter.setTaskScheduler(taskScheduler);
MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
Log errorHandlerLogger = TestUtils.getPropertyValue(errorHandler, "logger", Log.class);
errorHandlerLogger = spy(errorHandlerLogger);
DirectFieldAccessor dfa = new DirectFieldAccessor(errorHandler);
dfa.setPropertyValue("logger", errorHandlerLogger);
pollingChannelAdapter.setErrorHandler(errorHandler);
pollingChannelAdapter.setSource(ms);
pollingChannelAdapter.setOutputChannel(new NullChannel());
pollingChannelAdapter.setBeanFactory(mock(BeanFactory.class));
pollingChannelAdapter.afterPropertiesSet();
Log adapterLogger = TestUtils.getPropertyValue(pollingChannelAdapter, "logger", Log.class);
adapterLogger = spy(adapterLogger);
when(adapterLogger.isDebugEnabled()).thenReturn(true);
dfa = new DirectFieldAccessor(pollingChannelAdapter);
dfa.setPropertyValue("logger", adapterLogger);
pollingChannelAdapter.start();
assertTrue(startLatch.await(10, TimeUnit.SECONDS));
pollingChannelAdapter.stop();
taskScheduler.shutdown();
verifyZeroInteractions(errorHandlerLogger);
verify(adapterLogger).debug(contains("Poll interrupted - during stop()?"));
}
use of org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler in project spring-integration by spring-projects.
the class DefaultConfiguringBeanFactoryPostProcessor method registerTaskScheduler.
/**
* Register a TaskScheduler in the given BeanDefinitionRegistry.
*/
private void registerTaskScheduler(BeanDefinitionRegistry registry) {
if (this.logger.isInfoEnabled()) {
this.logger.info("No bean named '" + IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME + "' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.");
}
BeanDefinition scheduler = BeanDefinitionBuilder.genericBeanDefinition(ThreadPoolTaskScheduler.class).addPropertyValue("poolSize", IntegrationProperties.getExpressionFor(IntegrationProperties.TASK_SCHEDULER_POOL_SIZE)).addPropertyValue("threadNamePrefix", "task-scheduler-").addPropertyValue("rejectedExecutionHandler", new CallerRunsPolicy()).addPropertyValue("errorHandler", new RootBeanDefinition(MessagePublishingErrorHandler.class)).getBeanDefinition();
registry.registerBeanDefinition(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, scheduler);
}
use of org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler in project spring-integration by spring-projects.
the class OutboundGatewayFunctionTests method testContainerWithDestNoCorrelation.
@Test
public void testContainerWithDestNoCorrelation() throws Exception {
BeanFactory beanFactory = mock(BeanFactory.class);
when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.initialize();
when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)).thenReturn(scheduler);
final JmsOutboundGateway gateway = new JmsOutboundGateway();
gateway.setBeanFactory(beanFactory);
gateway.setConnectionFactory(getConnectionFactory());
gateway.setRequestDestination(requestQueue2);
gateway.setReplyDestination(replyQueue2);
gateway.setUseReplyContainer(true);
gateway.afterPropertiesSet();
gateway.start();
final AtomicReference<Object> reply = new AtomicReference<Object>();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
ExecutorService exec = Executors.newSingleThreadExecutor();
exec.execute(() -> {
latch1.countDown();
try {
reply.set(gateway.handleRequestMessage(new GenericMessage<String>("foo")));
} finally {
latch2.countDown();
}
});
assertTrue(latch1.await(10, TimeUnit.SECONDS));
JmsTemplate template = new JmsTemplate();
template.setConnectionFactory(getConnectionFactory());
template.setReceiveTimeout(10000);
javax.jms.Message request = template.receive(requestQueue2);
assertNotNull(request);
final javax.jms.Message jmsReply = request;
template.send(request.getJMSReplyTo(), (MessageCreator) session -> {
jmsReply.setJMSCorrelationID(jmsReply.getJMSMessageID());
return jmsReply;
});
assertTrue(latch2.await(20, TimeUnit.SECONDS));
assertNotNull(reply.get());
gateway.stop();
scheduler.destroy();
exec.shutdownNow();
}
use of org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler in project spring-integration by spring-projects.
the class OutboundGatewayFunctionTests method testContainerWithDest.
@Test
public void testContainerWithDest() throws Exception {
BeanFactory beanFactory = mock(BeanFactory.class);
when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.initialize();
when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)).thenReturn(scheduler);
final JmsOutboundGateway gateway = new JmsOutboundGateway();
gateway.setBeanFactory(beanFactory);
ConnectionFactory connectionFactory = getConnectionFactory();
gateway.setConnectionFactory(connectionFactory);
gateway.setRequestDestination(requestQueue1);
gateway.setReplyDestination(replyQueue1);
gateway.setCorrelationKey("JMSCorrelationID");
gateway.setUseReplyContainer(true);
gateway.afterPropertiesSet();
gateway.start();
final AtomicReference<Object> reply = new AtomicReference<Object>();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
ExecutorService exec = Executors.newSingleThreadExecutor();
exec.execute(() -> {
latch1.countDown();
try {
reply.set(gateway.handleRequestMessage(new GenericMessage<String>("foo")));
} finally {
latch2.countDown();
}
});
assertTrue(latch1.await(10, TimeUnit.SECONDS));
JmsTemplate template = new JmsTemplate();
template.setConnectionFactory(connectionFactory);
template.setReceiveTimeout(10000);
javax.jms.Message request = template.receive(requestQueue1);
assertNotNull(request);
final javax.jms.Message jmsReply = request;
template.send(request.getJMSReplyTo(), (MessageCreator) session -> jmsReply);
assertTrue(latch2.await(10, TimeUnit.SECONDS));
assertNotNull(reply.get());
gateway.stop();
scheduler.destroy();
exec.shutdown();
}
Aggregations