Search in sources :

Example 31 with Promise

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Promise(org.nustaq.kontraktor.Promise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) Test(org.junit.Test)

Example 32 with Promise

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);
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) ByteBuffer(java.nio.ByteBuffer)

Example 33 with Promise

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;
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) Actor(org.nustaq.kontraktor.Actor) ByteBuffer(java.nio.ByteBuffer)

Example 34 with Promise

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;
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) Actor(org.nustaq.kontraktor.Actor) ByteBuffer(java.nio.ByteBuffer)

Example 35 with Promise

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);
    }
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) Properties(java.util.Properties) Date(java.util.Date) Session(javax.mail.Session)

Aggregations

Promise (org.nustaq.kontraktor.Promise)67 IPromise (org.nustaq.kontraktor.IPromise)62 Test (org.junit.Test)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Actor (org.nustaq.kontraktor.Actor)6 Remoted (org.nustaq.kontraktor.annotations.Remoted)6 IOException (java.io.IOException)4 Actors (org.nustaq.kontraktor.Actors)4 RateMeasure (org.nustaq.kontraktor.util.RateMeasure)4 JsonObject (com.eclipsesource.json.JsonObject)3 ByteBuffer (java.nio.ByteBuffer)3 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 CertificateException (java.security.cert.CertificateException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Consumer (java.util.function.Consumer)3 ActorServer (org.nustaq.kontraktor.remoting.base.ActorServer)3 TCPConnectable (org.nustaq.kontraktor.remoting.tcp.TCPConnectable)3 TableSpaceActor (org.nustaq.reallive.impl.tablespace.TableSpaceActor)3 EOFException (java.io.EOFException)2