use of org.springframework.beans.factory.BeanFactory in project spring-integration by spring-projects.
the class FeedEntryMessageSource method onInit.
@Override
protected void onInit() throws Exception {
if (this.metadataStore == null) {
// first try to look for a 'messageStore' in the context
BeanFactory beanFactory = this.getBeanFactory();
if (beanFactory != null) {
this.metadataStore = IntegrationContextUtils.getMetadataStore(beanFactory);
}
// if no 'messageStore' in context, fall back to in-memory Map-based default
if (this.metadataStore == null) {
this.metadataStore = new SimpleMetadataStore();
}
}
String lastTimeValue = this.metadataStore.get(this.metadataKey);
if (StringUtils.hasText(lastTimeValue)) {
this.lastTime = Long.parseLong(lastTimeValue);
}
this.initialized = true;
}
use of org.springframework.beans.factory.BeanFactory in project spring-integration by spring-projects.
the class MessagingMethodInvokerHelper method initialize.
private synchronized void initialize() throws Exception {
if (!this.initialized) {
BeanFactory beanFactory = getBeanFactory();
if (beanFactory != null && beanFactory.containsBean(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME)) {
try {
MessageConverter messageConverter = beanFactory.getBean(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME, MessageConverter.class);
this.messageHandlerMethodFactory.setMessageConverter(messageConverter);
HandlerMethodArgumentResolversHolder handlerMethodArgumentResolversHolder = beanFactory.getBean(this.canProcessMessageList ? IntegrationContextUtils.LIST_ARGUMENT_RESOLVERS_BEAN_NAME : IntegrationContextUtils.ARGUMENT_RESOLVERS_BEAN_NAME, HandlerMethodArgumentResolversHolder.class);
this.messageHandlerMethodFactory.setCustomArgumentResolvers(handlerMethodArgumentResolversHolder.getResolvers());
} catch (NoSuchBeanDefinitionException e) {
configureLocalMessageHandlerFactory();
}
} else {
configureLocalMessageHandlerFactory();
}
this.messageHandlerMethodFactory.afterPropertiesSet();
prepareEvaluationContext();
this.initialized = true;
}
}
use of org.springframework.beans.factory.BeanFactory in project spring-integration by spring-projects.
the class ServiceActivatorEndpointTests method testBeanFactoryPopulation.
@Test
public void testBeanFactoryPopulation() {
ServiceActivatingHandler endpoint = this.createEndpoint();
BeanFactory mock = mock(BeanFactory.class);
endpoint.setBeanFactory(mock);
endpoint.afterPropertiesSet();
Object beanFactory = TestUtils.getPropertyValue(endpoint, "processor.beanFactory");
assertNotNull(beanFactory);
assertSame(mock, beanFactory);
}
use of org.springframework.beans.factory.BeanFactory in project spring-integration by spring-projects.
the class IdempotentReceiverTests method testIdempotentReceiverInterceptor.
@Test
public void testIdempotentReceiverInterceptor() {
ConcurrentMetadataStore store = new SimpleMetadataStore();
ExpressionEvaluatingMessageProcessor<String> idempotentKeyStrategy = new ExpressionEvaluatingMessageProcessor<>(new SpelExpressionParser().parseExpression("payload"));
BeanFactory beanFactory = Mockito.mock(BeanFactory.class);
idempotentKeyStrategy.setBeanFactory(beanFactory);
IdempotentReceiverInterceptor idempotentReceiverInterceptor = new IdempotentReceiverInterceptor(new MetadataStoreSelector(idempotentKeyStrategy, store));
idempotentReceiverInterceptor.setThrowExceptionOnRejection(true);
AtomicReference<Message<?>> handled = new AtomicReference<>();
MessageHandler idempotentReceiver = handled::set;
ProxyFactory proxyFactory = new ProxyFactory(idempotentReceiver);
proxyFactory.addAdvice(idempotentReceiverInterceptor);
idempotentReceiver = (MessageHandler) proxyFactory.getProxy();
idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
assertEquals(1, TestUtils.getPropertyValue(store, "metadata", Map.class).size());
assertNotNull(store.get("foo"));
try {
idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
fail("MessageRejectedException expected");
} catch (Exception e) {
assertThat(e, instanceOf(MessageRejectedException.class));
}
idempotentReceiverInterceptor.setThrowExceptionOnRejection(false);
idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
assertTrue(handled.get().getHeaders().get(IntegrationMessageHeaderAccessor.DUPLICATE_MESSAGE, Boolean.class));
assertEquals(1, TestUtils.getPropertyValue(store, "metadata", Map.class).size());
}
use of org.springframework.beans.factory.BeanFactory in project spring-integration by spring-projects.
the class RouterConcurrencyTest method test.
@Test
public void test() throws Exception {
final AtomicInteger count = new AtomicInteger();
final Semaphore semaphore = new Semaphore(1);
final AbstractMessageRouter router = new AbstractMessageRouter() {
@Override
protected Collection<MessageChannel> determineTargetChannels(Message<?> message) {
return null;
}
@Override
protected void setConversionService(ConversionService conversionService) {
try {
if (count.incrementAndGet() > 1) {
Thread.sleep(2000);
}
super.setConversionService(conversionService);
semaphore.release();
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
final AtomicInteger beanCounter = new AtomicInteger();
BeanFactory beanFactory = mock(BeanFactory.class);
doAnswer(invocation -> {
if (beanCounter.getAndIncrement() < 2) {
semaphore.tryAcquire(4, TimeUnit.SECONDS);
}
return false;
}).when(beanFactory).containsBean(IntegrationUtils.INTEGRATION_CONVERSION_SERVICE_BEAN_NAME);
router.setBeanFactory(beanFactory);
ExecutorService exec = Executors.newFixedThreadPool(2);
final List<ConversionService> returns = Collections.synchronizedList(new ArrayList<ConversionService>());
Runnable runnable = () -> {
ConversionService requiredConversionService = router.getRequiredConversionService();
returns.add(requiredConversionService);
};
exec.execute(runnable);
exec.execute(runnable);
exec.shutdown();
exec.awaitTermination(10, TimeUnit.SECONDS);
assertEquals(2, returns.size());
assertNotNull(returns.get(0));
assertNotNull(returns.get(1));
}
Aggregations