All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.sdn.api.event.NoACLMessageEvent Maven / Gradle / Ivy
//package org.sdn.api.event;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import org.apache.kafka.clients.consumer.ConsumerRecord;
//import org.apache.kafka.clients.consumer.ConsumerRecords;
//import org.apache.kafka.clients.consumer.KafkaConsumer;
//import org.sdn.api.DefaultOpenClient;
//import org.sdn.api.OpenApiException;
//import org.sdn.api.request.SubscribeEventRequest;
//import org.sdn.api.response.SubscribeEventResponse;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.config.BeanPostProcessor;
//import org.springframework.core.annotation.AnnotationUtils;
//import org.springframework.util.ReflectionUtils;
//
//import java.lang.reflect.InvocationTargetException;
//import java.lang.reflect.Method;
//import java.time.Duration;
//import java.util.*;
//
///**
// * User: humucai
// * Date: 2018/11/27
// * Time: 18:28
// */
//public class NoACLMessageEvent implements BeanPostProcessor {
// private static Logger logger = LoggerFactory.getLogger(NoACLMessageEvent.class);
//
// private Object obj;
//
// private Method method;
//
// private static Map executors = new HashMap<>();
//
// private static NoACLMessageEvent defaultExecutor;
//
// private static Set servers = new HashSet<>();
//
// private static Set topics = new HashSet<>();
//
// private static boolean isInit = false;
//
// private NoACLMessageEvent(Object obj, Method method) {
// this.obj = obj;
// this.method = method;
// }
//
// public NoACLMessageEvent() {
// }
//
// public synchronized static void start(DefaultOpenClient client, String accessToken) throws OpenApiException {
// if (isInit) {
// return;
// }
// isInit = true;
// subscribeEvent(client, accessToken);
// startEventListener(client.appKey);
// }
//
//
// // 订阅事件
// private static void subscribeEvent(DefaultOpenClient client, String accessToken) throws OpenApiException {
// if (isNullOrEmpty(client.authEvent) || isNullOrEmpty(accessToken)) {
// return;
// }
// try {
// JSONObject o = JSON.parseObject(client.authEvent);
// topics = o.keySet();
// List> ps = new ArrayList<>();
// for (String key : topics) {
// JSONObject ob = o.getJSONObject(key);
// JSONObject condition = ob.getJSONObject("condition");
// String server = condition.getString("server");
// JSONObject sub = condition.getJSONObject("sub");
// servers.add(server);
//
// String event = ob.getString("event");
// if (event != null) {
// Map param = new HashMap<>();
// param.put("servicename", key);
// sub.forEach(param::put);
// String[] events = event.split(",");
// List> evs = new ArrayList<>();
//
// for (String e : events) {
// Map m = new HashMap<>();
// m.put("eventname", e);
// m.put("ext", condition.getString(e));
// evs.add(m);
// }
// param.put("events", evs);
// ps.add(param);
// }
// }
//
// for (Map p : ps) {
// SubscribeEventRequest request = new SubscribeEventRequest(p);
// SubscribeEventResponse response = client.defaultExecute(request, accessToken);
// if (response.isSuccess()) {
// if (!response.isOk()) {
// throw new OpenApiException("subscribe event error");
// }
// }
// }
//
// } catch (Exception e) {
// throw new OpenApiException("subscribe event error", e);
// }
//
// }
//
//
// private static void startEventListener(String appKey) {
// new Thread(() -> {
// if (servers.isEmpty() || topics.isEmpty()) {
// return;
// }
// Iterator iterator = servers.iterator();
// String server = iterator.next();
//
// Properties props = new Properties();
// props.put("bootstrap.servers", server);
// props.put("group.id", appKey);
// props.put("enable.auto.commit", "true");
// props.put("auto.commit.interval.ms", "1000");
// props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
// props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
// KafkaConsumer consumer = new KafkaConsumer<>(props);
// consumer.subscribe(new ArrayList<>(topics));
// while (true) {
// ConsumerRecords records = consumer.poll(Duration.ofMillis(100));
//
// if (!records.isEmpty()) {
// if (executors.isEmpty() && defaultExecutor == null) {
// logger.warn("Has no handler!");
// continue;
// }
// }
//
// for (ConsumerRecord record : records) {
// String topic = record.topic();
//
// NoACLMessageEvent event = executors.get(topic);
// if (event != null) {
// try {
// event.method.invoke(event.obj, record.value());
// } catch (IllegalAccessException | InvocationTargetException e) {
// // ignore error
// }
// } else {
//
// if (defaultExecutor != null) {
// try {
// defaultExecutor.method.invoke(defaultExecutor.obj, record.value(), topic);
// } catch (IllegalAccessException | InvocationTargetException e) {
// // ignore error
// }
// continue;
// } else {
// logger.warn("Topic {} has no handler!", topic);
// }
// }
// }
// }
// }).start();
// }
//
//
// @Override
// public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// return bean;
// }
//
// @Override
// public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// Method[] methods = ReflectionUtils.getAllDeclaredMethods(bean.getClass());
// final String stringType = "java.lang.String";
//
// for (Method m : methods) {
// EventListener eventListener = AnnotationUtils.findAnnotation(m, EventListener.class);
// if (eventListener == null) {
// continue;
// }
//
// String v = eventListener.value();
//
// Class>[] parameterTypes = m.getParameterTypes();
// if (isNullOrEmpty(v)) {
// if (parameterTypes.length > 1 &&
// Objects.equals(parameterTypes[0].getName(), stringType) &&
// Objects.equals(parameterTypes[1].getName(), stringType)) {
// defaultExecutor = new NoACLMessageEvent(bean, m);
// }
// } else {
// if (parameterTypes.length < 1 ||
// !Objects.equals(parameterTypes[0].getName(), stringType)) {
// continue;
// }
// if (!isNullOrEmpty(v)) {
//
// executors.put(v, new NoACLMessageEvent(bean, m));
//
// }
// }
// }
// return bean;
// }
//
//
// private static boolean isNullOrEmpty(String s) {
// return s == null || s.trim().equals("");
// }
//
//}