-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathConnectionsManager.java
More file actions
112 lines (95 loc) · 3.11 KB
/
ConnectionsManager.java
File metadata and controls
112 lines (95 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.amazon.sqs.javamessaging.jndi;
import java.util.HashSet;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.jms.IllegalStateException;
import javax.jms.JMSException;
import javax.naming.NamingException;
import javax.naming.directory.InvalidAttributeValueException;
import com.amazon.sqs.javamessaging.SQSConnection;
import com.amazon.sqs.javamessaging.SQSConnectionFactory;
/**
* Manage the use of {@link SQSConnection connections} and their closings
* through an {@link SQSConnectionFactory} instance.
*
* @author krloss
* @since 1.1.0
*/
public class ConnectionsManager {
/**
* Set of connection configuration parameters.<br>
* Externally visible information.
*/
protected final SQSConnectionFactory connectionFactory;
private final HashSet<Callable<Boolean>> closeableConnections = new HashSet<>();
private SQSConnection defaultConnection;
private final Object stateLock = new Object(); // Used for interactions with connection state.
/**
* Public constructor that requires {@link SQSConnectionFactory} parameter.
*
* @param connectionFactory - set of connection configuration parameters.
* @throws NamingException
*/
public ConnectionsManager(final SQSConnectionFactory connectionFactory) throws InvalidAttributeValueException {
if(connectionFactory == null ) throw new InvalidAttributeValueException("ConnectionsManager Requires SQSConnectionFactory.");
this.connectionFactory = connectionFactory;
}
private static final Callable<Boolean> createCloseableConnection(final SQSConnection connection) {
return (new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
connection.close();
return true;
}
});
}
/**
* Creates and returns a new connection.
*
* @return {@link SQSConnection}
* @throws JMSException
*/
public SQSConnection createConnection() throws JMSException {
SQSConnection connection = connectionFactory.createConnection();
synchronized(stateLock) {
closeableConnections.add(createCloseableConnection(connection));
}
return connection;
}
/**
* Get default connection lazily.
*
* @return {@link SQSConnection}
* @throws JMSException
*/
public synchronized SQSConnection getLazyDefaultConnection() throws JMSException {
if(defaultConnection == null) defaultConnection = createConnection();
return defaultConnection;
}
private void close(ExecutorService executor) throws InterruptedException {
synchronized(stateLock) {
defaultConnection = null;
executor.invokeAll(closeableConnections);
closeableConnections.clear();
}
}
/**
* Manage the closing of {@link SQSConnection connections} through asynchronous tasks using a thread pool.
*
* @throws JMSException
* @see Executors#newCachedThreadPool()
*/
public synchronized void close() throws JMSException {
ExecutorService executor = Executors.newCachedThreadPool();
try {
close(executor);
}
catch(InterruptedException ie) {
throw new IllegalStateException(ie.getMessage());
}
finally {
executor.shutdown();
}
}
}