Exemplo n.º 1
0
  @Override
  protected void doStart() throws Exception {

    if (isEndpointTransacted()) {
      throw new IllegalArgumentException(
          "InOut exchange pattern is incompatible with transacted=true as it cuases a deadlock. Please use transacted=false or InOnly exchange pattern.");
    }

    if (getConnectionResource() == null) {
      throw new IllegalArgumentException(
          String.format("ConnectionResource or ConnectionFactory must be configured for %s", this));
    }

    if (ObjectHelper.isEmpty(getNamedReplyTo())) {
      log.debug("No reply to destination is defined.  Using temporary destinations.");
    } else {
      log.debug("Using {} as the reply to destination.", getNamedReplyTo());
    }
    if (uuidGenerator == null) {
      // use the generator configured on the camel context
      uuidGenerator = getEndpoint().getCamelContext().getUuidGenerator();
    }
    if (consumers == null) {
      consumers =
          new GenericObjectPool<MessageConsumerResources>(new MessageConsumerResourcesFactory());
      consumers.setMaxActive(getConsumerCount());
      consumers.setMaxIdle(getConsumerCount());
      while (consumers.getNumIdle() < consumers.getMaxIdle()) {
        consumers.addObject();
      }
    }
    super.doStart();
  }
Exemplo n.º 2
0
  /** @return @throws Exception */
  public DataSource setUp() throws Exception {
    /** Load JDBC Driver class. */
    Class.forName(ConnectionPool.DRIVER).newInstance();

    /** Creates an instance of GenericObjectPool that holds our pool of connections object. */
    connectionPool = new GenericObjectPool();
    // set the max number of connections
    connectionPool.setMaxActive(connections);
    // if the pool is exhausted (i.e., the maximum number of active objects has been reached), the
    // borrowObject() method should simply create a new object anyway
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);

    /**
     * Creates a connection factory object which will be used by the pool to create the connection
     * object. We pass the JDBC url info, username and password.
     */
    ConnectionFactory cf =
        new DriverManagerConnectionFactory(
            ConnectionPool.URL, ConnectionPool.USERNAME, ConnectionPool.PASSWORD);

    /**
     * Creates a PoolableConnectionFactory that will wrap the connection object created by the
     * ConnectionFactory to add object pooling functionality.
     */
    PoolableConnectionFactory pcf =
        new PoolableConnectionFactory(cf, connectionPool, null, null, false, true);
    return new PoolingDataSource(connectionPool);
  }
Exemplo n.º 3
0
  protected void doTransform(
      MuleMessage message, String outputEncoding, Source sourceDoc, Result result)
      throws Exception {
    DefaultErrorListener errorListener = new DefaultErrorListener(this);
    javax.xml.transform.Transformer transformer = null;

    try {
      transformer = (javax.xml.transform.Transformer) transformerPool.borrowObject();

      transformer.setErrorListener(errorListener);
      transformer.setOutputProperty(OutputKeys.ENCODING, outputEncoding);

      // set transformation parameters
      if (contextProperties != null) {
        for (Entry<String, Object> parameter : contextProperties.entrySet()) {
          String key = parameter.getKey();
          transformer.setParameter(
              key, evaluateTransformParameter(key, parameter.getValue(), message));
        }
      }

      transformer.transform(sourceDoc, result);

      if (errorListener.isError()) {
        throw errorListener.getException();
      }
    } finally {
      if (transformer != null) {
        transformerPool.returnObject(transformer);
      }
    }
  }
 protected void stopPool(GenericObjectPool pool) {
   pool.clear();
   try {
     pool.close();
   } catch (Exception e) {
     _log.error("ERROR >> Failed to close pool ", e);
   }
 }
Exemplo n.º 5
0
 public XsltTransformer() {
   super();
   transformerPool = new GenericObjectPool(new PooledXsltTransformerFactory());
   transformerPool.setMinIdle(MIN_IDLE_TRANSFORMERS);
   transformerPool.setMaxIdle(MAX_IDLE_TRANSFORMERS);
   transformerPool.setMaxActive(MAX_ACTIVE_TRANSFORMERS);
   contextProperties = new HashMap<String, Object>();
 }
Exemplo n.º 6
0
 public static DBI getDbi() {
   log.debug("connection pool info");
   log.debug(
       "Max: {} Active: {}, Idle: {}",
       connectionPool.getMaxActive(),
       connectionPool.getNumActive(),
       connectionPool.getNumIdle());
   return dbi;
 }
  private static synchronized void initialize(String poolname) {
    if (!isInitialized) {
      // create worker pool
      workerPool = new GenericObjectPool(new RWorkerObjectFactory(poolname));
      workerPool.setMaxActive(4);
      workerPool.setMaxIdle(4);
      workerPool.setTestOnBorrow(true);
      workerPool.setTestOnReturn(true);

      isInitialized = true;
    }
  }
Exemplo n.º 8
0
 public void verifyDbResourcesReleased() throws IOException, SQLException {
   // make sure there are no active connections
   PoolingDataSource ds = (PoolingDataSource) jdbcHelper.getDataSource();
   GenericObjectPool pool = TestUtils.getField(ds, "_pool", GenericObjectPool.class);
   assertEquals(
       pool.getNumActive(),
       0,
       "Found "
           + pool.getNumActive()
           + " active connections after test ended:\n"
           + TestUtils.invokeMethodNoArgs(pool, "debugInfo"));
   artifactoryHomeBoundTest.unbindArtifactoryHome();
 }
Exemplo n.º 9
0
  /** @see org.geotools.arcsde.session.ISessionPool#getSession(boolean) */
  public ISession getSession(final boolean transactional)
      throws IOException, UnavailableConnectionException {
    checkOpen();
    try {
      Session connection = null;
      if (transactional) {
        LOGGER.finest("Borrowing session from pool for transactional access");
        connection = (Session) pool.borrowObject();
      } else {
        synchronized (openSessionsNonTransactional) {
          try {
            if (LOGGER.isLoggable(Level.FINER)) {
              LOGGER.finer("Grabbing session from pool on " + Thread.currentThread().getName());
            }
            connection = (Session) pool.borrowObject();
            if (LOGGER.isLoggable(Level.FINER)) {
              LOGGER.finer("Got session from the pool on " + Thread.currentThread().getName());
            }
          } catch (NoSuchElementException e) {
            if (LOGGER.isLoggable(Level.FINER)) {
              LOGGER.finer("No available sessions in the pool, falling back to queued session");
            }
            connection = openSessionsNonTransactional.remove();
          }

          openSessionsNonTransactional.add(connection);

          if (LOGGER.isLoggable(Level.FINER)) {
            LOGGER.finer(
                "Got session from the in use queue on " + Thread.currentThread().getName());
          }
        }
      }

      connection.markActive();
      return connection;

    } catch (NoSuchElementException e) {
      LOGGER.log(
          Level.WARNING, "Out of connections: " + e.getMessage() + ". Config: " + this.config);
      throw new UnavailableConnectionException(config.getMaxConnections(), this.config);
    } catch (SeException se) {
      ArcSdeException sdee = new ArcSdeException(se);
      LOGGER.log(Level.WARNING, "ArcSDE error getting connection for " + config, sdee);
      throw sdee;
    } catch (Exception e) {
      LOGGER.log(Level.WARNING, "Unknown problem getting connection: " + e.getMessage(), e);
      throw (IOException)
          new IOException("Unknown problem fetching connection from connection pool").initCause(e);
    }
  }
Exemplo n.º 10
0
 @Override
 public String toString() {
   StringBuilder ret = new StringBuilder(getClass().getSimpleName());
   ret.append("[config=").append(getConfig());
   if (pool == null) {
     ret.append("[Session pool is disposed]");
   } else {
     ret.append("[ACTIVE: ");
     ret.append(pool.getNumActive() + "/" + ((GenericObjectPool) pool).getMaxActive());
     ret.append(" INACTIVE: ");
     ret.append(pool.getNumIdle() + "/" + ((GenericObjectPool) pool).getMaxIdle() + "]");
   }
   ret.append("]");
   return ret.toString();
 }
  public static synchronized void releaseResources() {
    log.debug("Releasing resources...");
    if (isInitialized) {
      try {
        if (workerPool.getNumActive() > 0) {
          log.warn("Shutting down even though there are still some active compute workers");
        }

        workerPool.clear();
        workerPool.close();
      } catch (Exception e) {
        log.error("Problem shutting down compute service", e);
      }
    }
  }
Exemplo n.º 12
0
 public void returnBrokenResource(final T resource) {
   try {
     internalPool.invalidateObject(resource);
   } catch (Exception e) {
     throw new JedisException("Could not return the resource to the pool", e);
   }
 }
 public Parser get() {
   try {
     return (Parser) pool.borrowObject();
   } catch (Exception e) {
     throw new RuntimeException("Error borrowing a parser from the pool", e);
   }
 }
 public void put(Parser parser) {
   try {
     pool.returnObject(parser);
   } catch (Exception e) {
     throw new RuntimeException("Error returning a parser to the pool", e);
   }
 }
Exemplo n.º 15
0
 public void destroy() {
   try {
     internalPool.close();
   } catch (Exception e) {
     throw new JedisException("Could not destroy the pool", e);
   }
 }
Exemplo n.º 16
0
  public static void setUp(String driverName, String dbUrl, String userName, String password)
      throws Exception {
    //
    // Load JDBC Driver class.
    //
    Class.forName(driverName).newInstance();

    //
    // Creates an instance of GenericObjectPool that holds our
    // pool of connections object.
    //
    connectionPool = new GenericObjectPool();
    connectionPool.setMaxActive(10);

    //
    // Creates a connection factory object which will be use by
    // the pool to create the connection object. We passes the
    // JDBC url info, username and password.
    //
    ConnectionFactory cf = new DriverManagerConnectionFactory(dbUrl, userName, password);

    //
    // Creates a PoolableConnectionFactory that will wraps the
    // connection object created by the ConnectionFactory to add
    // object pooling functionality.
    //
    PoolableConnectionFactory pcf =
        new PoolableConnectionFactory(cf, connectionPool, null, null, false, true);
    PoolingDataSource poolingDataSource = new PoolingDataSource(connectionPool);
    dbi = new DBI(poolingDataSource);
  }
Exemplo n.º 17
0
 public void close() throws Exception {
   if (mConnectionPool != null) {
     mConnectionPool.close();
     mConnectionPool = null;
   }
   mDataSource = null;
 }
  public static RServices createRServices(String poolname) {
    // lazily initialize servant provider
    initialize(poolname);

    log.trace(
        "Worker pool before borrow: [active = {}, idle = {}]",
        workerPool.getNumActive(),
        workerPool.getNumIdle());
    try {
      return tryBorrowAWorker(5, 20, TimeUnit.SECONDS);
    } finally {
      log.trace(
          "Worker pool after borrow: [active = {}, idle = {}]",
          workerPool.getNumActive(),
          workerPool.getNumIdle());
    }
  }
Exemplo n.º 19
0
 @SuppressWarnings("unchecked")
 public T getResource() {
   try {
     return (T) internalPool.borrowObject();
   } catch (Exception e) {
     throw new JedisConnectionException("Could not get a resource from the pool", e);
   }
 }
Exemplo n.º 20
0
 @Override
 protected void doStop() throws Exception {
   super.doStop();
   if (consumers != null) {
     consumers.close();
     consumers = null;
   }
 }
Exemplo n.º 21
0
  /** Initializes DatabaseFactory. */
  public static synchronized void init() {
    if (dataSource != null) {
      return;
    }

    DatabaseConfig.load();

    try {
      DatabaseConfig.DATABASE_DRIVER.newInstance();
    } catch (Exception e) {
      log.fatal("Error obtaining DB driver", e);
      throw new Error("DB Driver doesnt exist!");
    }

    connectionPool = new GenericObjectPool();

    if (DatabaseConfig.DATABASE_CONNECTIONS_MIN > DatabaseConfig.DATABASE_CONNECTIONS_MAX) {
      log.error(
          "Please check your database configuration. Minimum amount of connections is > maximum");
      DatabaseConfig.DATABASE_CONNECTIONS_MAX = DatabaseConfig.DATABASE_CONNECTIONS_MIN;
    }

    connectionPool.setMaxIdle(DatabaseConfig.DATABASE_CONNECTIONS_MIN);
    connectionPool.setMaxActive(DatabaseConfig.DATABASE_CONNECTIONS_MAX);

    /* test if connection is still valid before returning */
    connectionPool.setTestOnBorrow(true);

    try {
      dataSource = setupDataSource();
      Connection c = getConnection();
      DatabaseMetaData dmd = c.getMetaData();
      databaseName = dmd.getDatabaseProductName();
      databaseMajorVersion = dmd.getDatabaseMajorVersion();
      databaseMinorVersion = dmd.getDatabaseMinorVersion();
      c.close();
    } catch (Exception e) {
      log.fatal("Error with connection string: " + DatabaseConfig.DATABASE_URL, e);
      throw new Error("DatabaseFactory not initialized!");
    }

    log.info("Successfully connected to database");
  }
Exemplo n.º 22
0
  /** Create dataSource object using given parameters */
  public DataSource setupDataSource(
      String connectionURL, String username, String password, int minIdle, int maxActive)
      throws Exception {
    GenericObjectPool connectionPool = new GenericObjectPool(null);
    connectionPool.setMinIdle(minIdle);
    connectionPool.setMaxActive(maxActive);
    connectionPool.setMaxWait(1000 * 60);

    pool = connectionPool;
    ConnectionFactory connectionFactory =
        new DriverManagerConnectionFactory(connectionURL, username, password);
    PoolableConnectionFactory factory =
        new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);

    pool.setFactory(factory);
    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    return dataSource;
  }
Exemplo n.º 23
0
  /** Shuts down pool and closes connections */
  public static synchronized void shutdown() {
    try {
      connectionPool.close();
    } catch (Exception e) {
      log.warn("Failed to shutdown DatabaseFactory", e);
    }

    // set datasource to null so we can call init() once more...
    dataSource = null;
  }
Exemplo n.º 24
0
 public <T> T createProxy(Class<T> serviceClass) {
   NettyClient nettyClient = null;
   try {
     nettyClient = nettyClientPool.borrowObject();
     return nettyClient.createProxy(serviceClass);
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
     throw new NettyException(e.getMessage(), e);
   } finally {
     if (nettyClient != null) {
       try {
         nettyClientPool.returnObject(nettyClient);
       } catch (Exception e) {
         logger.error(e.getMessage(), e);
         throw new NettyException(e.getMessage(), e);
       }
     }
   }
 }
 protected void startPool(GenericObjectPool pool, int init_pool_size) {
   for (int i = 0; i < init_pool_size; i++) {
     try {
       pool.addObject();
     } catch (Exception e) {
       _log.error("<<ERROR>>", e);
     }
   }
   _log.info("Pool started with " + init_pool_size + " JRuby Runtimes!");
 }
Exemplo n.º 26
0
 protected synchronized ObjectPool getFtpPool(UMOEndpointURI uri) {
   String key =
       uri.getUsername() + ":" + uri.getPassword() + "@" + uri.getHost() + ":" + uri.getPort();
   ObjectPool pool = (ObjectPool) pools.get(key);
   if (pool == null) {
     pool = new GenericObjectPool(new FtpConnectionFactory(uri));
     ((GenericObjectPool) pool).setTestOnBorrow(this.validateConnections);
     pools.put(key, pool);
   }
   return pool;
 }
Exemplo n.º 27
0
 /*
  * (non-Javadoc)
  *
  * @see org.geotools.arcsde.session.ISessionPool#close()
  */
 public void close() {
   if (pool != null) {
     try {
       pool.close();
       pool = null;
       LOGGER.fine("SDE connection pool closed. ");
     } catch (Exception e) {
       LOGGER.log(Level.WARNING, "Closing pool: " + e.getMessage(), e);
     }
   }
 }
  public static GenericObjectPool getInstance(
      String poolName, String driver, final String url, final String user, final String password) {

    String key = driver + "%" + poolName + "%" + url + "%" + user + "%" + password;

    // log.info("getInstance-key="+key);

    if (_pool.get(key) != null) return _pool.get(key);

    synchronized (lock) {
      if (_pool.get(key) == null) {
        try {
          Class.forName(driver);

          DBLayerInterface dbLayer =
              DBLayer.getLayer(
                  getDBType(url),
                  new ConnectionProvider() {
                    public Connection newConnection() throws SQLException {
                      return DriverManager.getConnection(url, user, password);
                    }
                  });

          GenericObjectPool p = new ServerObjectPool(new ServantProxyFactoryDB(poolName, dbLayer));

          _pool.put(key, p);
          p.setMaxIdle(0);
          p.setMaxActive(-1);
          p.setTestOnBorrow(true);
          p.setTestOnReturn(true);
        } catch (Exception e) {
          throw new RuntimeException(getStackTraceAsString(e));
        }
      }
      return _pool.get(key);
    }
  }
 public static void recycleRServices(RServices rServices) {
   log.trace("Recycling R services");
   log.trace(
       "Worker pool before return... "
           + "active = "
           + workerPool.getNumActive()
           + ", idle = "
           + workerPool.getNumIdle());
   try {
     if (rServices != null) {
       workerPool.returnObject(rServices);
       log.trace(
           "Worker pool after return... "
               + "active = "
               + workerPool.getNumActive()
               + ", idle = "
               + workerPool.getNumIdle());
     } else {
       log.error("R services object became unexpectedly null!");
     }
   } catch (Exception e) {
     log.error("Failed to release an RServices object back into the pool of workers", e);
   }
 }
Exemplo n.º 30
0
  @Override
  protected void doInitialise() throws InitialisationException {
    logger.debug("Initialising transformer: " + this);
    try {
      // Only load the file once at initialize time
      if (xslFile != null) {
        this.xslt = IOUtils.getResourceAsString(xslFile, getClass());
      }

      if (uriResolver == null) {
        this.uriResolver = new LocalURIResolver(xslFile);
      }

      transformerPool.addObject();
    } catch (Throwable te) {
      throw new InitialisationException(te, this);
    }
  }