Search in sources :

Example 41 with ClassPathXmlApplicationContext

use of org.springframework.context.support.ClassPathXmlApplicationContext in project camel by apache.

the class SpringQuartzPersistentStoreRestartAppChangeOptionsTest method testRestartAppChangeTriggerType.

@Test
public void testRestartAppChangeTriggerType() throws Exception {
    // Test creates application context twice with different simple trigger options in configuration xml.
    // Both times it retrieves back the option, accessing it via trigger (so, using value stored in DB).
    // After that it asserts that two options are not equal.
    // load spring app
    app = new ClassPathXmlApplicationContext("org/apache/camel/component/quartz/SpringQuartzPersistentStoreRestartAppChangeCronExpressionTest1.xml");
    app.start();
    CamelContext camel = app.getBean("camelContext", CamelContext.class);
    assertNotNull(camel);
    assertTrue(getTrigger(camel, "quartzRoute") instanceof CronTrigger);
    app.stop();
    log.info("Restarting ...");
    log.info("Restarting ...");
    log.info("Restarting ...");
    // load spring app
    AbstractXmlApplicationContext app2 = new ClassPathXmlApplicationContext("org/apache/camel/component/quartz/SpringQuartzPersistentStoreRestartAppChangeOptionsTest2.xml");
    app2.start();
    CamelContext camel2 = app2.getBean("camelContext", CamelContext.class);
    assertNotNull(camel2);
    assertTrue(getTrigger(camel2, "quartzRoute") instanceof SimpleTrigger);
    app2.stop();
    // we're done so let's properly close the application contexts, but close
    // the second app before the first one so that the quartz scheduler running
    // inside it can be properly shutdown
    IOHelper.close(app2, app);
}
Also used : CamelContext(org.apache.camel.CamelContext) CronTrigger(org.quartz.CronTrigger) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) AbstractXmlApplicationContext(org.springframework.context.support.AbstractXmlApplicationContext) SimpleTrigger(org.quartz.SimpleTrigger) Test(org.junit.Test)

Example 42 with ClassPathXmlApplicationContext

use of org.springframework.context.support.ClassPathXmlApplicationContext in project camel by apache.

the class SpringQuartzPersistentStoreRestartAppChangeOptionsTest method testRestartAppChangeCronExpression.

@Test
public void testRestartAppChangeCronExpression() throws Exception {
    // Test creates application context twice with different cron expressions in configuration xml.
    // Both times it retrieves back the cron expression, accessing it via trigger (so, using value stored in DB).
    // After that it asserts that two cron expressions are not equal.
    // load spring app
    app = new ClassPathXmlApplicationContext("org/apache/camel/component/quartz/SpringQuartzPersistentStoreRestartAppChangeCronExpressionTest1.xml");
    app.start();
    CamelContext camel = app.getBean("camelContext", CamelContext.class);
    assertNotNull(camel);
    String cronExpression = ((CronTrigger) getTrigger(camel, "quartzRoute")).getCronExpression();
    app.stop();
    log.info("Restarting ...");
    log.info("Restarting ...");
    log.info("Restarting ...");
    // load spring app
    app2 = new ClassPathXmlApplicationContext("org/apache/camel/component/quartz/SpringQuartzPersistentStoreRestartAppChangeCronExpressionTest2.xml");
    app2.start();
    CamelContext camel2 = app2.getBean("camelContext", CamelContext.class);
    assertNotNull(camel2);
    String cronExpression2 = ((CronTrigger) getTrigger(camel2, "quartzRoute")).getCronExpression();
    app2.stop();
    assertNotEquals(cronExpression, cronExpression2);
    app3 = new ClassPathXmlApplicationContext("org/apache/camel/component/quartz/SpringQuartzPersistentStoreRestartAppChangeCronExpressionTest3.xml");
    app3.start();
    CamelContext camel3 = app3.getBean("camelContext3", CamelContext.class);
    assertNotNull(camel3);
    String cronExpression3 = ((CronTrigger) getTrigger(camel3, "quartzRoute")).getCronExpression();
    app3.stop();
    assertEquals(cronExpression2, cronExpression3);
}
Also used : CamelContext(org.apache.camel.CamelContext) CronTrigger(org.quartz.CronTrigger) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Test(org.junit.Test)

Example 43 with ClassPathXmlApplicationContext

use of org.springframework.context.support.ClassPathXmlApplicationContext in project camel by apache.

the class SpringQuartzConsumerTwoAppsClusteredFailoverTest method testQuartzPersistentStoreClusteredApp.

@Test
public void testQuartzPersistentStoreClusteredApp() throws Exception {
    // boot up the database the two apps are going to share inside a clustered quartz setup
    AbstractXmlApplicationContext db = new ClassPathXmlApplicationContext("org/apache/camel/component/quartz2/SpringQuartzConsumerClusteredAppDatabase.xml");
    db.start();
    // now launch the first clustered app which will acquire the quartz database lock and become the master
    AbstractXmlApplicationContext app = new ClassPathXmlApplicationContext("org/apache/camel/component/quartz2/SpringQuartzConsumerClusteredAppOne.xml");
    app.start();
    // as well as the second one which will run in slave mode as it will not be able to acquire the same lock
    AbstractXmlApplicationContext app2 = new ClassPathXmlApplicationContext("org/apache/camel/component/quartz2/SpringQuartzConsumerClusteredAppTwo.xml");
    app2.start();
    CamelContext camel = app.getBean("camelContext", CamelContext.class);
    MockEndpoint mock = camel.getEndpoint("mock:result", MockEndpoint.class);
    mock.expectedMinimumMessageCount(3);
    mock.expectedMessagesMatches(new ClusteringPredicate(true));
    // let the route run a bit...
    Thread.sleep(5000);
    mock.assertIsSatisfied();
    // now let's simulate a crash of the first app (the quartz instance 'app-one')
    log.warn("The first app is going to crash NOW!");
    IOHelper.close(app);
    log.warn("Crashed...");
    log.warn("Crashed...");
    log.warn("Crashed...");
    // wait long enough until the second app takes it over...
    Thread.sleep(20000);
    // inside the logs one can then clearly see how the route of the second app ('app-two') starts consuming:
    // 2013-09-30 11:22:20,349 [main           ] WARN  erTwoAppsClusteredFailoverTest - Crashed...
    // 2013-09-30 11:22:20,349 [main           ] WARN  erTwoAppsClusteredFailoverTest - Crashed...
    // 2013-09-30 11:22:20,349 [main           ] WARN  erTwoAppsClusteredFailoverTest - Crashed...
    // 2013-09-30 11:22:35,340 [_ClusterManager] INFO  LocalDataSourceJobStore        - ClusterManager: detected 1 failed or restarted instances.
    // 2013-09-30 11:22:35,340 [_ClusterManager] INFO  LocalDataSourceJobStore        - ClusterManager: Scanning for instance "app-one"'s failed in-progress jobs.
    // 2013-09-30 11:22:35,369 [eduler_Worker-1] INFO  triggered                      - Exchange[ExchangePattern: InOnly, BodyType: String, Body: clustering PONGS!]
    CamelContext camel2 = app2.getBean("camelContext2", CamelContext.class);
    MockEndpoint mock2 = camel2.getEndpoint("mock:result", MockEndpoint.class);
    mock2.expectedMinimumMessageCount(3);
    mock2.expectedMessagesMatches(new ClusteringPredicate(false));
    mock2.assertIsSatisfied();
    // and as the last step shutdown the second app as well as the database
    IOHelper.close(app2, db);
}
Also used : CamelContext(org.apache.camel.CamelContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) AbstractXmlApplicationContext(org.springframework.context.support.AbstractXmlApplicationContext) Test(org.junit.Test)

Example 44 with ClassPathXmlApplicationContext

use of org.springframework.context.support.ClassPathXmlApplicationContext in project camel by apache.

the class SpringQuartzPersistentStoreRestartAppChangeOptionsTest method testRestartAppChangeCronExpression.

@Test
public void testRestartAppChangeCronExpression() throws Exception {
    // Test creates application context twice with different cron expressions in configuration xml.
    // Both times it retrieves back the cron expression, accessing it via trigger (so, using value stored in DB).
    // After that it asserts that two cron expressions are not equal.
    // load spring app
    app = new ClassPathXmlApplicationContext("org/apache/camel/component/quartz2/SpringQuartzPersistentStoreRestartAppChangeCronExpressionTest1.xml");
    app.start();
    CamelContext camel = app.getBean("camelContext", CamelContext.class);
    assertNotNull(camel);
    String cronExpression = ((CronTrigger) getTrigger(camel, "quartzRoute")).getCronExpression();
    app.stop();
    log.info("Restarting ...");
    log.info("Restarting ...");
    log.info("Restarting ...");
    // load spring app
    app2 = new ClassPathXmlApplicationContext("org/apache/camel/component/quartz2/SpringQuartzPersistentStoreRestartAppChangeCronExpressionTest2.xml");
    app2.start();
    CamelContext camel2 = app2.getBean("camelContext", CamelContext.class);
    assertNotNull(camel2);
    String cronExpression2 = ((CronTrigger) getTrigger(camel2, "quartzRoute")).getCronExpression();
    app2.stop();
    assertNotEquals(cronExpression, cronExpression2);
    // load spring app
    app3 = new ClassPathXmlApplicationContext("org/apache/camel/component/quartz2/SpringQuartzPersistentStoreRestartAppChangeCronExpressionTest3.xml");
    app3.start();
    CamelContext camel3 = app3.getBean("camelContext3", CamelContext.class);
    assertNotNull(camel3);
    String cronExpression3 = ((CronTrigger) getTrigger(camel3, "quartzRoute")).getCronExpression();
    app3.stop();
    assertEquals(cronExpression2, cronExpression3);
}
Also used : CamelContext(org.apache.camel.CamelContext) CronTrigger(org.quartz.CronTrigger) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Test(org.junit.Test)

Example 45 with ClassPathXmlApplicationContext

use of org.springframework.context.support.ClassPathXmlApplicationContext in project camel by apache.

the class SpringQuartzTwoAppsClusteredFailoverTest method testQuartzPersistentStoreClusteredApp.

@Test
public void testQuartzPersistentStoreClusteredApp() throws Exception {
    // boot up the database the two apps are going to share inside a clustered quartz setup
    AbstractXmlApplicationContext db = new ClassPathXmlApplicationContext("org/apache/camel/routepolicy/quartz/SpringQuartzClusteredAppDatabase.xml");
    db.start();
    // now launch the first clustered app which will acquire the quartz database lock and become the master
    AbstractXmlApplicationContext app = new ClassPathXmlApplicationContext("org/apache/camel/routepolicy/quartz/SpringQuartzClusteredAppOne.xml");
    app.start();
    // as well as the second one which will run in slave mode as it will not be able to acquire the same lock
    AbstractXmlApplicationContext app2 = new ClassPathXmlApplicationContext("org/apache/camel/routepolicy/quartz/SpringQuartzClusteredAppTwo.xml");
    app2.start();
    CamelContext camel = app.getBean("camelContext", CamelContext.class);
    MockEndpoint mock = camel.getEndpoint("mock:result", MockEndpoint.class);
    mock.expectedMessageCount(1);
    mock.expectedBodiesReceived("clustering PINGS!");
    // wait a bit to make sure the route has already been properly started through the given route policy
    Thread.sleep(5000);
    app.getBean("template", ProducerTemplate.class).sendBody("direct:start", "clustering");
    mock.assertIsSatisfied();
    // now let's simulate a crash of the first app (the quartz instance 'app-one')
    log.warn("The first app is going to crash NOW!");
    IOHelper.close(app);
    log.warn("Crashed...");
    log.warn("Crashed...");
    log.warn("Crashed...");
    // wait long enough until the second app takes it over...
    Thread.sleep(20000);
    // inside the logs one can then clearly see how the route of the second app ('app-two') gets started:
    // 2013-09-24 22:51:34,215 [main           ] WARN  ersistentStoreClusteredAppTest - Crashed...
    // 2013-09-24 22:51:34,215 [main           ] WARN  ersistentStoreClusteredAppTest - Crashed...
    // 2013-09-24 22:51:34,215 [main           ] WARN  ersistentStoreClusteredAppTest - Crashed...
    // 2013-09-24 22:51:49,188 [_ClusterManager] INFO  LocalDataSourceJobStore        - ClusterManager: detected 1 failed or restarted instances.
    // 2013-09-24 22:51:49,188 [_ClusterManager] INFO  LocalDataSourceJobStore        - ClusterManager: Scanning for instance "app-one"'s failed in-progress jobs.
    // 2013-09-24 22:51:49,211 [eduler_Worker-1] INFO  SpringCamelContext             - Route: myRoute started and consuming from: Endpoint[direct://start]
    CamelContext camel2 = app2.getBean("camelContext2", CamelContext.class);
    MockEndpoint mock2 = camel2.getEndpoint("mock:result", MockEndpoint.class);
    mock2.expectedMessageCount(1);
    mock2.expectedBodiesReceived("clustering PONGS!");
    app2.getBean("template", ProducerTemplate.class).sendBody("direct:start", "clustering");
    mock2.assertIsSatisfied();
    // and as the last step shutdown the second app as well as the database
    IOHelper.close(app2, db);
}
Also used : CamelContext(org.apache.camel.CamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) AbstractXmlApplicationContext(org.springframework.context.support.AbstractXmlApplicationContext) Test(org.junit.Test)

Aggregations

ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)561 Test (org.junit.Test)265 ApplicationContext (org.springframework.context.ApplicationContext)167 Before (org.junit.Before)53 ConfigurableMessenger (org.springframework.scripting.ConfigurableMessenger)33 ITestBean (org.springframework.tests.sample.beans.ITestBean)30 Messenger (org.springframework.scripting.Messenger)29 CamelContext (org.apache.camel.CamelContext)25 Refreshable (org.springframework.aop.target.dynamic.Refreshable)23 AbstractXmlApplicationContext (org.springframework.context.support.AbstractXmlApplicationContext)22 DataSource (javax.sql.DataSource)18 SpringCamelContext (org.apache.camel.spring.SpringCamelContext)16 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)16 Bus (org.apache.cxf.Bus)14 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)14 ArrayList (java.util.ArrayList)13 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)12 DemoService (com.alibaba.dubbo.config.spring.api.DemoService)11 IOException (java.io.IOException)10 ProducerTemplate (org.apache.camel.ProducerTemplate)10