Search in sources :

Example 1 with Vertx

use of io.vertx.core.Vertx in project vert.x by eclipse.

the class StreamsExamples method pump2.

public void pump2(Vertx vertx) {
    NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost("localhost"));
    server.connectHandler(sock -> {
        sock.handler(buffer -> {
            if (!sock.writeQueueFull()) {
                sock.write(buffer);
            }
        });
    }).listen();
}
Also used : NetServerOptions(io.vertx.core.net.NetServerOptions) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Vertx(io.vertx.core.Vertx) Pump(io.vertx.core.streams.Pump) Handler(io.vertx.core.Handler) NetSocket(io.vertx.core.net.NetSocket) NetServerOptions(io.vertx.core.net.NetServerOptions) NetServer(io.vertx.core.net.NetServer)

Example 2 with Vertx

use of io.vertx.core.Vertx in project vert.x by eclipse.

the class StreamsExamples method pump1.

public void pump1(Vertx vertx) {
    NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost("localhost"));
    server.connectHandler(sock -> {
        sock.handler(buffer -> {
            sock.write(buffer);
        });
    }).listen();
}
Also used : NetServerOptions(io.vertx.core.net.NetServerOptions) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Vertx(io.vertx.core.Vertx) Pump(io.vertx.core.streams.Pump) Handler(io.vertx.core.Handler) NetSocket(io.vertx.core.net.NetSocket) NetServerOptions(io.vertx.core.net.NetServerOptions) NetServer(io.vertx.core.net.NetServer)

Example 3 with Vertx

use of io.vertx.core.Vertx in project vert.x by eclipse.

the class FileSystemExamples method asyncFileWrite.

public void asyncFileWrite() {
    Vertx vertx = Vertx.vertx();
    vertx.fileSystem().open("target/classes/hello.txt", new OpenOptions(), result -> {
        if (result.succeeded()) {
            AsyncFile file = result.result();
            Buffer buff = Buffer.buffer("foo");
            for (int i = 0; i < 5; i++) {
                file.write(buff, buff.length() * i, ar -> {
                    if (ar.succeeded()) {
                        System.out.println("Written ok!");
                    } else {
                        System.err.println("Failed to write: " + ar.cause());
                    }
                });
            }
        } else {
            System.err.println("Cannot open file " + result.cause());
        }
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) Buffer(io.vertx.core.buffer.Buffer) AsyncFile(io.vertx.core.file.AsyncFile) Vertx(io.vertx.core.Vertx)

Example 4 with Vertx

use of io.vertx.core.Vertx in project vert.x by eclipse.

the class FileSystemExamples method asyncAPIExamples.

public void asyncAPIExamples() {
    Vertx vertx = Vertx.vertx();
    // Read a file
    vertx.fileSystem().readFile("target/classes/readme.txt", result -> {
        if (result.succeeded()) {
            System.out.println(result.result());
        } else {
            System.err.println("Oh oh ..." + result.cause());
        }
    });
    // Copy a file
    vertx.fileSystem().copy("target/classes/readme.txt", "target/classes/readme2.txt", result -> {
        if (result.succeeded()) {
            System.out.println("File copied");
        } else {
            System.err.println("Oh oh ..." + result.cause());
        }
    });
    // Write a file
    vertx.fileSystem().writeFile("target/classes/hello.txt", Buffer.buffer("Hello"), result -> {
        if (result.succeeded()) {
            System.out.println("File written");
        } else {
            System.err.println("Oh oh ..." + result.cause());
        }
    });
    // Check existence and delete
    vertx.fileSystem().exists("target/classes/junk.txt", result -> {
        if (result.succeeded() && result.result()) {
            vertx.fileSystem().delete("target/classes/junk.txt", r -> {
                System.out.println("File deleted");
            });
        } else {
            System.err.println("Oh oh ... - cannot delete the file: " + result.cause());
        }
    });
}
Also used : Vertx(io.vertx.core.Vertx)

Example 5 with Vertx

use of io.vertx.core.Vertx in project vert.x by eclipse.

the class FileResolverTestBase method testCacheDirDeletedOnVertxClose.

@Test
public void testCacheDirDeletedOnVertxClose() {
    VertxInternal vertx2 = (VertxInternal) vertx();
    File file = vertx2.resolveFile(webRoot + "/somefile.html");
    assertTrue(file.exists());
    File cacheDir = file.getParentFile().getParentFile();
    assertTrue(cacheDir.exists());
    vertx2.close(onSuccess(v -> {
        assertFalse(cacheDir.exists());
        testComplete();
    }));
    await();
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) Files(java.nio.file.Files) Vertx(io.vertx.core.Vertx) StandardOpenOption(java.nio.file.StandardOpenOption) Test(org.junit.Test) Random(java.util.Random) File(java.io.File) ArrayList(java.util.ArrayList) FileResolver(io.vertx.core.impl.FileResolver) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Assert(org.junit.Assert) VertxInternal(io.vertx.core.impl.VertxInternal) File(java.io.File) Test(org.junit.Test)

Aggregations

Vertx (io.vertx.core.Vertx)43 Test (org.junit.Test)17 VertxOptions (io.vertx.core.VertxOptions)11 Buffer (io.vertx.core.buffer.Buffer)10 Handler (io.vertx.core.Handler)8 NetSocket (io.vertx.core.net.NetSocket)7 Pump (io.vertx.core.streams.Pump)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 DeploymentOptions (io.vertx.core.DeploymentOptions)6 VertxInternal (io.vertx.core.impl.VertxInternal)6 NetServer (io.vertx.core.net.NetServer)6 NetServerOptions (io.vertx.core.net.NetServerOptions)6 JsonObject (io.vertx.core.json.JsonObject)5 Launcher (io.vertx.core.Launcher)4 RunCommandTest (io.vertx.core.impl.launcher.commands.RunCommandTest)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 AbstractVerticle (io.vertx.core.AbstractVerticle)3 AsyncResult (io.vertx.core.AsyncResult)3