1

I made a simple test and it dosen't work.

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = {SIConfiguration.class, ExceptionTypeRouter.class})
public class ExceptionTypeRouterTest {

    @Autowired
    @Qualifier("inDefaultChannel")
    private DirectChannel in;

    @Test
    public void fistRouteTest() {

        in.send(new GenericMessage<>("ready!"));

    }
}


@Configuration
@Slf4j
public class ExceptionTypeRouter {

    @Bean
    @Qualifier("inDefaultChannel")
    public DirectChannel inDefaultChannel() {
        return MessageChannels
                .direct()
                .get();
    }

    @Bean
    @Qualifier("directErrorChannel")
    public DirectChannel directErrorChannel() {
        return MessageChannels
                .direct()
                .get();
    }

    @Bean
    public ErrorMessageExceptionTypeRouter errorMessageExceptionTypeRouter() {
        ErrorMessageExceptionTypeRouter router = new ErrorMessageExceptionTypeRouter();
        router.setChannelMapping(IOException.class.getName(), "directErrorChannel");
        router.setChannelMapping(NullPointerException.class.getName(), "directErrorChannel");
        router.setDefaultOutputChannel(outDefaultChannel());

        return router;
    }
}    

The proplem is somwhere here. Caze this.getApplicationContext() is null.

 private Class<?> resolveClassFromName(String className) {
        try {
            return ClassUtils.forName(className, this.getApplicationContext().getClassLoader());
        } catch (ClassNotFoundException var3) {
            throw new IllegalStateException("Cannot load class for channel mapping.", var3);
        }
    }

Error log:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'errorMessageExceptionTypeRouter' defined in integration.exceptiondemo.configuration.router.ExceptionTypeRouter: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework. integration.router.ErrorMessageExceptionTypeRouter]: Factory method 'errorMessageExceptionTypeRouter' threw exception; nested exception is java.lang.NullPointerException

il_raffa
  • 5,090
  • 129
  • 31
  • 36
Andrey D.
  • 31
  • 4
  • 1
    That question and answer is fully not related to this issue. The problem is in the Framework and its hard for new developers to overcome such a problem if they don't cause it. Please, give a chance of the Framework developers to answer to questions related to their products. And if you will close all the question from these people you are going to suffer from bad reputation for SO in the future. Thanks for understanding. – Artem Bilan Apr 24 '18 at 13:38

1 Answers1

1

Agreed. That's bug in the ErrorMessageExceptionTypeRouter.

When you call setChannelMapping() it doesn't check for the this.initialized and try to call the mentioned resolveClassFromName() when we don't have ApplicationContext populated yet.

Please, raise a JIRA to fix it: https://jira.spring.io/projects/INT

Meanwhile as a workaround you should consider to use setChannelMappings(Map<String, String>) instead:

@Bean
public ErrorMessageExceptionTypeRouter errorMessageExceptionTypeRouter() {
    ErrorMessageExceptionTypeRouter router = new ErrorMessageExceptionTypeRouter();
    Map<String, String> mappings = new HashMap<>();
    mappings.put(IOException.class.getName(), "directErrorChannel");
    mappings.put(NullPointerException.class.getName(), "directErrorChannel");
    router.setChannelMappings(mappings);
    router.setDefaultOutputChannel(outDefaultChannel());

    return router;
}
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • The JIRA on the matter: https://jira.spring.io/browse/INT-4455. Why did you downvote my answer though ? – Artem Bilan Apr 24 '18 at 15:39
  • 1
    I didn't. It's auto. Because i have less then 15 reputation. B.w. i'm reading jira and pull request. Thx a lot! – Andrey D. Apr 24 '18 at 16:29