diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/history/MessageHistory.java b/org.springframework.integration/src/main/java/org/springframework/integration/history/MessageHistory.java index 294587a0c7..d5cbc2cf8d 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/history/MessageHistory.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/history/MessageHistory.java @@ -30,8 +30,10 @@ import org.springframework.integration.support.ComponentMetadata; * Iterable list of {@link MessageHistoryEvent} instances. * * @author Mark Fisher + * @author Oleg Zhurakousky * @since 2.0 */ +@SuppressWarnings("serial") public class MessageHistory implements Iterable, Serializable { private final List events = new ArrayList(); @@ -76,14 +78,26 @@ public class MessageHistory implements Iterable, Serializab } public int hashCode() { - return 17 * this.events.hashCode(); + + try { + this.lock.readLock().lock(); + return 17 * this.events.hashCode(); + } + finally { + this.lock.readLock().unlock(); + } } /** * Returns a String representation of the history event list. */ public String toString() { - return this.events.toString(); + try { + this.lock.readLock().lock(); + return this.events.toString(); + } + finally { + this.lock.readLock().unlock(); + } } - } diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/history/MessageHistoryTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/history/MessageHistoryTests.java new file mode 100644 index 0000000000..065b76173d --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/history/MessageHistoryTests.java @@ -0,0 +1,72 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed 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 org.springframework.integration.history; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import org.junit.Test; +import org.springframework.integration.support.ComponentMetadata; + +/** + * @author Oleg Zhurakousky + * @since 2.0 + */ +public class MessageHistoryTests { + private long times = 1000; + private ExecutorService executor = Executors.newCachedThreadPool(); + @Test + public void testConcurrentModificationsOnObjectMethods() throws Exception{ + final MessageHistory history = new MessageHistory(); + + executor.execute(new Runnable() { + public void run() { + for (int i = 0; i < times; i++) { + ComponentMetadata event = new ComponentMetadata(); + event.setAttribute("foo", "foo"); + event.setAttribute("bar", "bar"); + event.setComponentName("MessageHistoryTests"); + history.addEvent(event); + } + } + }); + + executor.execute(new Runnable() { + public void run() { + for (int i = 0; i < times; i++) { + history.toString(); + } + } + }); + executor.execute(new Runnable() { + public void run() { + for (int i = 0; i < times; i++) { + history.hashCode(); + } + } + }); + executor.execute(new Runnable() { + public void run() { + for (int i = 0; i < times; i++) { + history.equals(new Object()); + } + } + }); + executor.shutdown(); + executor.awaitTermination(3, TimeUnit.SECONDS); + } +}