1 | package omq.common.util; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.security.KeyManagementException; |
---|
5 | import java.security.NoSuchAlgorithmException; |
---|
6 | import java.util.Properties; |
---|
7 | |
---|
8 | import org.apache.log4j.Logger; |
---|
9 | |
---|
10 | import com.rabbitmq.client.Channel; |
---|
11 | import com.rabbitmq.client.Connection; |
---|
12 | import com.rabbitmq.client.ConnectionFactory; |
---|
13 | |
---|
14 | /** |
---|
15 | * |
---|
16 | * @author Sergi Toda <sergi.toda@estudiants.urv.cat> |
---|
17 | * |
---|
18 | */ |
---|
19 | public 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 = 2000; |
---|
42 | if (env != null) { |
---|
43 | milis = Long.parseLong(env.getProperty(ParameterQueue.RETRY_TIME_CONNECTION, "2000")); |
---|
44 | } |
---|
45 | Thread.sleep(milis); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | return connection; |
---|
50 | } |
---|
51 | |
---|
52 | public static Connection getNewConnection(Properties env) throws IOException, KeyManagementException, NoSuchAlgorithmException { |
---|
53 | // Get login info of rabbitmq |
---|
54 | String username = env.getProperty(ParameterQueue.USER_NAME); |
---|
55 | String password = env.getProperty(ParameterQueue.USER_PASS); |
---|
56 | |
---|
57 | // Get host info of rabbimq (where it is) |
---|
58 | String host = env.getProperty(ParameterQueue.SERVER_HOST); |
---|
59 | int port = Integer.parseInt(env.getProperty(ParameterQueue.SERVER_PORT)); |
---|
60 | |
---|
61 | boolean ssl = Boolean.parseBoolean(env.getProperty(ParameterQueue.ENABLE_SSL)); |
---|
62 | |
---|
63 | // Start a new connection and channel |
---|
64 | ConnectionFactory factory = new ConnectionFactory(); |
---|
65 | factory.setUsername(username); |
---|
66 | factory.setPassword(password); |
---|
67 | factory.setHost(host); |
---|
68 | factory.setPort(port); |
---|
69 | factory.setConnectionTimeout(connectionTimeout); |
---|
70 | if (ssl) { |
---|
71 | factory.useSslProtocol(); |
---|
72 | } |
---|
73 | |
---|
74 | Connection connection = factory.newConnection(); |
---|
75 | logger.info("New connection created using: username: " + username + ", host: " + host + ", port: " + port + ", connection timeout: " |
---|
76 | + connectionTimeout + " SSL enabled: " + ssl); |
---|
77 | return connection; |
---|
78 | } |
---|
79 | |
---|
80 | public static Channel getNewChannel() throws IOException { |
---|
81 | Channel channel = connection.createChannel(); |
---|
82 | logger.info("New channel created using the default connection"); |
---|
83 | return channel; |
---|
84 | } |
---|
85 | } |
---|