[44] | 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 | |
---|
[49] | 8 | import org.apache.log4j.Logger; |
---|
| 9 | |
---|
[44] | 10 | import com.rabbitmq.client.Channel; |
---|
| 11 | import com.rabbitmq.client.Connection; |
---|
| 12 | import com.rabbitmq.client.ConnectionFactory; |
---|
| 13 | |
---|
| 14 | /** |
---|
[83] | 15 | * This class creates RabbitMQ connections |
---|
[44] | 16 | * |
---|
| 17 | * @author Sergi Toda <sergi.toda@estudiants.urv.cat> |
---|
| 18 | * |
---|
| 19 | */ |
---|
| 20 | public class OmqConnectionFactory { |
---|
[49] | 21 | private static final Logger logger = Logger.getLogger(OmqConnectionFactory.class.getName()); |
---|
| 22 | |
---|
[44] | 23 | private static Connection connection; |
---|
| 24 | private static int connectionTimeout = 2 * 1000; |
---|
| 25 | |
---|
[83] | 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 | */ |
---|
[44] | 36 | public static void init(Properties env) throws KeyManagementException, NoSuchAlgorithmException, IOException { |
---|
| 37 | if (connection == null) { |
---|
| 38 | connection = getNewConnection(env); |
---|
| 39 | } |
---|
| 40 | } |
---|
| 41 | |
---|
[83] | 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 | */ |
---|
[44] | 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) { |
---|
[49] | 59 | logger.error(e); |
---|
[51] | 60 | long milis = 2000; |
---|
| 61 | if (env != null) { |
---|
| 62 | milis = Long.parseLong(env.getProperty(ParameterQueue.RETRY_TIME_CONNECTION, "2000")); |
---|
| 63 | } |
---|
[44] | 64 | Thread.sleep(milis); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | return connection; |
---|
| 69 | } |
---|
| 70 | |
---|
[83] | 71 | /** |
---|
| 72 | * This function creates a new rabbitmq connection using the properties set |
---|
| 73 | * in env |
---|
| 74 | * |
---|
| 75 | * @param env |
---|
| 76 | * - Properties needed to create a new connection: username, |
---|
| 77 | * password, rabbit_host, rabbit_port, enable_ssl (optional) |
---|
| 78 | * @return new Connection |
---|
| 79 | * @throws IOException |
---|
| 80 | * @throws KeyManagementException |
---|
| 81 | * @throws NoSuchAlgorithmException |
---|
| 82 | */ |
---|
[44] | 83 | public static Connection getNewConnection(Properties env) throws IOException, KeyManagementException, NoSuchAlgorithmException { |
---|
| 84 | // Get login info of rabbitmq |
---|
| 85 | String username = env.getProperty(ParameterQueue.USER_NAME); |
---|
| 86 | String password = env.getProperty(ParameterQueue.USER_PASS); |
---|
| 87 | |
---|
| 88 | // Get host info of rabbimq (where it is) |
---|
[77] | 89 | String host = env.getProperty(ParameterQueue.RABBIT_HOST); |
---|
| 90 | int port = Integer.parseInt(env.getProperty(ParameterQueue.RABBIT_PORT)); |
---|
[44] | 91 | |
---|
[83] | 92 | boolean ssl = Boolean.parseBoolean(env.getProperty(ParameterQueue.ENABLE_SSL, "false")); |
---|
[44] | 93 | |
---|
| 94 | // Start a new connection and channel |
---|
| 95 | ConnectionFactory factory = new ConnectionFactory(); |
---|
| 96 | factory.setUsername(username); |
---|
| 97 | factory.setPassword(password); |
---|
| 98 | factory.setHost(host); |
---|
| 99 | factory.setPort(port); |
---|
| 100 | factory.setConnectionTimeout(connectionTimeout); |
---|
| 101 | if (ssl) { |
---|
| 102 | factory.useSslProtocol(); |
---|
| 103 | } |
---|
[49] | 104 | |
---|
| 105 | Connection connection = factory.newConnection(); |
---|
| 106 | logger.info("New connection created using: username: " + username + ", host: " + host + ", port: " + port + ", connection timeout: " |
---|
| 107 | + connectionTimeout + " SSL enabled: " + ssl); |
---|
| 108 | return connection; |
---|
[44] | 109 | } |
---|
| 110 | |
---|
[83] | 111 | /** |
---|
| 112 | * This method creates a new channel if the singleton pattern is used |
---|
| 113 | * |
---|
| 114 | * @return new Channel |
---|
| 115 | * @throws IOException |
---|
| 116 | */ |
---|
[44] | 117 | public static Channel getNewChannel() throws IOException { |
---|
[49] | 118 | Channel channel = connection.createChannel(); |
---|
| 119 | logger.info("New channel created using the default connection"); |
---|
| 120 | return channel; |
---|
[44] | 121 | } |
---|
| 122 | } |
---|