/src/main/java/org/mule/transport/rmq/RmqConnector.java

https://github.com/Ricston/mule-transport-rmq · Java · 279 lines · 194 code · 52 blank · 33 comment · 21 complexity · 47a86cc5f2134869420a1974d7353f89 MD5 · raw file

  1. /*
  2. * $Id: RmqConnector.java 1486 2011-09-30 09:57:05Z claude.mamo $
  3. * --------------------------------------------------------------------------------------
  4. * Copyright (c) Ricston Ltd. All rights reserved. http://www.ricston.com
  5. *
  6. * The software in this package is published under the terms of the CPAL v1.0
  7. * license, a copy of which has been included with this distribution in the
  8. * LICENSE.txt file.
  9. */
  10. package org.mule.transport.rmq;
  11. import java.io.IOException;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. import java.util.Set;
  15. import org.mule.transport.AbstractConnector;
  16. import org.mule.transport.rmq.essentials.Exchange;
  17. import org.mule.transport.rmq.parsers.RmqConnectorParser;
  18. import org.mule.api.MuleException;
  19. import org.mule.api.lifecycle.InitialisationException;
  20. import com.rabbitmq.client.Channel;
  21. import com.rabbitmq.client.Connection;
  22. import com.rabbitmq.client.ConnectionFactory;
  23. public class RmqConnector extends AbstractConnector
  24. {
  25. // Default variables.
  26. public static final String DEFAULT_HOST = "localhost";
  27. public static final int DEFAULT_PORT = 5672;
  28. public static final String DEFAULT_USERNAME = "guest";
  29. public static final String DEFAULT_PASSWORD = "guest";
  30. public static final String DEFAULT_VIRTUALHOST = "/";
  31. // //////////////////////////////////////////////////////////////////////
  32. // RMQ Connection
  33. // //////////////////////////////////////////////////////////////////////
  34. private ConnectionFactory connectionFactory;
  35. private Connection connection;
  36. private Channel channel;
  37. private Map<String, Exchange> exchangeMap = new HashMap<String, Exchange>();
  38. private boolean springAutoName = true;
  39. // //////////////////////////////////////////////////////////////////////
  40. // RMQ Connection Variables
  41. // //////////////////////////////////////////////////////////////////////
  42. private String connectorURI;
  43. private String host = DEFAULT_HOST;
  44. private int port = DEFAULT_PORT;
  45. private String vhost = DEFAULT_VIRTUALHOST;
  46. private String username = DEFAULT_USERNAME;
  47. private String password = DEFAULT_PASSWORD;
  48. private int channelNumber = -1;
  49. /* This constant defines the main transport protocol identifier */
  50. public static final String RMQ = "rmq";
  51. public void doInitialise() throws InitialisationException
  52. {
  53. // Construct a connection factory.
  54. connectionFactory = this.createConnectionFactory();
  55. }
  56. private ConnectionFactory createConnectionFactory()
  57. {
  58. ConnectionFactory factory = new ConnectionFactory();
  59. // Construct the factory from the connector URI if available
  60. // else from the variables.
  61. if (connectorURI != null)
  62. {
  63. RmqConnectorParser rcp = new RmqConnectorParser(connectorURI);
  64. host = rcp.getHost();
  65. port = rcp.getPort();
  66. vhost = rcp.getVhost();
  67. username = rcp.getUsername();
  68. password = rcp.getPassword();
  69. }
  70. factory.setHost(host);
  71. factory.setPort(port);
  72. if (vhost != null) factory.setVirtualHost(vhost);
  73. if (password != null) factory.setPassword(password);
  74. if (username != null) factory.setUsername(username);
  75. return factory;
  76. }
  77. public void doConnect() throws Exception
  78. {
  79. // Create a connection from the connection factory.
  80. connection = createConnection();
  81. // Create a channel from the connection.
  82. channel = connection.createChannel();
  83. // Get the list of exchanges to list.
  84. Set<String> keys = exchangeMap.keySet();
  85. // Pass through the map declaring the exchanges.
  86. for (String key : keys)
  87. {
  88. if (springAutoName) exchangeMap.get(key).setName(key);
  89. Exchange exchange = exchangeMap.get(key);
  90. if (exchange.getName() == null)
  91. {
  92. if (exchange.getType().equals("direct")) exchange.setName("amq.direct");
  93. if (exchange.getType().equals("fanout")) exchange.setName("amq.fanout");
  94. if (exchange.getType().equals("topic")) exchange.setName("amq.topic");
  95. }
  96. channel.exchangeDeclare(exchange.getName(), exchange.getType(), exchange.isDurable(),
  97. exchange.isAutoDelete(), exchange.getArguments());
  98. }
  99. }
  100. private Connection createConnection()
  101. {
  102. // Create a new connection unless an exception is thrown.
  103. try
  104. {
  105. return connectionFactory.newConnection();
  106. }
  107. catch (IOException e)
  108. {
  109. logger.error(e.getMessage());
  110. }
  111. return null;
  112. }
  113. public synchronized void doDisconnect() throws Exception
  114. {
  115. // Check that channel is not null or is open and close it.
  116. if (channel != null && channel.isOpen()) channel.close();
  117. // Check that connection is not null or is open and close it.
  118. if (connection != null && connection.isOpen()) connection.close();
  119. }
  120. public void doStart() throws MuleException
  121. {
  122. // EMPTY//
  123. }
  124. public void doStop() throws MuleException
  125. {
  126. // EMPTY//
  127. }
  128. public void doDispose()
  129. {
  130. // EMPTY//
  131. }
  132. public String getProtocol()
  133. {
  134. return RMQ;
  135. }
  136. // //////////////////////////////////////////////////////////////////////
  137. // Getters and Setters
  138. // //////////////////////////////////////////////////////////////////////
  139. protected Connection getConnection()
  140. {
  141. return connection;
  142. }
  143. protected void setConnection(Connection connection)
  144. {
  145. this.connection = connection;
  146. }
  147. protected Channel getChannel()
  148. {
  149. return channel;
  150. }
  151. protected void setChannel(Channel channel)
  152. {
  153. this.channel = channel;
  154. }
  155. public String getHost()
  156. {
  157. return host;
  158. }
  159. public void setHost(String host)
  160. {
  161. this.host = host;
  162. }
  163. public int getPort()
  164. {
  165. return port;
  166. }
  167. public void setPort(int port)
  168. {
  169. this.port = port;
  170. }
  171. public String getVhost()
  172. {
  173. return vhost;
  174. }
  175. public void setVhost(String vhost)
  176. {
  177. this.vhost = vhost;
  178. }
  179. public String getUsername()
  180. {
  181. return username;
  182. }
  183. public void setUsername(String username)
  184. {
  185. this.username = username;
  186. }
  187. public String getPassword()
  188. {
  189. return password;
  190. }
  191. public void setPassword(String password)
  192. {
  193. this.password = password;
  194. }
  195. public int getChannelNumber()
  196. {
  197. return channelNumber;
  198. }
  199. public void setChannelNumber(int channel_no)
  200. {
  201. this.channelNumber = channel_no;
  202. }
  203. public void setExchangeMap(Map<String, Exchange> exchangeMap)
  204. {
  205. this.exchangeMap = exchangeMap;
  206. }
  207. public Map<String, Exchange> getExchangeMap()
  208. {
  209. return exchangeMap;
  210. }
  211. public void setSpringAutoName(boolean springAutoName)
  212. {
  213. this.springAutoName = springAutoName;
  214. }
  215. public boolean isSpringAutoName()
  216. {
  217. return springAutoName;
  218. }
  219. public void setConnectorURI(String connectorURI)
  220. {
  221. this.connectorURI = connectorURI;
  222. }
  223. public String getConnectorURI()
  224. {
  225. return connectorURI;
  226. }
  227. }