private void internalHandleRegister( final SockJSSocket sock, final String address, Map<String, Handler<Message>> handlers) { if (handleRegister(sock, address)) { Handler<Message> handler = new Handler<Message>() { public void handle(final Message msg) { Match curMatch = checkMatches(false, address, msg.body); if (curMatch.doesMatch) { if (curMatch.requiresAuth && sockAuths.get(sock) == null) { log.debug( "Outbound message for address " + address + " rejected because auth is required and socket is not authed"); } else { checkAddAccceptedReplyAddress(msg.replyAddress); deliverMessage(sock, address, msg); } } else { log.debug( "Outbound message for address " + address + " rejected because there is no inbound match"); } } }; handlers.put(address, handler); eb.registerHandler(address, handler); } }
public void testDeploy() { final Thread t = Thread.currentThread(); eb.registerHandler( "test-handler", new Handler<Message<String>>() { public void handle(Message<String> message) { tu.azzert(Thread.currentThread() == t); if ("started".equals(message.body)) { eb.unregisterHandler("test-handler", this); tu.testComplete(); } } }); container.deployVerticle( ChildVerticle.class.getName(), null, 1, new Handler<String>() { public void handle(String deployID) { tu.azzert(Thread.currentThread() == t); tu.azzert(deployID != null); } }); }
@Override public void start() throws Exception { if (this.isStarted) { return; } System.setProperty( "vertx.clusterManagerFactory", HazelcastClusterManagerFactory.class.getName()); if (quorumSize != -1) { platformManager = PlatformLocator.factory.createPlatformManager(port, host, quorumSize, haGroup); } else { platformManager = PlatformLocator.factory.createPlatformManager(port, host); } eventBus = platformManager.vertx().eventBus(); Binding b = postOffice.getBinding(new SimpleString(queueName)); if (b == null) { throw new Exception(connectorName + ": queue " + queueName + " not found"); } handler = new EventHandler(); eventBus.registerHandler(vertxAddress, handler); isStarted = true; ActiveMQVertxLogger.LOGGER.debug(connectorName + ": started"); }
/** * 根据传入进来的handler对象,获取对象是否被@EBusHandler注解,如果此类被注解,那么需要判断此类中的全局变量 * 是否有被@EBusField进行注解。如果注解了,需要添加到Ebus中。 * * @param obj */ public static void scanEBus(Object obj) { if (obj == null) { return; } EventBus ebs = InstallVertIOService.getVertx().eventBus(); Class className = obj.getClass(); Annotation anno = className.getDeclaredAnnotation(EBusHandler.class); if (anno != null) { Field[] fields = className.getDeclaredFields(); for (Field field : fields) { EBusField ebf = (EBusField) field.getAnnotation(EBusField.class); if (ebf != null) { String value = ebf.value(); try { ebs.registerHandler(value, (Handler<Message>) field.get(obj)); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
public void start() { // handler config JSON JsonObject config = container.config(); int port = config.getNumber("hec_port").intValue(); int poolsize = config.getNumber("hec_poolsize").intValue(); this.token = config.getString("hec_token"); this.index = config.getString("index"); this.source = config.getString("source"); this.sourcetype = config.getString("sourcetype"); boolean useHTTPs = config.getBoolean("hec_https"); this.hecBatchMode = config.getBoolean("hec_batch_mode"); this.hecMaxBatchSizeBytes = config.getNumber("hec_max_batch_size_bytes").longValue(); this.hecMaxBatchSizeEvents = config.getNumber("hec_max_batch_size_events").longValue(); this.hecMaxInactiveTimeBeforeBatchFlush = config.getNumber("hec_max_inactive_time_before_batch_flush").longValue(); this.batchBuffer = Collections.synchronizedList(new LinkedList<String>()); this.lastEventReceivedTime = System.currentTimeMillis(); // Event Bus (so we can receive the data) String eventBusAddress = config.getString("output_address"); EventBus eb = vertx.eventBus(); client = vertx.createHttpClient().setPort(port).setHost("localhost").setMaxPoolSize(poolsize); if (useHTTPs) { client.setSSLContext(getSSLContext()); client.setVerifyHost(false); client.setSSL(true); client.setTrustAll(true); } // data handler that will process our received data Handler<Message<String>> myHandler = new Handler<Message<String>>() { public void handle(Message<String> message) { try { String messageContent = escapeMessageIfNeeded(message.body()); StringBuffer json = new StringBuffer(); json.append("{\"").append("event\":").append(messageContent).append(",\""); if (!index.equalsIgnoreCase("default")) json.append("index\":\"").append(index).append("\",\""); json.append("source\":\"") .append(source) .append("\",\"") .append("sourcetype\":\"") .append(sourcetype) .append("\"") .append("}"); String bodyContent = json.toString(); if (hecBatchMode) { lastEventReceivedTime = System.currentTimeMillis(); currentBatchSizeBytes += bodyContent.length(); batchBuffer.add(bodyContent); if (flushBuffer()) { bodyContent = rollOutBatchBuffer(); batchBuffer.clear(); currentBatchSizeBytes = 0; hecPost(bodyContent); } } else { hecPost(bodyContent); } } catch (Exception e) { logger.error("Error writing received data: " + ModularInput.getStackTrace(e)); } } /** * from Tivo * * @param message * @return */ private String escapeMessageIfNeeded(String message) { String trimmedMessage = message.trim(); if (trimmedMessage.startsWith("{") && trimmedMessage.endsWith("}")) { // this is *probably* JSON. return trimmedMessage; } else if (trimmedMessage.startsWith("\"") && trimmedMessage.endsWith("\"") && !message.substring(1, message.length() - 1).contains("\"")) { // this appears to be a quoted string with no internal // quotes return trimmedMessage; } else { // don't know what this thing is, so need to escape all // quotes, and // then wrap the result in quotes return "\"" + message.replace("\"", "\\\"") + "\""; } } }; if (hecBatchMode) { new BatchBufferActivityCheckerThread().start(); } // start listening for data eb.registerHandler(eventBusAddress, myHandler); }