-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from KB-iGOT/4.8.20-dev-v4
4.8.20 dev v4
- Loading branch information
Showing
3 changed files
with
77 additions
and
8 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/com/igot/cb/discussion/service/impl/DiscussionAsyncProcess.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.igot.cb.discussion.service.impl; | ||
|
||
import com.igot.cb.discussion.entity.DiscussionEntity; | ||
import com.igot.cb.pores.cache.CacheService; | ||
import com.igot.cb.pores.elasticsearch.service.EsUtilService; | ||
import com.igot.cb.pores.util.CbServerProperties; | ||
import com.igot.cb.pores.util.Constants; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Map; | ||
|
||
@Service | ||
@Slf4j | ||
public class DiscussionAsyncProcess { | ||
@Autowired | ||
private CacheService cacheService; | ||
@Autowired | ||
private EsUtilService esUtilService; | ||
@Autowired | ||
private CbServerProperties cbServerProperties; | ||
|
||
@Async | ||
public void updateElasticsearchAndRedis(DiscussionEntity saveJsonEntity) { | ||
try { | ||
// Elasticsearch update | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
ObjectNode jsonNode = objectMapper.createObjectNode(); | ||
jsonNode.setAll((ObjectNode) saveJsonEntity.getData()); | ||
Map<String, Object> map = objectMapper.convertValue(jsonNode, Map.class); | ||
esUtilService.addDocument(cbServerProperties.getDiscussionEntity(), Constants.INDEX_TYPE, saveJsonEntity.getDiscussionId(), map, cbServerProperties.getElasticDiscussionJsonPath()); | ||
log.info("Updated Elasticsearch for discussion ID: {}", saveJsonEntity.getDiscussionId()); | ||
|
||
// Redis update | ||
cacheService.putCache("discussion_" + saveJsonEntity.getDiscussionId(), jsonNode); | ||
log.info("Updated Redis cache for discussion ID: {}", saveJsonEntity.getDiscussionId()); | ||
} catch (Exception e) { | ||
log.error("Failed to update Elasticsearch or Redis for discussion ID: {}", saveJsonEntity.getDiscussionId(), e); | ||
} | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.igot.cb.pores.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
@Configuration | ||
@EnableAsync | ||
public class AsyncConfig { | ||
|
||
@Bean | ||
|
||
public ThreadPoolTaskExecutor taskExecutor() { | ||
|
||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(10); | ||
executor.setMaxPoolSize(20); | ||
executor.setQueueCapacity(500); | ||
executor.setThreadNamePrefix("async-"); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
} |