logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Shaw <j...@bitgladiator.com>
Subject How to make a custom connection source available in log4j 2 config?
Date Wed, 16 Apr 2014 19:55:49 GMT
Hello,
I made a custom connection source that I want to use. (Source follows
this message.) However, when I attempt to use my BoneCP connection
source in my config, I get the error, "ERROR JDBC contains an invalid
element or attribute "BoneCP"". What else do I need to do to make my
custom connection source available in the configuration?

I'm hoping the answer will also apply to a custom appender and manager
I've written, neither of which work because they also cannot be
instantiated from the configuration, however the error is a class not
found error.

Thanks,
Jeff

Source:

/**
 * Copyright (c) Bit Gladiator on 2014.
 */

@Plugin(name = "BoneCP", category="Core", elementType =
"connectionSource", printObject = true)
public class BoneCPConnectionSource implements ConnectionSource {
  private static final Logger LOGGER = StatusLogger.getLogger();

  private final BoneCP pool;

  private BoneCPConnectionSource(final BoneCP pool) {
    this.pool = pool;
  }

  @PluginFactory
  public static BoneCPConnectionSource createConnectionSource(
    @PluginAttribute("url") final String url,
    @PluginAttribute("username") final String username,
    @PluginAttribute("password") final String password,
    @PluginAttribute("partitionCount") final String partitionCount,
    @PluginAttribute("connectionsPerPartition") final String
connectionsPerPartition
  ) {
    BoneCPConfig config = new BoneCPConfig();
    config.setJdbcUrl(url);
    config.setUsername(username);
    config.setPassword(password);
    config.setPartitionCount(Integer.parseInt(partitionCount));
    config.setMaxConnectionsPerPartition(Integer.parseInt(connectionsPerPartition));
    try {
      BoneCP pool = new BoneCP(config);
      return new BoneCPConnectionSource(pool);
    } catch (SQLException e) {
      LOGGER.error("Failed to create the connection pool.");
      return null;
    }
  }

  @Override
  public Connection getConnection() throws SQLException {
    return pool.getConnection();
  }

  @Override
  public String toString() {
    return pool.getConfig().toString();
  }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message