INT-4422: No MessagingException in SimplePool

JIRA: https://jira.spring.io/browse/INT-4422

Inappropriate exception type, there is no message in the context of the pool.

Add a new exception.
This commit is contained in:
Gary Russell
2018-05-10 14:43:42 -04:00
committed by Artem Bilan
parent d9186f1b4a
commit c2895f67cc
3 changed files with 49 additions and 11 deletions

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2018 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.util;
/**
* Thrown when a pooled item could not be obtained for some reason.
*
* @author Gary Russell
* @since 5.1
*
*/
@SuppressWarnings("serial")
public class PoolItemNotAvailableException extends RuntimeException {
public PoolItemNotAvailableException(String message, Throwable cause) {
super(message, cause);
}
public PoolItemNotAvailableException(String message) {
super(message);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@@ -28,7 +28,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.messaging.MessagingException;
import org.springframework.util.Assert;
/**
@@ -155,7 +154,7 @@ public class SimplePool<T> implements Pool<T> {
/**
* Obtains an item from the pool; waits up to waitTime milliseconds (default infinity).
* @throws MessagingException if no items become available in time.
* @throws PoolItemNotAvailableException if no items become available in time.
*/
@Override
public T getItem() {
@@ -166,10 +165,10 @@ public class SimplePool<T> implements Pool<T> {
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new MessagingException("Interrupted awaiting a pooled resource", e);
throw new PoolItemNotAvailableException("Interrupted awaiting a pooled resource", e);
}
if (!permitted) {
throw new IllegalStateException("Timed out while waiting to acquire a pool entry.");
throw new PoolItemNotAvailableException("Timed out while waiting to acquire a pool entry.");
}
return doGetItem();
}
@@ -177,10 +176,10 @@ public class SimplePool<T> implements Pool<T> {
if (permitted) {
this.permits.release();
}
if (e instanceof MessagingException) {
throw (MessagingException) e;
if (e instanceof PoolItemNotAvailableException) {
throw (PoolItemNotAvailableException) e;
}
throw new MessagingException("Failed to obtain pooled item", e);
throw new PoolItemNotAvailableException("Failed to obtain pooled item", e);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -30,7 +30,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Test;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.MessagingException;
/**
* @author Gary Russell
@@ -85,7 +84,7 @@ public class SimplePoolTests {
pool.getItem();
fail("Expected exception");
}
catch (MessagingException e) { }
catch (PoolItemNotAvailableException e) { }
// resize up
pool.setPoolSize(4);
@@ -154,17 +153,20 @@ public class SimplePoolTests {
final AtomicBoolean stale) {
SimplePool<String> pool = new SimplePool<String>(size, new SimplePool.PoolItemCallback<String>() {
private int i;
@Override
public String createForPool() {
String string = "String" + i++;
strings.add(string);
return string;
}
@Override
public boolean isStale(String item) {
if (stale.get()) {
strings.remove(item);
}
return stale.get();
}
@Override
public void removedFromPool(String item) {
strings.remove(item);
}