source: branches/objectmq-1.0/src/omq/common/util/OmqConnectionFactory.java

Last change on this file was 33, checked in by amoreno, 11 years ago

new release version

File size: 2.1 KB
Line 
1package omq.common.util;
2
3import java.io.IOException;
4import java.security.KeyManagementException;
5import java.security.NoSuchAlgorithmException;
6import java.util.Properties;
7
8import com.rabbitmq.client.Channel;
9import com.rabbitmq.client.Connection;
10import com.rabbitmq.client.ConnectionFactory;
11
12/**
13 *
14 * @author Sergi Toda <sergi.toda@estudiants.urv.cat>
15 *
16 */
17public class OmqConnectionFactory {
18        private static Connection connection;
19        private static int connectionTimeout = 2 * 1000;
20
21        public static void init(Properties env) throws KeyManagementException, NoSuchAlgorithmException, IOException {
22                if (connection == null) {
23                        connection = getNewConnection(env);
24                }
25        }
26
27        public static Connection getNewWorkingConnection(Properties env) throws Exception {
28                Connection connection = null;
29                boolean working = false;
30
31                while (!working) {
32                        try {
33                                connection = getNewConnection(env);
34                                working = true;
35                        } catch (Exception e) {
36                                e.printStackTrace();
37                                long milis = Long.parseLong(env.getProperty(ParameterQueue.RETRY_TIME_CONNECTION, "2000"));
38                                Thread.sleep(milis);
39                        }
40                }
41
42                return connection;
43        }
44
45        public static Connection getNewConnection(Properties env) throws IOException, KeyManagementException, NoSuchAlgorithmException {
46                // Get login info of rabbitmq
47                String username = env.getProperty(ParameterQueue.USER_NAME);
48                String password = env.getProperty(ParameterQueue.USER_PASS);
49
50                // Get host info of rabbimq (where it is)
51                String host = env.getProperty(ParameterQueue.SERVER_HOST);
52                int port = Integer.parseInt(env.getProperty(ParameterQueue.SERVER_PORT));
53
54                boolean ssl = Boolean.parseBoolean(env.getProperty(ParameterQueue.ENABLE_SSL));
55
56                // Start a new connection and channel
57                ConnectionFactory factory = new ConnectionFactory();
58                factory.setUsername(username);
59                factory.setPassword(password);
60                factory.setHost(host);
61                factory.setPort(port);
62                factory.setConnectionTimeout(connectionTimeout);
63                if (ssl) {
64                        factory.useSslProtocol();
65                }
66                return factory.newConnection();
67        }
68
69        public static Channel getNewChannel() throws IOException {
70                return connection.createChannel();
71        }
72}
Note: See TracBrowser for help on using the repository browser.