source: trunk/src/main/java/omq/common/util/OmqConnectionFactory.java @ 49

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

log4j added

File size: 2.5 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 *
16 * @author Sergi Toda <sergi.toda@estudiants.urv.cat>
17 *
18 */
19public class OmqConnectionFactory {
20        private static final Logger logger = Logger.getLogger(OmqConnectionFactory.class.getName());
21
22        private static Connection connection;
23        private static int connectionTimeout = 2 * 1000;
24
25        public static void init(Properties env) throws KeyManagementException, NoSuchAlgorithmException, IOException {
26                if (connection == null) {
27                        connection = getNewConnection(env);
28                }
29        }
30
31        public static Connection getNewWorkingConnection(Properties env) throws Exception {
32                Connection connection = null;
33                boolean working = false;
34
35                while (!working) {
36                        try {
37                                connection = getNewConnection(env);
38                                working = true;
39                        } catch (Exception e) {
40                                logger.error(e);
41                                long milis = Long.parseLong(env.getProperty(ParameterQueue.RETRY_TIME_CONNECTION, "2000"));
42                                Thread.sleep(milis);
43                        }
44                }
45
46                return connection;
47        }
48
49        public static Connection getNewConnection(Properties env) throws IOException, KeyManagementException, NoSuchAlgorithmException {
50                // Get login info of rabbitmq
51                String username = env.getProperty(ParameterQueue.USER_NAME);
52                String password = env.getProperty(ParameterQueue.USER_PASS);
53
54                // Get host info of rabbimq (where it is)
55                String host = env.getProperty(ParameterQueue.SERVER_HOST);
56                int port = Integer.parseInt(env.getProperty(ParameterQueue.SERVER_PORT));
57
58                boolean ssl = Boolean.parseBoolean(env.getProperty(ParameterQueue.ENABLE_SSL));
59
60                // Start a new connection and channel
61                ConnectionFactory factory = new ConnectionFactory();
62                factory.setUsername(username);
63                factory.setPassword(password);
64                factory.setHost(host);
65                factory.setPort(port);
66                factory.setConnectionTimeout(connectionTimeout);
67                if (ssl) {
68                        factory.useSslProtocol();
69                }
70
71                Connection connection = factory.newConnection();
72                logger.info("New connection created using: username: " + username + ", host: " + host + ", port: " + port + ", connection timeout: "
73                                + connectionTimeout + " SSL enabled: " + ssl);
74                return connection;
75        }
76
77        public static Channel getNewChannel() throws IOException {
78                Channel channel = connection.createChannel();
79                logger.info("New channel created using the default connection");
80                return channel;
81        }
82}
Note: See TracBrowser for help on using the repository browser.