|
| 1 | +package org.gm4java.engine.support; |
| 2 | + |
| 3 | +import org.apache.commons.pool.impl.GenericObjectPool; |
| 4 | + |
| 5 | +/** |
| 6 | + * Defines the behavior of the {@link PooledGMService#getConnection()} method when the pool is exhausted. |
| 7 | + * |
| 8 | + * @see GMConnectionPoolConfig#getWhenExhaustedAction() |
| 9 | + */ |
| 10 | +public enum WhenExhaustedAction { |
| 11 | + /** |
| 12 | + * Throw a {@link NoSuchElementException}. |
| 13 | + */ |
| 14 | + FAIL(GenericObjectPool.WHEN_EXHAUSTED_FAIL), |
| 15 | + |
| 16 | + /** |
| 17 | + * Blocks until a new or idle connection is available. Or fail if maxWait is positive and passed. |
| 18 | + */ |
| 19 | + BLOCK(GenericObjectPool.WHEN_EXHAUSTED_BLOCK), |
| 20 | + |
| 21 | + /** |
| 22 | + * Create a new connection and return it (essentially making maxActive meaningless). |
| 23 | + */ |
| 24 | + GROW(GenericObjectPool.WHEN_EXHAUSTED_GROW); |
| 25 | + |
| 26 | + private WhenExhaustedAction(byte whenExhaustedAction) { |
| 27 | + this.whenExhaustedAction = whenExhaustedAction; |
| 28 | + } |
| 29 | + |
| 30 | + private final byte whenExhaustedAction; |
| 31 | + |
| 32 | + byte toValue() { |
| 33 | + return whenExhaustedAction; |
| 34 | + } |
| 35 | + |
| 36 | + static WhenExhaustedAction fromValue(byte whenExhaustedAction) { |
| 37 | + switch (whenExhaustedAction) { |
| 38 | + case GenericObjectPool.WHEN_EXHAUSTED_BLOCK: |
| 39 | + return WhenExhaustedAction.BLOCK; |
| 40 | + case GenericObjectPool.WHEN_EXHAUSTED_FAIL: |
| 41 | + return WhenExhaustedAction.FAIL; |
| 42 | + case GenericObjectPool.WHEN_EXHAUSTED_GROW: |
| 43 | + return WhenExhaustedAction.GROW; |
| 44 | + default: |
| 45 | + throw new IllegalArgumentException("whenExhaustedAction " + whenExhaustedAction + " not recognized."); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments