All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nstream.adapter.mongodb.ClientProvision Maven / Gradle / Ivy

There is a newer version: 4.13.21
Show newest version
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://redis.com/legal/rsalv2-agreement/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nstream.adapter.mongodb;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import java.util.Properties;
import nstream.adapter.common.provision.AbstractProvision;
import swim.util.Log;

/**
 * Provision providing a {@link MongoClient} object.
 * Should be concurrently used across agents as {@link MongoClient}
 * provides connection pooling.
 * Required properties:
 * 
    *
  • connectionString - connection string of the mongo database
  • *
*/ public class ClientProvision extends AbstractProvision { public ClientProvision() { } @Override public void loadValue(Log log, Properties properties) { final String connectionString = properties.getProperty("connectionString"); if (connectionString == null || connectionString.isEmpty()) { log.error("Did not load ClientProvision due to empty clientProvisionName property"); throw new IllegalArgumentException("ClientProvision config must include nonempty connectionString"); } try { this.value = MongoClients.create(connectionString); } catch (RuntimeException e) { log.error("Did not load ClientProvision due to issue loading MongoClient from properties"); throw new RuntimeException("Failed to load ClientProvision", e); } } @Override public void unloadValue(Log log) { try { this.value.close(); } catch (RuntimeException e) { log.error("Failed to close MongoClient (" + e.getMessage() + ")"); throw new RuntimeException("Failed to close MongoClient", e); } this.value = null; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy