java - 从 Infinispan 缓存读取进入无限循环

标签 java groovy apache-nifi infinispan

我正在使用Nifi创建数据流管道,其中我使用Infinispan作为缓存服务器但是当我将executescript与Groovy脚本一起使用时,它会无限循环并打开许多套接字连接。我尝试关闭相同的连接,但它仍然打开许多连接,然后抛出

java.net.SocketException: No buffer space available (maximum connections reached?): connect

通过下面的链接我更改了注册表 https://support.pitneybowes.com/VFP06_KnowledgeWithSidebarTroubleshoot?id=kA280000000PEE1CAO&popup=false&lang=en_US

然后使用 netstat -n 检查打开的连接,由于上述设置,我打开了 65534。

下面是从 Infinispan 缓存读取的 groovy 脚本

import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.apache.commons.io.IOUtils;
import java.nio.charset.StandardCharsets;

def cacheName = "mycache"

def configuration = new ConfigurationBuilder()
.addServer().host("localhost").port(11322).build();

def cacheManager = new RemoteCacheManager(configuration)

RemoteCache cacheA = cacheManager.getCache(cacheName)

flowFile = session.get()
if(!flowFile) return
key = flowFile.getAttribute('key')
id = flowFile.getAttribute('id')
jsonFromCache = cacheA.get(key + "_" + id);
if(cacheA != null) {
cacheA.stop()
}
if(cacheManager != null) {
cacheManager.stop()
}

flowFile = session.write(flowFile, {outputStream ->
  outputStream.write(jsonFromCache.getBytes(StandardCharsets.UTF_8))
} as OutputStreamCallback)
session.transfer(flowFile, REL_SUCCESS)

最佳答案

您正在从 session 获取文件之前打开与缓存的连接。

因此,您正在打开连接,并在以下行中退出脚本而不关闭它:

if(!flowFile) return

还有一点: 您可以使用 ExecuteGroovyScript 处理器。然后可以管理处理器的启动和停止。您可以在这里找到示例:https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-groovyx-nar/1.9.2/org.apache.nifi.processors.groovyx.ExecuteGroovyScript/additionalDetails.html

import org.apache.nifi.processor.ProcessContext
import java.util.concurrent.atomic.AtomicLong

class Const{
  static Date startTime = null;
  static AtomicLong triggerCount = null;
}

static onStart(ProcessContext context){
  Const.startTime = new Date()
  Const.triggerCount = new AtomicLong(0)
  println "onStart $context ${Const.startTime}"
}

static onStop(ProcessContext context){
  def alive = (System.currentTimeMillis() - Const.startTime.getTime()) / 1000
  println "onStop $context executed ${ Const.triggerCount } times during ${ alive } seconds"
}

def flowFile = session.get()
if(!flowFile)return
flowFile.'trigger.count' = Const.triggerCount.incrementAndGet()
REL_SUCCESS << flowFile

关于java - 从 Infinispan 缓存读取进入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55990914/

相关文章:

java - 如何在 Java 的 if 语句中使用 "or"而无需重新输入整个表达式?

java - 文档 - 如何通过名称获取标签的值?

java - 如何从 MainActivity 启动 fragment Activity ?

ubuntu - Jenkins 突然显示 "wrapper script does not seem to be touching the log file"并超时并显示 FAILURE 消息

java - ElasticSearch Java API : Update Existing Document

apache-nifi:使用模板时如何避免重复

java - 为什么我们在 Java PATH 变量中添加分号而不是在 JAVA_HOME 变量中添加分号?

python - Python 中的安全取消引用

apache-nifi - JoltTransformJSON - 提取属性而不删除它

json - Apache NiFi 拆分 JSON 根数组