[improvement][headless]Setup thread pool for data event listeners.

This commit is contained in:
jerryjzhang
2025-01-02 15:27:24 +08:00
parent 22f6190e7c
commit 83cb6967e7
3 changed files with 16 additions and 8 deletions

View File

@@ -11,6 +11,14 @@ import java.util.concurrent.TimeUnit;
@Component @Component
public class ThreadPoolConfig { public class ThreadPoolConfig {
@Bean("eventExecutor")
public ThreadPoolExecutor getTaskEventExecutor() {
return new ThreadPoolExecutor(4, 8, 60 * 3, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1024),
new ThreadFactoryBuilder().setNameFormat("supersonic-event-pool-").build(),
new ThreadPoolExecutor.CallerRunsPolicy());
}
@Bean("commonExecutor") @Bean("commonExecutor")
public ThreadPoolExecutor getCommonExecutor() { public ThreadPoolExecutor getCommonExecutor() {
return new ThreadPoolExecutor(8, 16, 60 * 3, TimeUnit.SECONDS, return new ThreadPoolExecutor(8, 16, 60 * 3, TimeUnit.SECONDS,

View File

@@ -10,7 +10,7 @@ import dev.langchain4j.store.embedding.TextSegmentConvert;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener; import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@@ -19,7 +19,7 @@ import java.util.List;
@Component @Component
@Slf4j @Slf4j
public class MetaEmbeddingListener implements ApplicationListener<DataEvent> { public class MetaEmbeddingListener {
@Autowired @Autowired
private EmbeddingConfig embeddingConfig; private EmbeddingConfig embeddingConfig;
@@ -30,8 +30,8 @@ public class MetaEmbeddingListener implements ApplicationListener<DataEvent> {
@Value("${s2.embedding.operation.sleep.time:3000}") @Value("${s2.embedding.operation.sleep.time:3000}")
private Integer embeddingOperationSleepTime; private Integer embeddingOperationSleepTime;
@Async @Async("eventExecutor")
@Override @EventListener
public void onApplicationEvent(DataEvent event) { public void onApplicationEvent(DataEvent event) {
List<DataItem> dataItems = event.getDataItems(); List<DataItem> dataItems = event.getDataItems();
if (CollectionUtils.isEmpty(dataItems)) { if (CollectionUtils.isEmpty(dataItems)) {

View File

@@ -7,17 +7,17 @@ import com.tencent.supersonic.common.pojo.enums.EventType;
import com.tencent.supersonic.headless.chat.knowledge.DictWord; import com.tencent.supersonic.headless.chat.knowledge.DictWord;
import com.tencent.supersonic.headless.chat.knowledge.helper.HanlpHelper; import com.tencent.supersonic.headless.chat.knowledge.helper.HanlpHelper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener; import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@Component @Component
@Slf4j @Slf4j
public class SchemaDictUpdateListener implements ApplicationListener<DataEvent> { public class SchemaDictUpdateListener {
@Async @Async("eventExecutor")
@Override @EventListener
public void onApplicationEvent(DataEvent dataEvent) { public void onApplicationEvent(DataEvent dataEvent) {
if (CollectionUtils.isEmpty(dataEvent.getDataItems())) { if (CollectionUtils.isEmpty(dataEvent.getDataItems())) {
return; return;