All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rapatao.vertx.eventbus.proxyhelper.ServiceRegistry Maven / Gradle / Ivy

package com.rapatao.vertx.eventbus.proxyhelper;

import io.vertx.core.eventbus.EventBus;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Event Bus Service Registry
 * 

* This class is responsibly to registry all services int the Vertx EventBus. *

* Created by rapatao on 13/09/16 */ @RequiredArgsConstructor(access = AccessLevel.PRIVATE) public class ServiceRegistry { private final static Logger logger = LoggerFactory.getLogger(ServiceRegistry.class); private final EventBus eventBus; private final List services = new ArrayList<>(); private String prefix = ""; /** * Create a ServiceRegistry using an {@link EventBus}. * * @param eventBus the Vertx EventBus. * @return the ServiceRegistry instance. */ public static ServiceRegistry toEventBus(final EventBus eventBus) { return new ServiceRegistry(eventBus); } /** * Defines the prefix to use when registry the consumers. * * @param prefix the prefix to be used in service address. Default is an empty string. * @return the ServiceRegistry instance. */ public ServiceRegistry withPrefix(final String prefix) { this.prefix = prefix; return this; } /** * Add an Service implementation to be registered in the Vertx EventBus. * * @param instance the service implementation instance. * @param the service type. * @return the ServiceRegistry instance. */ public ServiceRegistry to(T instance) { services.add(instance); return this; } /** * Registry all services in the Vertx EventBus. */ public void registry() { services.forEach(this::registry); } private void registry(final Object instance) { final Class anInterface = getInterfaceClass(instance); for (Method method : anInterface.getMethods()) { final String address = prefix + "#" + anInterface.getName() + "#" + method.getName(); logger.info("Registering consumer for " + address); EventBusConsumerHandler eventBusConsumerHandler = new EventBusConsumerHandler(method, instance); eventBus.consumer(address, eventBusConsumerHandler::handle); } } private Class getInterfaceClass(T instance) { final Class[] interfaces = instance.getClass().getInterfaces(); if (interfaces == null) { throw new RuntimeException("Class must have an interface"); } if (interfaces.length > 1) { throw new RuntimeException("The class must have only one interface"); } return interfaces[0]; } }