Spring Boot和FlywayTest导致JPA Camel路由在数据库重置期间抛出异常

时间:2015-02-05 07:13:58

标签: jpa testing apache-camel spring-boot flyway

我有一个Spring Boot应用程序,其中包含一个带有JPA使用者的Camel路由。

当运行使用@FlyTest批注的测试时,数据库会在测试之前按预期重置,但是当发生这种情况时,Camel JPA使用者会尝试对数据库执行SQL选择。

如何在FlywayTest重置数据库时禁用路由?

任何建议都表示赞赏。

4 个答案:

答案 0 :(得分:1)

我通过设置consumer.initialDelay = 5000来解决这个问题,这足以让Flyway重置数据库。

答案 1 :(得分:0)

解决此问题的更有力的方法是使用以下方法暂停我的测试中的Camel路由:

@Autowired
CamelContext camelContext;

@Before
public void init() throws Exception {
    camelContext.suspend();
}

答案 2 :(得分:0)

在Camel 2.15(当前主分支)中,我最近推迟了CamelContext启动。这条路线不应该这么早开始。因此,路线不应该过早地从JPA中消耗。

如果您仍然遇到此问题 - 请随时给我写信。我会解决它。

答案 3 :(得分:0)

我将我的应用程序配置为在Flyway迁移完成后启动所有Camel路由。事实证明,这对于迁移速度非常慢的慢速机器或大型数据库非常重要。

@Configuration
@EnableTransactionManagement
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) throws Exception {
        new SpringApplication((Object[])args).run();
    }

    @Bean 
    @DependsOn({"flyway","dataSource"})
    CamelContext camelContext(ApplicationContext applicationContext,
                              CamelConfigurationProperties configurationProperties) {
        CamelContext camelContext = new SpringCamelContext(applicationContext);
        SpringCamelContext.setNoStart(true);

        if (!configurationProperties.isJmxEnabled()) {
            camelContext.disableJMX();
        }

        if (configurationProperties.getName() != null) {
            ((SpringCamelContext) camelContext).setName(configurationProperties.getName());
        }

        camelContext.setAutoStartup(false);

        return camelContext;
    }

    @Bean 
    FlywayCallback flywayCallback(Flyway flyway, final CamelContext camelContext) {
        FlywayCallback callback = new FlywayCallback() {

            @Override
            public void beforeClean(Connection connection) {}
            @Override
            public void afterClean(Connection connection) {}
            @Override
            public void beforeMigrate(Connection connection) {}
            @Override
            public void afterMigrate(Connection connection) {
                try {
                    camelContext.startAllRoutes();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException("Camel startup failed", e);
                }
            }
            @Override
            public void beforeEachMigrate(Connection connection,
                    MigrationInfo info) {}
            @Override
            public void afterEachMigrate(Connection connection,
                    MigrationInfo info) {}
            @Override
            public void beforeValidate(Connection connection) {}
            @Override
            public void afterValidate(Connection connection) {}
            @Override
            public void beforeBaseline(Connection connection) {}
            @Override
            public void afterBaseline(Connection connection) {}
            @Override
            public void beforeInit(Connection connection) {}
            @Override
            public void afterInit(Connection connection) {}
            @Override
            public void beforeRepair(Connection connection) {}
            @Override
            public void afterRepair(Connection connection) {}
            @Override
            public void beforeInfo(Connection connection) {}
            @Override
            public void afterInfo(Connection connection) {}
        };
        flyway.setCallbacks(callback);
        return callback;
    }

}