Announcement Announcement Module
Collapse
No announcement yet.
Create MessageHandller with RouterFactoryBean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Create MessageHandller with RouterFactoryBean

    Hello,

    I am trying to create a MessageHandler that is configured for a router bean. I am going about this using the RouterFactoryBean.getObject() method. However, I keep getting null values instead of a MessageHandler. Please refer to my code pasted below:

    @Bean
    MessageHandler computerResponseRouterMessageHandler() {

    RouterFactoryBean routerFactory = new RouterFactoryBean();
    routerFactory.setTargetObject(computerResponseRout er);
    routerFactory.setTargetMethodName("route");
    routerFactory.setDefaultOutputChannel(dossierInput Channel);

    // NECCESSARY/POSSIBLE: ???
    // routerFactory.setChannelMappings(null);
    // routerFactory.setDefaultOutputChannel(null);

    try {
    return routerFactory.getObject();
    }
    catch (Exception e) {
    // TODO Always returning NULL... what to do?
    e.printStackTrace();
    return null;
    }
    }


    Thanks in advance for any help!

  • #2
    Try calling routerFactory.afterPropertiesSet() before getObject().

    Can you explain why you are using the FactoryBean programmatically? Typically it's used internally by the code generated after parsing configuration or when used FactoryBeans are themselves configured within the context so that the lifecycle is managed (including afterPropertiesSet() being invoked upon initialization).

    Comment


    • #3
      Hi Mark,

      RouterFactoryBean does not seem to have the "afterPropertiesSet" method. Casting to AbstractFactoryBean does not work.

      I am doing this because I need to declare a PollingConsumer explicitly using Java, rather than XML. The PollingConsumer constructor takes a pollable input channel, and a message handler as its parameters. I am doing this so that I can use a control-bus to start and stop the pollers, as well as set the poll period using a web API. I have successfully managed to implement this with endpoints that are service activators, but have failed with a router. (See code below).

      I should mentioned that I have since modified the code from the original question. The problem now is that messages are not going through. I suspect that it is because I have not assigned a channel mapping, but my routing is not based solely on header values, but also other variables.

      NOT WORKING

      @Bean
      MessageHandler computerResponseRouterMessageHandler() {
      PayloadTypeRouter payloadTypeRouter = new PayloadTypeRouter();
      payloadTypeRouter.setDefaultOutputChannel(computer ResponseChannel);
      payloadTypeRouter.afterPropertiesSet();
      return payloadTypeRouter;
      }

      @Bean
      PollingConsumer computerResponseRouterPoller() {
      PollingConsumer pollingConsumer = new PollingConsumer(computerResponseChannel, computerResponseRouterMessageHandler());
      pollingConsumer.setMaxMessagesPerPoll(pollMaxMsg);
      pollingConsumer.setReceiveTimeout(pollReceiveTimeo ut);
      pollingConsumer.setTrigger(computerResponseRouterP eriodicTrigger());
      return pollingConsumer;
      }
      }



      With the service activators, everything works using the code below:



      @Bean
      MessageHandler dossierServiceActivatorHandler() {
      ServiceActivatingHandler serviceActivatingHandler = new ServiceActivatingHandler(dossierServiceActivator);
      serviceActivatingHandler.setOutputChannel(logFullM essageChannel);
      return serviceActivatingHandler;
      }

      @Bean
      PollingConsumer dossierServiceActivatorPoller() {
      PollingConsumer pollingConsumer = new PollingConsumer(dossierInputChannel, dossierServiceActivatorHandler());
      pollingConsumer.setMaxMessagesPerPoll(pollMaxMsg);
      pollingConsumer.setReceiveTimeout(pollReceiveTimeo ut);
      pollingConsumer.setTrigger(dossierServiceActivator PeriodicTrigger());
      return pollingConsumer;
      }

      Comment


      • #4
        I got it working. SOLUTION:

        @Bean
        MessageHandler computerResponseRouterMessageHandler() {
        RouterFactoryBean r = new RouterFactoryBean();
        MethodInvokingRouter methodInvokingRouter = new MethodInvokingRouter(computerResponseRouter, "route");
        return methodInvokingRouter;
        }

        @Bean
        PollingConsumer computerResponseRouterPoller() {
        PollingConsumer pollingConsumer = new PollingConsumer(computerResponseChannel, computerResponseRouterMessageHandler());
        pollingConsumer.setMaxMessagesPerPoll(pollMaxMsg);
        pollingConsumer.setReceiveTimeout(pollReceiveTimeo ut);
        pollingConsumer.setTrigger(computerResponseRouterP eriodicTrigger());
        return pollingConsumer;
        }

        Comment

        Working...
        X