Vert.x Connector Service Example

This example shows you how to configure ActiveMQ to use the Vert.x Connector Service.

ActiveMQ supports 2 types of Vert.x connector, incoming and outgoing. Incoming connector consumes from Vert.x event bus and forwards to a configurable address. Outgoing connector consumes from a configurable address and forwards to a configurable Vert.x event bus.

In this example, an incoming connector and an outgoing connector are configured. A simple java Verticle is deployed. The verticle registers a message handler on the outgoing connector's address ("outgoing.vertx.address"). A String message is sent to Vert.x event bus on the incoming connector's address("incoming.vertx.address"). The message then will be forwarded to a ActiveMQ queue by the incoming connector. The outgoing connector listens to the ActiveMQ queue and forwards the message from ActiveMQ to Vert.x event bus on the outgoing connector's address. The verticle finally receives the message from it's event bus.

For more information on Vert.x concept please visit the Vertx site

Example step-by-step

To run the server, simply type mvn verify from this directory.

  1. First we need to create a Vert.x PlatformManager
  2.            platformManager = PlatformLocator.factory.createPlatformManager(PORT, HOST);
            
  3. We deploy a Verticle using the platformManager
  4.            String verticle = "org.apache.activemq.core.example.ExampleVerticle";
               platformManager.deployVerticle(verticle, null, new URL[0], 1, null, 
                      new Handler>(){
    
                         @Override
                         public void handle(AsyncResult result)
                         {
                            if (!result.succeeded())
                            {
                               throw new RuntimeException("failed to deploy verticle", result.cause());
                            }
                            latch0.countDown();
                         }
                
               });
            
  5. We register a message handler with the event bus in the Verticle to listen on the outgoing connector's address.
  6.            EventBus eventBus = vertx.eventBus();
               eventBus.registerHandler(VertxConnectorExample.OUTGOING, 
                          new Handler>() {
                             @Override
                             public void handle(Message startMsg)
                             {
                                Object body = startMsg.body();
                                System.out.println("Verticle receives a message: " + body);
                                VertxConnectorExample.result.set(VertxConnectorExample.MSG.equals(body));
                                latch0.countDown();
                             }
                          });
               
            
  7. We send a message to incoming connector's address via event bus
  8.            
                  EventBus bus = platformManager.vertx().eventBus();
                  bus.send(INCOMING, MSG);
               
            
  9. The message will eventually arrives at the Verticle's message handler.