1 | package omq.common.remote; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.security.KeyManagementException; |
---|
5 | import java.security.NoSuchAlgorithmException; |
---|
6 | import java.util.Properties; |
---|
7 | |
---|
8 | import omq.common.util.ParameterQueue; |
---|
9 | |
---|
10 | |
---|
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 | public static Connection getConnection(Properties env) throws IOException, KeyManagementException, NoSuchAlgorithmException { |
---|
21 | // Get login info of rabbitmq |
---|
22 | String username = env.getProperty(ParameterQueue.USER_NAME); |
---|
23 | String password = env.getProperty(ParameterQueue.USER_PASS); |
---|
24 | |
---|
25 | // Get host info of rabbimq (where it is) |
---|
26 | String host = env.getProperty(ParameterQueue.SERVER_HOST); |
---|
27 | int port = Integer.parseInt(env.getProperty(ParameterQueue.SERVER_PORT)); |
---|
28 | |
---|
29 | boolean ssl = Boolean.parseBoolean(env.getProperty(ParameterQueue.ENABLE_SSL)); |
---|
30 | |
---|
31 | // Start a new connection and channel |
---|
32 | ConnectionFactory factory = new ConnectionFactory(); |
---|
33 | factory.setUsername(username); |
---|
34 | factory.setPassword(password); |
---|
35 | factory.setHost(host); |
---|
36 | factory.setPort(port); |
---|
37 | if (ssl) { |
---|
38 | factory.useSslProtocol(); |
---|
39 | } |
---|
40 | return factory.newConnection(); |
---|
41 | } |
---|
42 | } |
---|