Announcement Announcement Module
Collapse
No announcement yet.
@Router(inputChannel="") VS <router ref="Router" input-channel=""/> Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Router(inputChannel="") VS <router ref="Router" input-channel=""/>

    My code stopped working after replacing:

    <router ref="myRouter" input-channel="myInputChannel" />

    in my XML file with the POJO:

    public class MyRouter {

    @Router(inputChannel = "myInputChannel")
    public String route(Message<String> message) {
    return foo;
    }
    }


    The Java.config file was not altered.

    @Bean
    MyRouter myRouter() {
    return new MyRouter();
    }


    Any ideas on why this is happening?

    Thanks,
    K

  • #2
    Did you add <int:annotation-config/> to the xml?

    That tells SI to look for @Router annotations etc.

    Comment


    • #3
      Yes. Here's what it looks like after I make the changes:

      <beans:beans xmlns="http://www.springframework.org/schema/integration"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:beans="http://www.springframework.org/schema/beans"
      xmlns:task="http://www.springframework.org/schema/task"
      xmlns:int="http://www.springframework.org/schema/integration"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schem...-beans-3.2.xsd
      http://www.springframework.org/schema/integration
      http://www.springframework.org/schem...ration-2.2.xsd
      http://www.springframework.org/schema/task
      http://www.springframework.org/schema/task/spring-task-3.2.xsd">

      <annotation-config/>

      <task:executor id="bestRequestExecutor" pool-size="1" queue-capacity="0" keep-alive="0" rejection-policy="DISCARD" />

      <service-activator input-channel="computerErrorChannel" output-channel="errorChannel" ref="errorService" method="addError" />

      </beans:beans>



      This is the before (working) xml file. My goal is to remove all references to the router in my xml file.

      <beans:beans xmlns="http://www.springframework.org/schema/integration"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:beans="http://www.springframework.org/schema/beans"
      xmlns:task="http://www.springframework.org/schema/task"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schem...-beans-3.2.xsd
      http://www.springframework.org/schema/integration
      http://www.springframework.org/schem...ration-2.2.xsd
      http://www.springframework.org/schema/task
      http://www.springframework.org/schema/task/spring-task-3.2.xsd">

      <router ref="computerRequestRouter" input-channel="computerInputChannel" />

      <task:executor id="bestRequestExecutor" pool-size="1" queue-capacity="0" keep-alive="0" rejection-policy="DISCARD" />

      <service-activator input-channel="computerErrorChannel" output-channel="errorChannel" ref="errorService" method="addError" />

      </beans:beans>
      Last edited by kashiB; May 30th, 2013, 03:46 PM.

      Comment


      • #4
        The bean post processor can only find annotations on beans in the context; it doesn't look at all classes on the classpath. So you either need to add a <bean/> for MyRouter or you need to annotate it's class as an @Component and use <context:component-scan/> to add it as a bean, so the post processor can post process it.

        Comment


        • #5
          I've tried that and still no dice. I'm going to include my code below. Please let me know if you spot my error.

          ComputerRequestRouter.java
          Code:
          package com.otpp.dossier.computer.endpoint;
          
          import org.springframework.integration.Message;
          import org.springframework.integration.annotation.MessageEndpoint;
          import org.springframework.integration.annotation.Router;
          
          @MessageEndpoint
          public class ComputerRequestRouter {
          
              @Router(inputChannel = "computerInputChannel")
              public String route(Message<String> message) {
                  return "defaultPenCalcInputChannel";
              }
          
          }
          ComputerConfig.Java
          Code:
          //imports/
          
          @Configuration
          @Import({ComputerDevConfig.class})
          @ImportResource("classpath:/META-INF/spring/computer-integration-context.xml")
          public class ComputerConfig {
          
              @Bean
              ComputerRequestRouter computerRequestRouter() {
                  return new ComputerRequestRouter();
              }
          computer-integration-context.xml
          Code:
          <beans:beans xmlns="http://www.springframework.org/schema/integration" 
          	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          	xmlns:beans="http://www.springframework.org/schema/beans"
          	xmlns:task="http://www.springframework.org/schema/task"
          	xmlns:int="http://www.springframework.org/schema/integration"
          	xmlns:context="http://www.springframework.org/schema/context"
          	xsi:schemaLocation="
                  http://www.springframework.org/schema/beans     
                  http://www.springframework.org/schem...-beans-3.2.xsd
                  http://www.springframework.org/schema/integration
          		http://www.springframework.org/schem...ration-2.2.xsd
          		http://www.springframework.org/schema/task
          		http://www.springframework.org/schem...g-task-3.2.xsd
          		http://www.springframework.org/schema/context
                  http://www.springframework.org/schema/context/spring-context-3.0.xsd">
          
          <!--  	<router ref="computerRequestRouter" input-channel="computerInputChannel" />  --> How it was configured prior
          
          	<int:annotation-config/>
          	<context:annotation-config/>
          	
          	<context:component-scan base-package="com.computer.endpoint" />
          	
           />
          		
          </beans:beans>
          Last edited by Gary Russell; May 31st, 2013, 11:47 AM.

          Comment


          • #6
            When posting code and config, please use [ code ] ... [ /code ] tags (no spaces in brackets). I edited your post.

            I can't reproduce your issue; this works fine for me...

            Code:
            package foo;
            
            ...
            
            @MessageEndpoint
            public class Foo {
            
            	@Router(inputChannel="input")
            	public String route(Message<?> message) {
            		return "toTCP";
            	}
            }
            
            ///////////////////////////////////////////
            
            package bar;
            
            ...
            import foo.Foo;
            ...
            @Configuration
            public class Bar {
            
            	@Bean(name="foo")
            	Foo foo() {
            		return new Foo();
            	}
            
            }
            Code:
            	<context:component-scan base-package="bar" />
            
            	<int:annotation-config/>
            
            	<int:channel id="input" />
            
            	<int:channel id="toTCP" />

            Comment


            • #7
              Is it necessary to include:

              Code:
               <int:channel id="input" />
              ?

              I forgot to mention, but I have my channel declared as follows in the ComputerConfig file:

              Code:
                   @Bean
                  WireTap computerInputChannelWireTap() {
                      return new WireTap(logHeaderChannel);
                  }
              
                  @Bean
                  public MessageChannel computerInputChannel() {
                      DirectChannel computerInputChannel = new DirectChannel();
                      computerInputChannel.addInterceptor(computerInputChannelWireTap());
                      return computerInputChannel;
                  }

              Comment


              • #8
                No; that works for me too...

                Code:
                @Configuration
                public class Bar {
                
                	@Bean(name="foo")
                	Foo foo() {
                		return new Foo();
                	}
                
                	@Bean
                	MessageChannel input() {
                		return new DirectChannel();
                	}
                }
                Can you describe more what you mean by "stopped working"?

                Does the context fail to initialize? Or is it a runtime problem?

                In either case turn on DEBUG logging to see the bean creation/initiation logs.

                Or, follow a message through your flow to see what's happening.

                Comment


                • #9
                  The context initializes, but the router isn't connected to the input channel. For example, if I have components A, B, C and D. The router is point C. I sent a message at point A and it should end up at point D. Right now, it goes from A to B, leaves B, but never arrives at C.

                  I think I'm just going to go back to declaring the router in XML. It was working perfectly before. We just felt configuring things in Java would make things easier to maintain in the future.

                  Thanks again for all your help!

                  Comment


                  • #10
                    It has to be something simple.

                    Just compare a DEBUG log with the XML and without it.

                    Comment

                    Working...
                    X