Last active
May 12, 2022 03:19
-
-
Save yangl/931b399674e30c1100b2b91ac5480296 to your computer and use it in GitHub Desktop.
MQ Pulsar Kafka消息过滤使用(消息头) AviatorScript https://github.com/killme2008/aviatorscript 函数库 https://www.yuque.com/boyan-avfmj/aviatorscript/ashevw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sf.springbootstrap; | |
import com.google.common.collect.Maps; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import com.googlecode.aviator.*; | |
import java.util.Map; | |
import java.util.Set; | |
import lombok.extern.slf4j.Slf4j; | |
/** | |
* @author YANGLiiN | |
*/ | |
@Slf4j | |
public class AviatorScriptDemo { | |
// AviatorEvaluatorInstance 执行实例 | |
private static AviatorEvaluatorInstance AV_INSTANCE; | |
private static final Gson GSON = new GsonBuilder().create(); | |
static { | |
AV_INSTANCE = AviatorEvaluator.getInstance(); | |
// MQ消息过滤 只启用 If Return 即可 | |
Set<Feature> features = Feature.asSet(Feature.If, Feature.Return); | |
AV_INSTANCE.setOption(Options.FEATURE_SET, features); | |
} | |
public static void main(String[] args) { | |
// Map<String, Object> env = AviatorEvaluator.newEnv("a", 2.1, "b", 301.3, "c", "ABC"); | |
Map<String, Object> env = Maps.newHashMap(); | |
env.put("a", 2.1); | |
env.put("b", 301.3); | |
env.put("c", "a"); | |
String expression = "a>1 && (b<200 || c=='ABC')"; | |
Object rs = null; | |
try { | |
rs = AV_INSTANCE.execute(expression, env); | |
} catch (Exception e) { | |
log.error("Aviator表达式{},参数{}执行报错", expression, GSON.toJson(env), e); | |
} | |
if (rs != null && Boolean.TRUE.equals(rs)) { | |
System.out.println("符合规则数据"); | |
} else { | |
System.out.println("非标数据!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment