Search in sources :

Example 26 with IPromise

use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.

the class DataShard method init.

@Override
public IPromise init(ConnectableActor gravityConnectable, ServiceArgs options, boolean auto) /*ignored*/
{
    IPromise p = new Promise();
    try {
        super.init(gravityConnectable, options, false).await();
        initTableSpace();
        registerAtGravity();
        p.resolve();
    } catch (Throwable t) {
        p.reject(t);
    }
    return p;
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise)

Example 27 with IPromise

use of org.nustaq.kontraktor.IPromise 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 28 with IPromise

use of org.nustaq.kontraktor.IPromise 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 29 with IPromise

use of org.nustaq.kontraktor.IPromise 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 30 with IPromise

use of org.nustaq.kontraktor.IPromise 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

IPromise (org.nustaq.kontraktor.IPromise)58 Promise (org.nustaq.kontraktor.Promise)56 Remoted (org.nustaq.kontraktor.annotations.Remoted)6 Actor (org.nustaq.kontraktor.Actor)5 JsonObject (com.eclipsesource.json.JsonObject)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 ByteBuffer (java.nio.ByteBuffer)3 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 CertificateException (java.security.cert.CertificateException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Consumer (java.util.function.Consumer)3 TableSpaceActor (org.nustaq.reallive.impl.tablespace.TableSpaceActor)3 JsonValue (com.eclipsesource.json.JsonValue)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)2 HttpPost (org.apache.http.client.methods.HttpPost)2 StringEntity (org.apache.http.entity.StringEntity)2