nstream.adapter.mongodb.MongoDbPublishingAgent Maven / Gradle / Ivy
Show all versions of nstream-adapter-mongodb Show documentation
// 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 nstream.adapter.common.egress.PublisherAgent;
import nstream.adapter.common.provision.ProvisionLoader;
import org.bson.Document;
import swim.structure.Value;
/**
* An abstract Web Agent that can publish documents to a MongoDB collection.
*
* The {@link MongoDbPublishingAgent} loads agent configuration into a settings
* object on agent start.
* Subclasses can use the configured {@link MongoClient} and settings to publish
* state.
*
* Publication is done asynchronously of the agent thread.
*
* @see MongoDbEgressSettings
*/
public abstract class MongoDbPublishingAgent extends PublisherAgent {
protected MongoClient client;
public MongoDbPublishingAgent() {
}
/**
* Assign the {@link MongoClient} to be used for publication.
*
* @param client the Mongo client
*/
protected void assignClient(MongoClient client) {
this.client = client;
}
@Override
protected MongoDbEgressSettings parseEgressSettings(Value prop) {
final MongoDbEgressSettings settings = MongoDbEgressSettings.form().cast(prop);
return settings == null ? MongoDbEgressSettings.defaultSettings() : settings;
}
@Override
protected void stagePublication() {
loadSettings("mongoDbEgressConf");
assignClient(ProvisionLoader.getProvision(this.egressSettings.clientProvisionName())
.value());
info(nodeUri() + ": successfully assigned client for publication");
}
/**
* Stage publication on agent start.
*/
@Override
public void didStart() {
info(nodeUri() + ": didStart");
stagePublication();
}
/**
* Close any open resources on agent close.
*/
@Override
public void willStop() {
info(nodeUri() + ": willStop");
}
}