Skip to content

Commit

Permalink
Add eventbus example.
Browse files Browse the repository at this point in the history
  • Loading branch information
bertjan committed May 10, 2016
1 parent 5349db0 commit 9132da1
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 3 deletions.
18 changes: 18 additions & 0 deletions basic-eventbus/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>vertx3-examples</artifactId>
<groupId>nl.revolution</groupId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>basic-eventbus</artifactId>

<name>Vert.x 3 examples : Basic event bus</name>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package nl.revolution.vertx3.eventbus.basic;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

public class EchoServiceVerticle extends AbstractVerticle {

public static final String ADDRESS = "echo-service";
private static final Logger LOG = LoggerFactory.getLogger(EchoServiceVerticle.class);

@Override
public void start() {
LOG.info("Starting echo-service");

vertx.eventBus().consumer(EchoServiceVerticle.ADDRESS, message -> {
JsonObject messageBody = (JsonObject)message.body();
LOG.info("Received message: " + messageBody);

messageBody.put("passedThrough", "echo-service");
LOG.info("Sending reply: " + messageBody);

message.reply(messageBody);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package nl.revolution.vertx3.eventbus.basic;

import io.vertx.core.*;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.core.logging.SLF4JLogDelegateFactory;

public class VertxStarter extends AbstractVerticle {

public static void main(String... args) {
System.setProperty(LoggerFactory.LOGGER_DELEGATE_FACTORY_CLASS_NAME, SLF4JLogDelegateFactory.class.getName());

VertxOptions options = new VertxOptions().setClustered(true).setClusterHost("localhost");

Vertx.clusteredVertx(options, resultHandler -> {
Vertx vertx = resultHandler.result();
vertx.deployVerticle(new EchoServiceVerticle());
vertx.deployVerticle(new WebVerticle());
});

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package nl.revolution.vertx3.eventbus.basic;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

public class WebVerticle extends AbstractVerticle {

private static final Logger LOG = LoggerFactory.getLogger(WebVerticle.class);

@Override
public void start() {
LOG.info("Starting webVerticle");

vertx.createHttpServer().requestHandler(request -> {
JsonObject message = new JsonObject()
.put("text", "Hello world!")
.put("queryString", request.query());

LOG.info("Sending message: " + message);
vertx.eventBus().send(EchoServiceVerticle.ADDRESS, message, reply -> {

JsonObject replyBody = (JsonObject) reply.result().body();
LOG.info("Received reply: " + replyBody);

request.response().end(replyBody.encodePrettily());
});

}).listen(8080);
}
}
19 changes: 19 additions & 0 deletions logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender" level="INFO">
<encoder>
<pattern>[%-5level] %logger{15} - %msg%n</pattern>
</encoder>
</appender>

<logger name="io.vertx" level="INFO">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="com.hazelcast" level="ERROR">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="io.netty.util.internal.PlatformDependent" level="ERROR">
<appender-ref ref="STDOUT"/>
</logger>
<root level="INFO">
</root>
</configuration>
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
<artifactId>groovy-all</artifactId>
<version>2.3.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.5</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -74,6 +80,7 @@
<modules>
<module>helloworld</module>
<module>sockjs-eventbus</module>
<module>basic-eventbus</module>
</modules>

</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nl.revolution.vertx3.eventbus;
package nl.revolution.vertx3.eventbus.sockjs;

import io.vertx.core.AbstractVerticle;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nl.revolution.vertx3.eventbus;
package nl.revolution.vertx3.eventbus.sockjs;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.logging.Logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package nl.revolution.vertx3.eventbus;
package nl.revolution.vertx3.eventbus.sockjs;

import io.vertx.core.*;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.core.logging.SLF4JLogDelegateFactory;

public class VertxStarter extends AbstractVerticle {

public static void main(String... args) {
System.setProperty(LoggerFactory.LOGGER_DELEGATE_FACTORY_CLASS_NAME, SLF4JLogDelegateFactory.class.getName());

VertxOptions options = new VertxOptions().setClustered(true).setClusterHost("localhost");

Vertx.clusteredVertx(options, resultHandler -> {
Expand Down

0 comments on commit 9132da1

Please sign in to comment.