|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 5 of 6
In point-to-point messaging, a message is sent from a publisher directly to a consumer via a queue. Point-to-point messaging is a good choice when you want a message consumed exactly once, or when two components want to communicate with each other asynchronously. A point-to-point messaging architecture is shown in Figure 3.
We'll use Vert.x to explore both messaging systems in the next sections.
Listing 4 updates my original Server class (from Listing 1) in a few ways. First, it deploys a new verticle called AuditVerticle (which is defined in Listing 5) by invoking the deployVerticle() method on the container instance. The container instance, which is defined as part of the parent Verticle class, provides access to the container in which a verticle runs; therefore, it is the appropriate place to deploy new verticles.
package com.geekcap.vertxexamples;
import org.vertx.java.core.Handler;
import org.vertx.java.core.eventbus.EventBus;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.logging.Logger;
import org.vertx.java.platform.Verticle;
public class Server extends Verticle {
public void start() {
// Create our dependent verticles
container.deployVerticle("com.geekcap.vertxexamples.AuditVerticle");
// Create an HTTP Server that serves files
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
public void handle(HttpServerRequest req) {
Logger logger = container.logger();
if (logger.isDebugEnabled()) {
logger.debug("Received a request for resource: " + req.path());
}
logger.fatal("Where are my logs!?!?");
logger.info("Here is an info message");
// Serve up our files
String file = req.path().equals("/") ? "index.html" : req.path();
req.response().sendFile("webroot/" + file);
// Let's tell the world (via the event bus) that we received a request
EventBus eb = vertx.eventBus();
eb.publish( "com.geekcap.vertxexamples.Server.announcements", "We received a request for resource: " + req.path() );
}
}).listen(8080);
}
}
Listing 4 executes deployVerticle() to deploy the AuditVerticle. The deployVerticle() method deploys a standard Verticle to the container, which maintains its own event-loop. After handling an incoming HTTP request (as shown in Listing 1), Listing
4 publishes a message to the event bus. First, it obtains access to the event bus through the vertx instance variable, then it executes the eventBus() method. Once it has the EventBus object it invokes its publish() method, which is the gateway to publishing messages in a publish/subscribe fashion.
For the past three years I have worked in an event-driven architecture, and I have found that publish/subscribe messaging, sometimes called topics, is a great way to loosely couple systems. Message publishers do not need to know anything about their subscribers, so new subscribers can be added at any time without affecting the publisher.