From 1b9b9621dd20b95a398e423b46c7b96ceb519f55 Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Mon, 7 Jul 2014 09:29:59 -0500 Subject: [PATCH] Change prefetch on jms tests to 0 and add @DirtiesContext to tests With the upgrade to ActiveMQ 5.9.0 as part of Spring IO, ActiveMQ's prefetch behavior changed. With each call to JmsTemplate#receiveAndConvert, the template creates a new consumer. In these tests, the first call created a consumer which prefetched all the test messages on the queue, leaving them unavailable for the second consumer the JmsTemplate created. By setting prefetch to 0, the messages are now available for the subsequent JmsTemplate#receiveAndConvert calls. This addresses BATCH-2248. Conflicts: spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/repeat/jms/SynchronousTests.java --- ...sageListenerContainerIntegrationTests.java | 30 +++++----- .../batch/repeat/jms/AsynchronousTests.java | 27 +++++---- .../batch/repeat/jms/SynchronousTests.java | 55 ++++++++++--------- .../org/springframework/batch/jms/init.sql | 2 + .../springframework/batch/jms/jms-context.xml | 19 ++++--- 5 files changed, 74 insertions(+), 59 deletions(-) diff --git a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/container/jms/BatchMessageListenerContainerIntegrationTests.java b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/container/jms/BatchMessageListenerContainerIntegrationTests.java index 9eddd947a..d9231a44f 100644 --- a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/container/jms/BatchMessageListenerContainerIntegrationTests.java +++ b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/container/jms/BatchMessageListenerContainerIntegrationTests.java @@ -15,11 +15,26 @@ */ package org.springframework.batch.container.jms; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.SortedSet; +import java.util.TreeSet; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.TextMessage; + import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.retry.RecoveryCallback; @@ -28,28 +43,17 @@ import org.springframework.retry.RetryContext; import org.springframework.retry.policy.NeverRetryPolicy; import org.springframework.retry.support.DefaultRetryState; import org.springframework.retry.support.RetryTemplate; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import javax.jms.JMSException; -import javax.jms.Message; -import javax.jms.MessageListener; -import javax.jms.TextMessage; -import java.util.SortedSet; -import java.util.TreeSet; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.TimeUnit; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - /** * @author Dave Syer * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/org/springframework/batch/jms/jms-context.xml") +@DirtiesContext public class BatchMessageListenerContainerIntegrationTests { @Autowired diff --git a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/repeat/jms/AsynchronousTests.java b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/repeat/jms/AsynchronousTests.java index 85dfc0b73..115bdc8ec 100644 --- a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/repeat/jms/AsynchronousTests.java +++ b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/repeat/jms/AsynchronousTests.java @@ -16,7 +16,9 @@ package org.springframework.batch.repeat.jms; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; @@ -27,22 +29,25 @@ import javax.jms.Session; import javax.jms.TextMessage; import javax.sql.DataSource; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + import org.springframework.batch.container.jms.BatchMessageListenerContainer; import org.springframework.batch.jms.ExternalRetryInBatchTests; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.listener.SessionAwareMessageListener; -import org.springframework.util.ClassUtils; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; -import org.springframework.beans.factory.annotation.Autowired; -import org.junit.runner.RunWith; -import org.junit.Before; -import org.junit.After; -import org.junit.Test; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.util.ClassUtils; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/org/springframework/batch/jms/jms-context.xml") +@DirtiesContext public class AsynchronousTests { protected String[] getConfigLocations() { @@ -117,7 +122,7 @@ public class AsynchronousTests { }); container.initializeProxy(); - + container.start(); // Need to sleep for at least a second here... @@ -139,7 +144,7 @@ public class AsynchronousTests { public void testRollback() throws Exception { assertInitialState(); - + // Prevent us from being overwhelmed after rollback container.setRecoveryInterval(500); @@ -155,7 +160,7 @@ public class AsynchronousTests { } } }); - + container.initializeProxy(); container.start(); diff --git a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/repeat/jms/SynchronousTests.java b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/repeat/jms/SynchronousTests.java index 26f80093f..250e98d6b 100644 --- a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/repeat/jms/SynchronousTests.java +++ b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/repeat/jms/SynchronousTests.java @@ -23,6 +23,8 @@ import java.util.ArrayList; import java.util.List; import javax.jms.ConnectionFactory; +import javax.jms.JMSException; +import javax.jms.Session; import javax.sql.DataSource; import org.junit.Test; @@ -37,7 +39,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jms.connection.SessionProxy; import org.springframework.jms.core.JmsTemplate; +import org.springframework.jms.core.SessionCallback; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.BeforeTransaction; @@ -48,6 +53,7 @@ import org.springframework.transaction.support.TransactionTemplate; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/org/springframework/batch/jms/jms-context.xml") +@DirtiesContext public class SynchronousTests implements ApplicationContextAware { @Autowired @@ -103,18 +109,18 @@ public class SynchronousTests implements ApplicationContextAware { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { String text = (String) jmsTemplate.receiveAndConvert("queue"); - System.out.println("text = " + text); list.add(text); jdbcTemplate.update("INSERT into T_BARS (id,name,foo_date) values (?,?,null)", list.size(), text); return RepeatStatus.continueIf(text != null); } }); - System.err.println(jdbcTemplate.queryForList("select * from T_BARS")); - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); assertEquals(2, count); + assertTrue(list.contains("foo")); + assertTrue(list.contains("bar")); + String text = (String) jmsTemplate.receiveAndConvert("queue"); assertEquals(null, text); @@ -123,6 +129,8 @@ public class SynchronousTests implements ApplicationContextAware { @Test public void testFullRollback() throws Exception { + onSetUpBeforeTransaction(); + assertInitialState(); new TransactionTemplate(transactionManager).execute(new TransactionCallback() { @@ -180,7 +188,6 @@ public class SynchronousTests implements ApplicationContextAware { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { String text = (String) txJmsTemplate.receiveAndConvert("queue"); - System.out.println("Receiving in transaction: " + text); list.add(text); jdbcTemplate.update("INSERT into T_BARS (id,name,foo_date) values (?,?,null)", list.size(), text); return RepeatStatus.continueIf(text != null); @@ -189,36 +196,32 @@ public class SynchronousTests implements ApplicationContextAware { // Simulate a message system failure before the main transaction // commits... -// txJmsTemplate.execute(new SessionCallback() { -// @Override -// public Void doInJms(Session session) throws JMSException { -// try { -// System.out.println("Session = " + session + " pass test? " + (session instanceof SessionProxy)); -// assertTrue("Not a SessionProxy - wrong spring version?", session instanceof SessionProxy); -// ((SessionProxy) session).getTargetSession().rollback(); -// } -// catch (JMSException e) { -// throw e; -// } -// catch (Exception e) { -// // swallow it -// e.printStackTrace(); -// } -// return null; -// } -// }); + txJmsTemplate.execute(new SessionCallback() { + @Override + public Void doInJms(Session session) throws JMSException { + try { + assertTrue("Not a SessionProxy - wrong spring version?", session instanceof SessionProxy); + ((SessionProxy) session).getTargetSession().rollback(); + } + catch (JMSException e) { + throw e; + } + catch (Exception e) { + // swallow it + e.printStackTrace(); + } + return null; + } + }); return null; } }); - System.err.println(jdbcTemplate.queryForList("select * from T_BARS")); - String text = ""; List msgs = new ArrayList(); while (text != null) { text = (String) txJmsTemplate.receiveAndConvert("queue"); - System.out.println("text = " + text); msgs.add(text); } @@ -232,4 +235,4 @@ public class SynchronousTests implements ApplicationContextAware { } -} +} \ No newline at end of file diff --git a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/jms/init.sql b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/jms/init.sql index 655789e8f..dc8893d01 100644 --- a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/jms/init.sql +++ b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/jms/init.sql @@ -1,3 +1,5 @@ +DROP TABLE T_BARS; + create table T_BARS ( id integer not null primary key, name varchar(80), diff --git a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/jms/jms-context.xml b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/jms/jms-context.xml index cb6c8e7f9..4c136324b 100644 --- a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/jms/jms-context.xml +++ b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/jms/jms-context.xml @@ -17,10 +17,13 @@ - - - vm://localhost - + + + + + + + - - org.springframework.batch.repeat.RepeatOperations - + - + @@ -92,7 +93,7 @@ - vm://localhost + vm://localhost?jms.prefetchPolicy.all=0