/src/com/googlecode/jmxtrans/util/JmxConnectionFactory.java

http://jmxtrans.googlecode.com/ · Java · 57 lines · 33 code · 10 blank · 14 comment · 0 complexity · 65b7aeec6bca836a2ad6ef7095a84a82 MD5 · raw file

  1. package com.googlecode.jmxtrans.util;
  2. import java.io.IOException;
  3. import javax.management.remote.JMXConnector;
  4. import org.apache.commons.pool.BaseKeyedPoolableObjectFactory;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import com.googlecode.jmxtrans.model.Server;
  8. /**
  9. * Allows us to pool connections to remote jmx servers.
  10. */
  11. public class JmxConnectionFactory extends BaseKeyedPoolableObjectFactory {
  12. @SuppressWarnings("unused")
  13. private static final Logger log = LoggerFactory.getLogger(JmxConnectionFactory.class);
  14. /** constructor */
  15. public JmxConnectionFactory() {}
  16. /**
  17. * Creates the connection.
  18. */
  19. @Override
  20. public Object makeObject(Object key) throws Exception {
  21. Server server = (Server) key;
  22. return JmxUtils.getServerConnection(server);
  23. }
  24. /**
  25. * Closes the connection.
  26. */
  27. @Override
  28. public void destroyObject(Object key, Object obj) throws Exception {
  29. JMXConnector conn = (JMXConnector) obj;
  30. conn.close();
  31. }
  32. /**
  33. * Validates that the connection is good.
  34. */
  35. @Override
  36. public boolean validateObject(Object key, Object obj) {
  37. JMXConnector conn = (JMXConnector) obj;
  38. boolean result = false;
  39. try {
  40. conn.getConnectionId();
  41. result = true;
  42. } catch (IOException ex) {
  43. // ignored
  44. }
  45. return result;
  46. }
  47. }