Lambdas for Remaining Modules JPA -> ZK

Polishing - PR Comments and Closeable Warnings

Eclipse emits bogus warnings with exceptions in lambdas.
Even though the lambda might run on another thread, elipse thinks it could
cause the context to not be closed.

SPR-14854: MessageChannel is now a @FunctionalInterface

* Additional Lambda polishing and some code style fixes
This commit is contained in:
Gary Russell
2016-10-28 12:07:06 -04:00
committed by Artem Bilan
parent 16be9fc47d
commit 67d6cd0c89
83 changed files with 870 additions and 1324 deletions

View File

@@ -38,9 +38,7 @@ import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.util.ReflectionUtils;
/**
@@ -66,13 +64,7 @@ public class SubscribableRedisChannelTests extends RedisAvailableTests {
RedisMessageListenerContainer.class));
final CountDownLatch latch = new CountDownLatch(3);
MessageHandler handler = new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
latch.countDown();
}
};
MessageHandler handler = message -> latch.countDown();
channel.subscribe(handler);
channel.send(new GenericMessage<String>("1"));

View File

@@ -34,8 +34,6 @@ import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.integration.support.utils.IntegrationUtils;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.support.GenericMessage;
@@ -81,12 +79,9 @@ public class RedisChannelParserTests extends RedisAvailableTests {
final Message<?> m = new GenericMessage<String>("Hello Redis");
final CountDownLatch latch = new CountDownLatch(1);
redisChannel.subscribe(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
assertEquals(m.getPayload(), message.getPayload());
latch.countDown();
}
redisChannel.subscribe(message -> {
assertEquals(m.getPayload(), message.getPayload());
latch.countDown();
});
redisChannel.send(m);

View File

@@ -45,7 +45,6 @@ import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.connection.RedisConnectionFactory;
@@ -262,14 +261,7 @@ public class RedisQueueMessageDrivenEndpointTests extends RedisAvailableTests {
final CountDownLatch stopLatch = new CountDownLatch(1);
endpoint.stop(new Runnable() {
@Override
public void run() {
stopLatch.countDown();
}
});
endpoint.stop(() -> stopLatch.countDown());
executorService.shutdown();
assertTrue(executorService.awaitTermination(10, TimeUnit.SECONDS));
@@ -282,7 +274,6 @@ public class RedisQueueMessageDrivenEndpointTests extends RedisAvailableTests {
@Test
@RedisAvailable
@SuppressWarnings("unchecked")
@Ignore("JedisConnectionFactory doesn't support proper 'destroy()' and allows to create new fresh Redis connection")
public void testInt3196Recovery() throws Exception {
String queueName = "test.si.Int3196Recovery";
@@ -294,19 +285,9 @@ public class RedisQueueMessageDrivenEndpointTests extends RedisAvailableTests {
RedisQueueMessageDrivenEndpoint endpoint = new RedisQueueMessageDrivenEndpoint(queueName, this.connectionFactory);
endpoint.setBeanFactory(Mockito.mock(BeanFactory.class));
endpoint.setApplicationEventPublisher(new ApplicationEventPublisher() {
@Override
public void publishEvent(ApplicationEvent event) {
exceptionEvents.add(event);
exceptionsLatch.countDown();
}
@Override
public void publishEvent(Object event) {
}
endpoint.setApplicationEventPublisher(event -> {
exceptionEvents.add((ApplicationEvent) event);
exceptionsLatch.countDown();
});
endpoint.setOutputChannel(channel);
endpoint.setReceiveTimeout(100);

View File

@@ -35,8 +35,6 @@ import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.SubscribableChannel;
/**
@@ -108,6 +106,7 @@ public class RedisStoreInboundChannelAdapterIntegrationTests extends RedisAvaila
context.close();
}
@SuppressWarnings("resource")
@Test
@RedisAvailable
// synchronization rollback renames the list
@@ -120,14 +119,9 @@ public class RedisStoreInboundChannelAdapterIntegrationTests extends RedisAvaila
this.getClass());
SubscribableChannel fail = context.getBean("redisFailChannel", SubscribableChannel.class);
final CountDownLatch latch = new CountDownLatch(1);
fail.subscribe(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
latch.countDown();
throw new RuntimeException("Test Rollback");
}
fail.subscribe(message -> {
latch.countDown();
throw new RuntimeException("Test Rollback");
});
SourcePollingChannelAdapter spca = context.getBean("listAdapterWithSynchronizationAndRollback",
SourcePollingChannelAdapter.class);

View File

@@ -83,7 +83,7 @@ public class RedisPublishingMessageHandlerTests extends RedisAvailableTests {
private final CountDownLatch latch;
private Listener(CountDownLatch latch) {
Listener(CountDownLatch latch) {
this.latch = latch;
}

View File

@@ -327,27 +327,19 @@ public class RedisMessageGroupStoreTests extends RedisAvailableTests {
for (int i = 0; i < 100; i++) {
executor = Executors.newCachedThreadPool();
executor.execute(new Runnable() {
@Override
public void run() {
MessageGroup group = store1.addMessageToGroup(1, message);
if (group.getMessages().size() != 1) {
failures.add("ADD");
throw new AssertionFailedError("Failed on ADD");
}
executor.execute(() -> {
MessageGroup group = store1.addMessageToGroup(1, message);
if (group.getMessages().size() != 1) {
failures.add("ADD");
throw new AssertionFailedError("Failed on ADD");
}
});
executor.execute(new Runnable() {
@Override
public void run() {
store2.removeMessagesFromGroup(1, message);
MessageGroup group = store2.getMessageGroup(1);
if (group.getMessages().size() != 0) {
failures.add("REMOVE");
throw new AssertionFailedError("Failed on Remove");
}
executor.execute(() -> {
store2.removeMessagesFromGroup(1, message);
MessageGroup group = store2.getMessageGroup(1);
if (group.getMessages().size() != 0) {
failures.add("REMOVE");
throw new AssertionFailedError("Failed on Remove");
}
});

View File

@@ -131,17 +131,12 @@ public class AggregatorWithRedisLocksTests extends RedisAvailableTests {
public void testDistributedAggregator() throws Exception {
this.releaseStrategy.reset(1);
Executors.newSingleThreadExecutor().execute(asyncSend("foo", 1, 1));
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
try {
in2.send(new GenericMessage<String>("bar", stubHeaders(2, 2, 1)));
}
catch (Exception e) {
e.printStackTrace();
exception = e;
}
Executors.newSingleThreadExecutor().execute(() -> {
try {
in2.send(new GenericMessage<String>("bar", stubHeaders(2, 2, 1)));
}
catch (Exception e) {
exception = e;
}
});
assertTrue(this.releaseStrategy.latch2.await(10, TimeUnit.SECONDS));
@@ -162,17 +157,12 @@ public class AggregatorWithRedisLocksTests extends RedisAvailableTests {
}
private Runnable asyncSend(final String payload, final int sequence, final int correlation) {
return new Runnable() {
@Override
public void run() {
try {
in.send(new GenericMessage<String>(payload, stubHeaders(sequence, 2, correlation)));
}
catch (Exception e) {
e.printStackTrace();
exception = e;
}
return () -> {
try {
in.send(new GenericMessage<String>(payload, stubHeaders(sequence, 2, correlation)));
}
catch (Exception e) {
exception = e;
}
};
}

View File

@@ -31,7 +31,6 @@ import static org.junit.Assert.fail;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@@ -82,7 +81,7 @@ public class RedisLockRegistryTests extends RedisAvailableTests {
}
private RedisTemplate<String, ?> createTemplate() {
RedisTemplate<String, ?> template = new RedisTemplate<String, Object>();
RedisTemplate<String, ?> template = new RedisTemplate<>();
template.setConnectionFactory(this.getConnectionFactoryForTest());
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
@@ -206,21 +205,17 @@ public class RedisLockRegistryTests extends RedisAvailableTests {
lock1.lockInterruptibly();
final AtomicBoolean locked = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
Future<Object> result = Executors.newSingleThreadExecutor().submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
Lock lock2 = registry.obtain("foo");
locked.set(lock2.tryLock(200, TimeUnit.MILLISECONDS));
latch.countDown();
try {
lock2.unlock();
}
catch (IllegalStateException ise) {
return ise;
}
return null;
Future<Object> result = Executors.newSingleThreadExecutor().submit(() -> {
Lock lock2 = registry.obtain("foo");
locked.set(lock2.tryLock(200, TimeUnit.MILLISECONDS));
latch.countDown();
try {
lock2.unlock();
}
catch (IllegalStateException ise) {
return ise;
}
return null;
});
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertFalse(locked.get());
@@ -242,25 +237,21 @@ public class RedisLockRegistryTests extends RedisAvailableTests {
final CountDownLatch latch3 = new CountDownLatch(1);
lock1.lockInterruptibly();
assertNotNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
Lock lock2 = registry.obtain("foo");
try {
latch1.countDown();
lock2.lockInterruptibly();
assertNotNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
latch2.await(10, TimeUnit.SECONDS);
locked.set(true);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
finally {
lock2.unlock();
latch3.countDown();
}
Executors.newSingleThreadExecutor().execute(() -> {
Lock lock2 = registry.obtain("foo");
try {
latch1.countDown();
lock2.lockInterruptibly();
assertNotNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
latch2.await(10, TimeUnit.SECONDS);
locked.set(true);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
finally {
lock2.unlock();
latch3.countDown();
}
});
assertTrue(latch1.await(10, TimeUnit.SECONDS));
@@ -284,31 +275,26 @@ public class RedisLockRegistryTests extends RedisAvailableTests {
final CountDownLatch latch3 = new CountDownLatch(1);
lock1.lockInterruptibly();
assertNotNull(TestUtils.getPropertyValue(registry1, "hardThreadLocks", ThreadLocal.class).get());
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
Lock lock2 = registry2.obtain("foo");
Executors.newSingleThreadExecutor().execute(() -> {
Lock lock2 = registry2.obtain("foo");
try {
latch1.countDown();
lock2.lockInterruptibly();
assertNotNull(TestUtils.getPropertyValue(registry2, "hardThreadLocks", ThreadLocal.class).get());
latch2.await(10, TimeUnit.SECONDS);
locked.set(true);
}
catch (InterruptedException e1) {
Thread.currentThread().interrupt();
logger.error("Interrupted while locking: " + lock2, e1);
}
finally {
try {
latch1.countDown();
lock2.lockInterruptibly();
assertNotNull(TestUtils.getPropertyValue(registry2, "hardThreadLocks", ThreadLocal.class).get());
latch2.await(10, TimeUnit.SECONDS);
locked.set(true);
logger.debug("Locks in store: " + registry2.listLocks());
lock2.unlock();
latch3.countDown();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.error("Interrupted while locking: " + lock2, e);
}
finally {
try {
lock2.unlock();
latch3.countDown();
}
catch (IllegalStateException e) {
logger.error("Failed to unlock: " + lock2, e);
}
catch (IllegalStateException e2) {
logger.error("Failed to unlock: " + lock2, e2);
}
}
});
@@ -330,19 +316,15 @@ public class RedisLockRegistryTests extends RedisAvailableTests {
lock.lockInterruptibly();
final AtomicBoolean locked = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
Future<Object> result = Executors.newSingleThreadExecutor().submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
lock.unlock();
}
catch (IllegalStateException ise) {
latch.countDown();
return ise;
}
return null;
Future<Object> result = Executors.newSingleThreadExecutor().submit(() -> {
try {
lock.unlock();
}
catch (IllegalStateException ise) {
latch.countDown();
return ise;
}
return null;
});
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertFalse(locked.get());
@@ -500,15 +482,10 @@ public class RedisLockRegistryTests extends RedisAvailableTests {
Long expire = getExpire(registry, "foo");
Future<Object> result = Executors.newSingleThreadExecutor().submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
Lock lock2 = registry.obtain("foo");
assertFalse(lock2.tryLock());
return null;
}
Future<Object> result = Executors.newSingleThreadExecutor().submit(() -> {
Lock lock2 = registry.obtain("foo");
assertFalse(lock2.tryLock());
return null;
});
result.get();
assertEquals(expire, getExpire(registry, "foo"));