@Slf4j
@Component
public class LambdaSupport implements ApplicationContextAware {
public void consumerRecords(String beanName, String functionName, List<ConsumerRecord<String, Object>> records, long taskId, int source) {
String logPre = beanName + "." + functionName;
FiveFunction<JSONObject, String, String, String, Integer, LabelContext> function = getFunctionFromBean(beanName, functionName);
if (Objects.isNull(function)) {
log.error("#" + logPre + "#getFunctionFromBean is null...beanName: {}, functionName: {}", beanName, functionName);
return;
}
LabelContext context = func.apply(jsonObject, name, family, qualifier(), source);
}
@FunctionalInterface
public interface FiveFunction<T, U, V, P, Q, R> {
R apply(T t, U u, V v, P p, Q q);
}
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
//将任意 Spring Bean 函数转换为 Function
public FiveFunction<JSONObject, String, String, String, Integer, LabelHbaseContext> getFunctionFromBean(String beanName, String functionName) {
return (JSONObject jsonObject, String name, String family, String qualifier, Integer source) -> {
// 根据 beanName 获取相应的 Bean
Object bean = applicationContext.getBean(beanName);
try {
// 使用反射调用特定函数
Method method = bean.getClass().getMethod(functionName, JSONObject.class, String.class, String.class, String.class, Integer.class);
return (LabelHbaseContext) method.invoke(bean, jsonObject, name, family, qualifier, source);
} catch (Exception e) {
log.error("#LambdaSupport.getFunctionFromBean#error", e);
return null; // 处理异常情况
}
};
}
}
springBean结合lambda function
最新推荐文章于 2025-08-13 18:07:46 发布