nstream.adapter.nats.NatsPublishingAgent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nstream-adapter-nats Show documentation
Show all versions of nstream-adapter-nats Show documentation
Templates for consuming from and producing to NATS with Swim
The 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.nats;
import io.nats.client.Connection;
import io.nats.client.JetStream;
import nstream.adapter.common.egress.PublisherAgent;
import nstream.adapter.common.provision.ProvisionLoader;
import swim.structure.Value;
public abstract class NatsPublishingAgent extends PublisherAgent {
protected volatile Connection natsConnection;
protected volatile String natsStream;
protected volatile String natsSubject;
protected volatile JetStream jetStream;
public NatsPublishingAgent() {
}
protected void assignSettings(String natsStream, String natsSubject) {
this.natsStream = natsStream;
this.natsSubject = natsSubject;
}
@Override
protected NatsEgressSettings parseEgressSettings(Value prop) {
final NatsEgressSettings settings = NatsEgressSettings.form().cast(prop);
return settings == null ? NatsEgressSettings.defaultSettings() : settings;
}
@Override
protected void publish(byte[] msg) {
try {
trace(nodeUri() + ": Will publish message with bytesize " + msg.length);
this.natsConnection.publish(this.natsSubject, msg);
trace(nodeUri() + ": Published message");
} catch (Exception e) {
throw new RuntimeException(nodeUri() + ": Failed to publish message with body " + new String(msg), e);
}
}
@Override
protected void stagePublication() {
loadSettings("natsEgressConf");
try {
this.natsConnection = ProvisionLoader
.getProvision(this.egressSettings.getConnectionProvisionName())
.value();
this.natsStream = this.egressSettings.getNatsEgressStreamName();
this.natsSubject = this.egressSettings.getNatsEgressSubjectName();
// // Get JetStream context
// JetStreamManagement jsm = natsConnection.jetStreamManagement();
// // TODO: Check if stream exists already
// StreamConfiguration streamConfig = StreamConfiguration.builder()
// .name(this.natsStream)
// .subjects(this.natsSubject)
// .storageType(StorageType.Memory)
// .build();
// jsm.addStream(streamConfig);
// // Get JetStream context for publishing
// this.jetStream = natsConnection.jetStream();
} catch (Exception e) {
throw new RuntimeException(nodeUri() + ": Failed to stage publication", e);
}
}
/**
* Stage publication on agent start.
*/
@Override
public void didStart() {
info(nodeUri() + ": didStart");
stagePublication();
}
}