use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class RxJava method rxToJ8StreamTest.
@Test
public void rxToJ8StreamTest() {
Observable<Integer> range = Observable.range(0, NUM_MSG / 4);
Publisher<Integer> pub = RxReactiveStreams.toPublisher(range);
AtomicInteger count = new AtomicInteger(0);
RateMeasure rm = new RateMeasure("events");
Promise<Integer> finished = new Promise<>();
KxReactiveStreams.get().asKxPublisher(pub).async().stream(stream -> {
stream.forEach(i -> {
rm.count();
count.incrementAndGet();
});
finished.resolve(count.get());
});
Assert.assertTrue(finished.await(50000).intValue() == NUM_MSG / 4);
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class AsyncFile method readFully.
public IPromise<AsyncFileIOEvent> readFully() {
ByteBuffer buf = ByteBuffer.allocate((int) length());
AsyncFileIOEvent ev = new AsyncFileIOEvent(0, 0, buf);
do {
ev = read(ev.nextPosition, (int) ((int) length() - ev.nextPosition), buf).await();
} while (buf.limit() != buf.capacity() && ev.getNextPosition() >= 0);
return new Promise<>(ev);
}
use of org.nustaq.kontraktor.Promise 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;
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class AsyncFile method read.
public IPromise<AsyncFileIOEvent> read(long position, int chunkSize, ByteBuffer target) {
if (fileChannel == null)
throw new RuntimeException("file not opened");
Actor sender = Actor.current();
Promise p = new Promise();
if (target == null) {
target = ByteBuffer.allocate(chunkSize);
}
final long bufferStartPos = target.position();
final ByteBuffer finalTarget = target;
fileChannel.read(target, position, target, new CompletionHandler<Integer, ByteBuffer>() {
@Override
public void completed(Integer result, ByteBuffer attachment) {
// FIXME: how to handle incomplete read. (currently burden on reader)
long newPos = position + 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;
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class Mailer method sendMail.
/**
* @param receiver - the mail receiver
* @param subject - subject of the mail
* @param content - mail content
* @param displayName - display name shown instead of the sender email ..
* @return promise ..
*/
public IPromise<Boolean> sendMail(String receiver, String subject, String content, String displayName) /* Sender Name*/
{
if (receiver == null || !receiver.contains("@")) {
return new Promise<>(false, "Not a valid email address: " + receiver);
}
try {
Properties props = new Properties();
props.put("mail.smtp.auth", settings.getSmtpAuth());
props.put("mail.smtp.starttls.enable", settings.getStartTls());
props.put("mail.smtp.host", settings.getSmtpHost());
props.put("mail.smtp.port", settings.getSmtpPort());
Session session = Session.getInstance(props);
MimeMessage message = new MimeMessage(session);
message.setFrom(displayName == null ? new InternetAddress("support@juptr.io") : new InternetAddress("support@juptr.io", displayName));
message.setSubject(subject);
message.setText(content, "utf-8", "html");
message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiver, false));
message.setSentDate(new Date());
Transport.send(message, settings.getUser(), settings.getPassword());
Log.Info(this, "definitely sent mail to " + receiver + " subject:" + subject);
return new Promise<>(true);
} catch (Exception e) {
Log.Warn(this, e);
return new Promise<>(false, e);
}
}
Aggregations