Spring Integration IP - превращение XML в Java Config, получение `Найдено неоднозначного типа параметра` - PullRequest
0 голосов
/ 24 октября 2018

Попытка превратить конфигурацию Spring Integration xml в конфигурацию Java.Это xml config: https://github.com/spring-projects/spring-integration-samples/blob/master/basic/tcp-client-server/src/main/resources/META-INF/spring/integration/tcpClientServerDemo-context.xml. В этом примере создается клиентский сокет и серверный сокет и отправляется сообщение от клиента на сервер ... вот и все.

Это конфигурация и Javaклассы, которые я создал.У меня недостаточно опыта работы с СИ, чтобы понять причину возникновения исключения ниже.

@Configuration
@IntegrationComponentScan
@EnableIntegration
public class TcpConfig {

    // CLIENT
    @Bean
    public TcpNetClientConnectionFactory tcpNetClientConnectionFactory() {
        TcpNetClientConnectionFactory tcpNetClientConnectionFactory = new TcpNetClientConnectionFactory("localhost", 8080);
        tcpNetClientConnectionFactory.setSingleUse(true);
        tcpNetClientConnectionFactory.setSoTimeout(10_000);

        return tcpNetClientConnectionFactory;
    }

    @Bean
    public MessageChannel inputChannel() {
        return new DirectChannel();
    }

    @Bean
    public TcpOutboundGateway tcpOutboundGateway() {
        TcpOutboundGateway tcpOutboundGateway = new TcpOutboundGateway();
        tcpOutboundGateway.setConnectionFactory(tcpNetClientConnectionFactory());
        tcpOutboundGateway.setRequestTimeout(10_000);
        tcpOutboundGateway.setRemoteTimeout(10_000);
        tcpOutboundGateway.setReplyChannel(clientBytes2StringChannel());

        return tcpOutboundGateway;
    }

    @Bean
    public MessageChannel clientBytes2StringChannel() {
        return new DirectChannel();
    }

    @Bean
    @ServiceActivator(inputChannel = "clientBytes2StringChannel", outputChannel="clientOutputChannel")
    public ObjectToStringTransformer objectToStringTransformerClient() {
        return new ObjectToStringTransformer();
    }

    @Bean
    public MessageChannel clientOutputChannel() {
        return new DirectChannel();
    }

    // SERVER
    @Bean
    public TcpNetServerConnectionFactory tcpNetServerConnectionFactory() {
        return new TcpNetServerConnectionFactory(8080);
    }

    @Bean
    public TcpInboundGateway tcpInboundGateway() {
        TcpInboundGateway tcpInboundGateway = new TcpInboundGateway();
        tcpInboundGateway.setConnectionFactory(tcpNetServerConnectionFactory());
        tcpInboundGateway.setRequestChannel(serverBytes2StringChannel());
        tcpInboundGateway.setErrorChannel(errorChannel());

        return tcpInboundGateway;
    }

    @Bean
    public MessageChannel serverBytes2StringChannel() {
        return new DirectChannel();
    }

    @Bean
    @ServiceActivator(inputChannel = "serverBytes2StringChannel", outputChannel = "toSA")
    public ObjectToStringTransformer objectToStringTransformerServer() {
        return new ObjectToStringTransformer();
    }

    @Bean
    public MessageChannel toSA() {
        return new DirectChannel();
    }

    @Bean
    @ServiceActivator(inputChannel = "toSA")
    public EchoService echoService() {
        return new EchoService();
    }

    @Bean
    public MessageChannel errorChannel() {
        return new DirectChannel();
    }

    @Bean
    @ServiceActivator(inputChannel = "errorChannel")
    public Transformer transformer() {
        ExpressionParser p = new SpelExpressionParser(new SpelParserConfiguration(true, true));

        return new ExpressionEvaluatingTransformer(p.parseExpression("payload.failedMessage.payload + ':' + payload.cause.message"));
    }

}

-

@MessagingGateway(name = "gw", defaultRequestChannel = "inputChannel", defaultReplyChannel = "clientOutputChannel")
public interface SimpleGateway {
    String send(String text);
}

-

public class EchoService {
    public String test(String input) {
        if ("FAIL".equals(input)) {
            throw new RuntimeException("Failure Demonstration");
        }
        return "echo:" + input;
    }
}

-

// Just running this gives me the exception.
public class TcpApp {
    public static void main(String[] args) {
        new AnnotationConfigApplicationContext(TcpConfig.class);
    }
}

-

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tcpConfig': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Found ambiguous parameter type [class java.lang.Void] for method match: [public void org.springframework.integration.context.IntegrationObjectSupport.setMessageBuilderFactory(org.springframework.integration.support.MessageBuilderFactory), public void org.springframework.integration.context.IntegrationObjectSupport.setBeanFactory(org.springframework.beans.factory.BeanFactory), public void org.springframework.integration.context.IntegrationObjectSupport.setComponentName(java.lang.String), public final void org.springframework.integration.context.IntegrationObjectSupport.setPrimaryExpression(org.springframework.expression.Expression), public void org.springframework.integration.context.IntegrationObjectSupport.setChannelResolver(org.springframework.messaging.core.DestinationResolver), public java.lang.String org.springframework.integration.context.IntegrationObjectSupport.getApplicationContextId(), public void org.springframework.integration.context.IntegrationObjectSupport.setApplicationContext(org.springframework.context.ApplicationContext) throws org.springframework.beans.BeansException]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
    at com.connectplaza.prototype.tcp.TcpApp.main(TcpApp.java:8)
Caused by: java.lang.IllegalArgumentException: Found ambiguous parameter type [class java.lang.Void] for method match: [public void org.springframework.integration.context.IntegrationObjectSupport.setMessageBuilderFactory(org.springframework.integration.support.MessageBuilderFactory), public void org.springframework.integration.context.IntegrationObjectSupport.setBeanFactory(org.springframework.beans.factory.BeanFactory), public void org.springframework.integration.context.IntegrationObjectSupport.setComponentName(java.lang.String), public final void org.springframework.integration.context.IntegrationObjectSupport.setPrimaryExpression(org.springframework.expression.Expression), public void org.springframework.integration.context.IntegrationObjectSupport.setChannelResolver(org.springframework.messaging.core.DestinationResolver), public java.lang.String org.springframework.integration.context.IntegrationObjectSupport.getApplicationContextId(), public void org.springframework.integration.context.IntegrationObjectSupport.setApplicationContext(org.springframework.context.ApplicationContext) throws org.springframework.beans.BeansException]
    at org.springframework.util.Assert.isNull(Assert.java:113)
    at org.springframework.integration.util.MessagingMethodInvokerHelper.findHandlerMethodsForTarget(MessagingMethodInvokerHelper.java:499)
    at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:226)
    at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:149)
    at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:144)
    at org.springframework.integration.handler.MethodInvokingMessageProcessor.<init>(MethodInvokingMessageProcessor.java:60)
    at org.springframework.integration.handler.ServiceActivatingHandler.<init>(ServiceActivatingHandler.java:37)
    at org.springframework.integration.config.annotation.ServiceActivatorAnnotationPostProcessor.createHandler(ServiceActivatorAnnotationPostProcessor.java:66)
    at org.springframework.integration.config.annotation.AbstractMethodAnnotationPostProcessor.postProcess(AbstractMethodAnnotationPostProcessor.java:144)
    at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.processAnnotationTypeOnMethod(MessagingAnnotationPostProcessor.java:228)
    at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor$1.doWith(MessagingAnnotationPostProcessor.java:200)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:530)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:537)
    at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.postProcessAfterInitialization(MessagingAnnotationPostProcessor.java:180)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:421)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1635)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    ... 10 more

1 Ответ

0 голосов
/ 24 октября 2018

Каждый раз, когда вы используете Transformer для обработки сообщений, вам необходимо переключиться на аннотацию @Transformer.В противном случае @ServiceActivator не может определить, какой метод вызывать во время выполнения: https://docs.spring.io/spring-integration/reference/html/configuration.html#annotations_on_beans. Например:

@Bean
@Transformer(inputChannel = "clientBytes2StringChannel", outputChannel="clientOutputChannel")
public ObjectToStringTransformer objectToStringTransformerClient() {
    return new ObjectToStringTransformer();
}
...