Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

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

Open source Java projects: Vert.x

Enterprise messaging and integration with Vert.x

  • Print
  • Feedback

Page 4 of 6

mvn clean install

This will yield a file named vertx-examples-1.0-SNAPSHOT.jar that will need to be in your CLASSPATH in order to launch your verticle. This sample application serves up web resources found in the webroot directory relative to where the application is launched. You'll therefore need to create a webroot directory and build some resources to serve from it. To launch this verticle, execute the vertx application in Vert.x's bin directory, like so:

$VERTX_HOME/bin/vertx run com.geekcap.vertxexamples.Server -cp vertx-examples-1.0-SNAPSHOT.jar

The vertx command accepts several options including the one we're using, which is run. The run option requires a verticle called main, which is just the name of the class that extends Verticle and contains a start() method and an optional set of arguments. In this case we set the CLASSPATH using the -cp argument and passing in the JAR file that we just created. The server will start without outputting anything to the screen, but you can point your browser to the URL: http://localhost:8080.

For my example I created a simple HTML file that says "Hello, Vert.x," named it index.html and placed it in my webroot directory. Here is my output:

$ curl http://localhost:8080

<html>
<head><title>Hello, Vert.x</title></head>
<body>
<p>Hello, Vert.x</p>
</body>
</html>

Messaging with Vert.x

One of the most important features of Vert.x is its event bus. The Vert.x event bus allows verticles, potentially written in different programming languages, to communicate with one another using either point-to-point messaging or publish/subscribe messaging. In this section you'll get a feel for how to integrate functionality from different verticles using both approaches.

Before we begin, why would you want to use messaging over a more traditional event-based programming model? For one thing, messaging supports the integration of applications and components written in different programming languages. It also enables loose coupling, which means that you can write multiple task-focused pieces of code rather than a single, complex program. Finally, asynchronous communication between verticles increases system scalability. Asynchronous communication enables you to define the system capacity as it evolves. Messages might back up as your system load increases, but they will eventually be processed. Vert.x's support for a distributed event bus also gives you the option to start up additional verticles to handle increased load.

In order to set up a Vert.x messaging system, you need to gain access to the event bus. Start by executing the eventBus() method on a vertx class instance:

EventBus eb = vertx.eventBus();    

Once you're connected to an EventBus you can publish messages in one of two ways:

  • publish() publishes a message to an address using publish/subscribe messaging, meaning that every subscriber to a given address will receive the published message. Addresses are just Strings, so you want to choose something meaningful, but in the end what matters is that both publisher and subscriber are configured to use the same string. If you are familiar with Java Message System (JMS), publish() acts similarly to publishing a message to a topic.
  • send() sends a message to an address using point-to-point messaging, meaning that only one subscriber will receive the message. If there are multiple subscribers to the address then Vert.x will use a round-robin algorithm to send the message. The benefit of using a round-robin algorithm is scalability: if you do not have enough resources on a single Vert.x instance to support your load, then you can simply start additional Vert.x instances and register them as listeners to the specified address. In JMS terms, issuing send() is similar to publishing a message to a queue.

Publish/subscribe vs point-to-point messaging

In a publish/subscribe messaging model, a publisher sends a message to a topic that is broadcast out to all subscribers. Using publish/subscribe over point-to-point messaging in an event-driven architecture means that a component is only responsible for publishing events as they occur. The publisher does not need to be aware of its subscribers in order to broadcast to them. Figure 2 is a flow diagram of a typical Vert.x publish/subscribe messaging architecture.

  • Print
  • Feedback

Resources