/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package camelinaction; import javax.sql.DataSource; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.test.junit4.CamelSpringTestSupport; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.support.AbstractXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; /** * @version $Revision: 332 $ */ public class AtomikosXARollbackAfterDbTest extends CamelSpringTestSupport { private JdbcTemplate jdbc; @Before public void setupDatabase() throws Exception { DataSource ds = context.getRegistry().lookup("myDataSource", DataSource.class); jdbc = new JdbcTemplate(ds); jdbc.execute("create table partner_metric " + "( partner_id varchar(10), time_occurred varchar(20), status_code varchar(3), perf_time varchar(10) )"); } @After public void dropDatabase() throws Exception { jdbc.execute("drop table partner_metric"); } @Override protected AbstractXmlApplicationContext createApplicationContext() { return new ClassPathXmlApplicationContext("spring-context.xml"); } @Override protected int getExpectedRouteCount() { // use 0 as we use a Java based route builder directly in this unit test return 0; } @Test public void testXaRollbackAfterDb() throws Exception { // there should be 0 row in the database when we start assertEquals(0, jdbc.queryForInt("select count(*) from partner_metric")); String xml = "<?xml version=\"1.0\"?><partner id=\"123\"><date>200911150815</date><code>200</code><time>4387</time></partner>"; template.sendBody("activemq:queue:partners", xml); // wait for the route to complete with failure Thread.sleep(15000); // data not inserted so there should be 0 rows assertEquals(0, jdbc.queryForInt("select count(*) from partner_metric")); // should be in DLQ // now check that the message is on the queue by consuming it again String dlq = consumer.receiveBodyNoWait("activemq:queue:ActiveMQ.DLQ", String.class); assertNotNull("Should not lose message", dlq); } @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("activemq:queue:partners") .transacted() .log("*** before database ***") .bean(PartnerServiceBean.class, "toSql") .to("jdbc:myDataSource") .log("*** after database ***") .throwException(new IllegalArgumentException("Forced failure after DB")); } }; } }