source: branches/supervisor/src/main/java/omq/common/util/OmqConnectionFactory.java @ 102

Last change on this file since 102 was 102, checked in by stoda, 11 years ago

All tests working

TODO sometimes exceptiontest fails

File size: 3.6 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 org.apache.log4j.Logger;
9
10import com.rabbitmq.client.Channel;
11import com.rabbitmq.client.Connection;
12import com.rabbitmq.client.ConnectionFactory;
13
14/**
15 * This class creates RabbitMQ connections
16 *
17 * @author Sergi Toda <sergi.toda@estudiants.urv.cat>
18 *
19 */
20public class OmqConnectionFactory {
21        private static final Logger logger = Logger.getLogger(OmqConnectionFactory.class.getName());
22
23        private static Connection connection;
24        private static int connectionTimeout = 2 * 1000;
25
26        /**
27         * If this class is wanted to use as a singleton class this is the init
28         * function
29         *
30         * @param env
31         *            - environment that sets the properties needed by RabbitMQ
32         * @throws KeyManagementException
33         * @throws NoSuchAlgorithmException
34         * @throws IOException
35         */
36        public static void init(Properties env) throws KeyManagementException, NoSuchAlgorithmException, IOException {
37                if (connection == null) {
38                        connection = getNewConnection(env);
39                }
40        }
41
42        /**
43         * This function returns a working connection.
44         *
45         * @param env
46         *            - used if it's necessary to create a new connection
47         * @return workingConnection
48         * @throws Exception
49         */
50        public static Connection getNewWorkingConnection(Properties env) throws Exception {
51                Connection connection = null;
52                boolean working = false;
53
54                while (!working) {
55                        try {
56                                connection = getNewConnection(env);
57                                working = true;
58                        } catch (Exception e) {
59//                              e.printStackTrace();
60                                logger.error(e);
61                                long milis = 2000;
62                                if (env != null) {
63                                        milis = Long.parseLong(env.getProperty(ParameterQueue.RETRY_TIME_CONNECTION, "2000"));
64                                }
65                                Thread.sleep(milis);
66                        }
67                }
68
69                return connection;
70        }
71
72        /**
73         * This function creates a new rabbitmq connection using the properties set
74         * in env
75         *
76         * @param env
77         *            - Properties needed to create a new connection: username,
78         *            password, rabbit_host, rabbit_port, enable_ssl (optional)
79         * @return new Connection
80         * @throws IOException
81         * @throws KeyManagementException
82         * @throws NoSuchAlgorithmException
83         */
84        public static Connection getNewConnection(Properties env) throws IOException, KeyManagementException, NoSuchAlgorithmException {
85                // Get login info of rabbitmq
86                String username = env.getProperty(ParameterQueue.USER_NAME);
87                String password = env.getProperty(ParameterQueue.USER_PASS);
88
89                // Get host info of rabbimq (where it is)
90                String host = env.getProperty(ParameterQueue.RABBIT_HOST);
91                int port = Integer.parseInt(env.getProperty(ParameterQueue.RABBIT_PORT));
92
93                boolean ssl = Boolean.parseBoolean(env.getProperty(ParameterQueue.ENABLE_SSL, "false"));
94
95                // Start a new connection and channel
96                ConnectionFactory factory = new ConnectionFactory();
97                factory.setUsername(username);
98                factory.setPassword(password);
99                factory.setHost(host);
100                factory.setPort(port);
101                factory.setConnectionTimeout(connectionTimeout);
102                if (ssl) {
103                        factory.useSslProtocol();
104                }
105
106                Connection connection = factory.newConnection();
107                logger.info("New connection created using: username: " + username + ", host: " + host + ", port: " + port + ", connection timeout: "
108                                + connectionTimeout + " SSL enabled: " + ssl);
109                return connection;
110        }
111
112        /**
113         * This method creates a new channel if the singleton pattern is used
114         *
115         * @return new Channel
116         * @throws IOException
117         */
118        public static Channel getNewChannel() throws IOException {
119                Channel channel = connection.createChannel();
120                logger.info("New channel created using the default connection");
121                return channel;
122        }
123}
Note: See TracBrowser for help on using the repository browser.