use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.
the class ServiceActor method awaitRequiredServices.
public IPromise awaitRequiredServices() {
Log.Info(this, "connecting required services ..");
if (requiredServices.size() == 0) {
return resolve();
}
IPromise res = new Promise<>();
gravity.getServiceMap().then((smap, err) -> {
List<IPromise<Object>> servicePromis = new ArrayList();
String[] servNames = getAllServiceNames();
for (int i = 0; i < servNames.length; i++) {
String servName = servNames[i];
ServiceDescription serviceDescription = smap.get(servName);
if (serviceDescription != null && requiredServices.get(servName) instanceof Actor == false) {
if (serviceDescription.getConnectable() == null) {
Log.Error(this, "No connecteable defined for service " + serviceDescription.getName());
}
IPromise connect;
try {
Log.Info(this, "connect " + serviceDescription.getConnectable());
connect = serviceDescription.getConnectable().connect();
} catch (Throwable th) {
Log.Error(this, th, "failed to connect " + serviceDescription.getName());
continue;
}
Promise notify = new Promise();
servicePromis.add(notify);
connect.then((actor, connectionError) -> {
if (actor != null) {
requiredServices.put(servName, actor);
notify.complete();
} else {
requiredServices.put(servName, UNCONNECTED);
Log.Warn(this, "failed to connect " + servName + " " + connectionError + " " + serviceDescription.getConnectable());
notify.reject("failed to connect " + servName + " " + connectionError);
}
});
} else {
res.reject("required service " + servName + " not registered.");
return;
}
}
if (!res.isSettled()) {
all(servicePromis).timeoutIn(15000).then(res).onTimeout(() -> {
// todo:retry
Log.Info(this, "failed to connect required services, retry");
});
}
});
return res;
}
use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.
the class KxStreamServer method main.
public static void main(String[] args) {
// give fat queue
KxStreamServer server = Actors.AsActor(KxStreamServer.class, 256000);
server.init();
TCPNIOPublisher publisher = new TCPNIOPublisher(server, 7890);
publisher.publish(actor -> System.out.println("actor " + actor + " disconnected")).await();
System.out.println("server started at " + publisher);
}
use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.
the class EventSink method subscribe.
@Override
public void subscribe(Subscriber<? super T> subscriber) {
if (subs != null) {
throw new RuntimeException("only one subscription supported");
}
if (subscriber == null) {
throw null;
}
subs = subscriber;
if (subs instanceof Actor) {
actorSubs = (Actor) subs;
}
subscriber.onSubscribe(new Subscription() {
@Override
public void request(long l) {
if (l <= 0) {
subs.onError(new IllegalArgumentException("spec rule 3.9: request > 0 elements"));
}
credits.addAndGet(l);
}
@Override
public void cancel() {
subs = null;
canceled = true;
}
});
}
use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.
the class AsyncFile method open.
public void open(Path file, OpenOption... options) throws IOException {
if (fileChannel != null)
throw new RuntimeException("can only open once");
Actor sender = Actor.current();
Set<OpenOption> set = new HashSet<OpenOption>(options.length);
Collections.addAll(set, options);
fileChannel = AsynchronousFileChannel.open(file, set, new ActorExecutorService(sender), NO_ATTRIBUTES);
}
use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.
the class AsyncFile method write.
public IPromise<AsyncFileIOEvent> write(long filePosition, ByteBuffer source) {
if (fileChannel == null)
throw new RuntimeException("file not opened");
Actor sender = Actor.current();
Promise p = new Promise();
final long bufferStartPos = source.position();
final ByteBuffer finalTarget = source;
fileChannel.write(source, filePosition, source, new CompletionHandler<Integer, ByteBuffer>() {
@Override
public void completed(Integer result, ByteBuffer attachment) {
if (source.remaining() > 0) {
// just retry (will enqueue new message/job to actor mailbox)
fileChannel.write(source, filePosition, source, this);
} else {
long newPos = filePosition + finalTarget.limit() - bufferStartPos;
if (result < 0)
newPos = -1;
attachment.flip();
p.resolve(new AsyncFileIOEvent(newPos, result, finalTarget));
}
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
p.reject(exc);
}
});
return p;
}
Aggregations