Search in sources :

Example 1 with NamedComponent

use of org.springframework.integration.support.context.NamedComponent in project spring-integration by spring-projects.

the class RouterSpec method channelMapping.

/**
 * @param key the key.
 * @param channelName the channelName.
 * @return the router spec.
 * @see AbstractMappingMessageRouter#setChannelMapping(String, String)
 */
public RouterSpec<K, R> channelMapping(K key, final String channelName) {
    Assert.notNull(key, "'key' must not be null");
    Assert.hasText(channelName, "'channelName' must not be null");
    if (key instanceof String) {
        this.handler.setChannelMapping((String) key, channelName);
    } else {
        this.mappingProvider.addMapping(key, new NamedComponent() {

            @Override
            public String getComponentName() {
                return channelName;
            }

            @Override
            public String getComponentType() {
                return "channel";
            }
        });
    }
    return _this();
}
Also used : NamedComponent(org.springframework.integration.support.context.NamedComponent)

Example 2 with NamedComponent

use of org.springframework.integration.support.context.NamedComponent in project spring-integration by spring-projects.

the class AmqpOutboundChannelAdapterParserTests method verifyIdAsChannel.

@Test
public void verifyIdAsChannel() {
    Object channel = context.getBean("rabbitOutbound");
    Object adapter = context.getBean("rabbitOutbound.adapter");
    assertEquals(DirectChannel.class, channel.getClass());
    assertEquals(EventDrivenConsumer.class, adapter.getClass());
    MessageHandler handler = TestUtils.getPropertyValue(adapter, "handler", MessageHandler.class);
    assertTrue(handler instanceof NamedComponent);
    assertEquals("amqp:outbound-channel-adapter", ((NamedComponent) handler).getComponentType());
    handler.handleMessage(new GenericMessage<String>("foo"));
    assertEquals(1, adviceCalled);
    assertTrue(TestUtils.getPropertyValue(handler, "lazyConnect", Boolean.class));
}
Also used : MessageHandler(org.springframework.messaging.MessageHandler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) NamedComponent(org.springframework.integration.support.context.NamedComponent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 3 with NamedComponent

use of org.springframework.integration.support.context.NamedComponent in project spring-integration by spring-projects.

the class AbstractSimpleMessageHandlerFactoryBean method createHandlerInternal.

protected final H createHandlerInternal() {
    synchronized (this.initializationMonitor) {
        if (this.initialized) {
            // There was a problem when this method was called already
            return null;
        }
        this.handler = createHandler();
        if (this.handler instanceof ApplicationContextAware && this.applicationContext != null) {
            ((ApplicationContextAware) this.handler).setApplicationContext(this.applicationContext);
        }
        if (this.handler instanceof BeanFactoryAware && getBeanFactory() != null) {
            ((BeanFactoryAware) this.handler).setBeanFactory(getBeanFactory());
        }
        if (this.handler instanceof BeanNameAware && this.beanName != null) {
            ((BeanNameAware) this.handler).setBeanName(this.beanName);
        }
        if (this.handler instanceof ApplicationEventPublisherAware && this.applicationEventPublisher != null) {
            ((ApplicationEventPublisherAware) this.handler).setApplicationEventPublisher(this.applicationEventPublisher);
        }
        if (this.handler instanceof MessageProducer && this.outputChannel != null) {
            ((MessageProducer) this.handler).setOutputChannel(this.outputChannel);
        }
        Object actualHandler = extractTarget(this.handler);
        if (actualHandler == null) {
            actualHandler = this.handler;
        }
        if (actualHandler instanceof IntegrationObjectSupport) {
            if (this.componentName != null) {
                ((IntegrationObjectSupport) actualHandler).setComponentName(this.componentName);
            }
            if (this.channelResolver != null) {
                ((IntegrationObjectSupport) actualHandler).setChannelResolver(this.channelResolver);
            }
        }
        if (!CollectionUtils.isEmpty(this.adviceChain)) {
            if (actualHandler instanceof AbstractReplyProducingMessageHandler) {
                ((AbstractReplyProducingMessageHandler) actualHandler).setAdviceChain(this.adviceChain);
            } else if (this.logger.isDebugEnabled()) {
                String name = this.componentName;
                if (name == null && actualHandler instanceof NamedComponent) {
                    name = ((NamedComponent) actualHandler).getComponentName();
                }
                this.logger.debug("adviceChain can only be set on an AbstractReplyProducingMessageHandler" + (name == null ? "" : (", " + name)) + ".");
            }
        }
        if (this.async != null) {
            if (actualHandler instanceof AbstractMessageProducingHandler) {
                ((AbstractMessageProducingHandler) actualHandler).setAsync(this.async);
            }
        }
        if (this.handler instanceof Orderable && this.order != null) {
            ((Orderable) this.handler).setOrder(this.order);
        }
        this.initialized = true;
    }
    if (this.handler instanceof InitializingBean) {
        try {
            ((InitializingBean) this.handler).afterPropertiesSet();
        } catch (Exception e) {
            throw new BeanInitializationException("failed to initialize MessageHandler", e);
        }
    }
    return this.handler;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) ApplicationContextAware(org.springframework.context.ApplicationContextAware) IntegrationObjectSupport(org.springframework.integration.context.IntegrationObjectSupport) Orderable(org.springframework.integration.context.Orderable) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) NamedComponent(org.springframework.integration.support.context.NamedComponent) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) BeansException(org.springframework.beans.BeansException) BeanNameAware(org.springframework.beans.factory.BeanNameAware) BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) AbstractMessageProducingHandler(org.springframework.integration.handler.AbstractMessageProducingHandler) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MessageProducer(org.springframework.integration.core.MessageProducer) InitializingBean(org.springframework.beans.factory.InitializingBean)

Example 4 with NamedComponent

use of org.springframework.integration.support.context.NamedComponent in project spring-integration by spring-projects.

the class BeanFactoryTypeConverterTests method testMessageHistoryNotConverted.

@Test
public void testMessageHistoryNotConverted() {
    BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter();
    typeConverter.setBeanFactory(new DefaultListableBeanFactory());
    Message<String> message = new GenericMessage<String>("foo");
    message = MessageHistory.write(message, new NamedComponent() {

        @Override
        public String getComponentName() {
            return "bar";
        }

        @Override
        public String getComponentType() {
            return "baz";
        }
    });
    MessageHistory history = MessageHistory.read(message);
    assertSame(history, typeConverter.convertValue(history, TypeDescriptor.valueOf(MessageHistory.class), TypeDescriptor.valueOf(MessageHistory.class)));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHistory(org.springframework.integration.history.MessageHistory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) NamedComponent(org.springframework.integration.support.context.NamedComponent) Test(org.junit.Test)

Example 5 with NamedComponent

use of org.springframework.integration.support.context.NamedComponent in project spring-integration by spring-projects.

the class IntegrationMBeanExporter method registerChannels.

private void registerChannels() {
    for (MessageChannelMetrics monitor : this.channels) {
        String name = ((NamedComponent) monitor).getComponentName();
        this.allChannelsByName.put(name, monitor);
        if (!matches(this.componentNamePatterns, name)) {
            continue;
        }
        String beanKey = getChannelBeanKey(name);
        if (logger.isInfoEnabled()) {
            logger.info("Registering MessageChannel " + name);
        }
        registerBeanNameOrInstance(monitor, beanKey);
    }
}
Also used : MessageChannelMetrics(org.springframework.integration.support.management.MessageChannelMetrics) NamedComponent(org.springframework.integration.support.context.NamedComponent)

Aggregations

NamedComponent (org.springframework.integration.support.context.NamedComponent)9 Test (org.junit.Test)3 BeansException (org.springframework.beans.BeansException)3 Collection (java.util.Collection)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)2 MessageSource (org.springframework.integration.core.MessageSource)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Charset (java.nio.charset.Charset)1 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Pattern (java.util.regex.Pattern)1 JMException (javax.management.JMException)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 TargetSource (org.springframework.aop.TargetSource)1 Advised (org.springframework.aop.framework.Advised)1