package com.aliyun.openservices.springboot.example.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.springboot.example.bean.*;
import com.aliyun.openservices.springboot.example.http.ConsumerThread;
import com.aliyun.openservices.springboot.example.http.ProducerThread;
import com.aliyun.openservices.springboot.example.utils.HttpRequestUtils;
import com.aliyun.openservices.springboot.example.utils.HttpsApiClient;
import com.aliyun.openservices.springboot.example.utils.TimeUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static com.aliyun.openservices.springboot.example.utils.TimeUtils.getCurrentTime;
@Service
public class AiService {
public ConcurrentLinkedQueue<JSONObject> allMessages = new ConcurrentLinkedQueue<>();
/***
* 线程相关
*/
private final ConsumerThread consumerThread = new ConsumerThread();
private final ProducerThread producerThread = new ProducerThread();
/***
* 结果相关
*/
public static Map<String,List<String>> configTimesResults = new HashMap();
public static List<KeywordChatResponse> keywordsKeywordChatRespons = new ArrayList<>();
public static AllCheckResult allCheckResult = new AllCheckResult();
/***
* 参数设置相关
*/
Map<String,List<KeywordSetting>> keyWordMap = new HashMap<>();
public Map<String,UserInfo>userInfosMap ;
public TimeSetting configTimes = new TimeSetting();
/***
* 检测周期
*/
public int checkPeriod =10; //检测周期
/***
* 重试次数
*/
public int retryParamCount = 0;//重试次数
public int retryKeywordsCount = 0;
/**
*关键词检测和设置 url相关
*/
public String aiUrl = "http://127.0.0.1:8086/predict_text";
public String aiUrlSettingKeywords = "http://127.0.0.1:8086/setkeywords";
public long checkTime = System.currentTimeMillis();
public AiService() {
initParam();
consumerThread.setAll_messages(allMessages);
runConsumer();
}
public Map<String, UserInfo> getUserInfosMap() {
return userInfosMap;
}
public void setUserInfosMap(Map<String, UserInfo> userInfosMap) {
this.userInfosMap = userInfosMap;
}
public void initParam(){
try{
if (this.retryParamCount>=3){
System.out.println("################初始化initParam失败"+this.retryParamCount);
}else {
HttpsApiClient.instance.init();
System.out.println("@@@@@@@@keyword settings ");
keyWordMap = HttpsApiClient.instance.getKeywordSetting();
System.out.println("@@@@@@@@time settings ");
configTimes = HttpsApiClient.instance.getTimeSetting();
System.out.println("@@@@@@@@user settings ");
userInfosMap = HttpsApiClient.instance.getUserSetting();
System.out.println("################初始化initParam成功");
//把keyword发送给python服务
sendKeywordsSetting();
}
this.retryParamCount++;
}catch (Exception e){
initParam();
System.out.println("################初始化initParam失败 retry:"+this.retryParamCount);
}
}
public boolean sendKeywordsSetting(){
try {
if(retryKeywordsCount<=3){
String result = HttpRequestUtils.postKeywords(aiUrlSettingKeywords, JSONArray.toJSONString(keyWordMap));
if (result.contains("ok")){
return true;
}else {
return false;
}
}else {
System.out.println("##############error sendKeywordsSuanfa 重试了多少次"+retryKeywordsCount);
}
retryKeywordsCount++;
}catch (Exception e){
sendKeywordsSetting();
System.out.println("##############error sendKeywordsSuanfa 正在重试:"+retryKeywordsCount+e);
}
return false;
}
private void runConsumer() {
ExecutorService executor = Executors.newFixedThreadPool(3); // 创建一个固定大小的线程池
try {
executor.submit(consumerThread);
// for (int i = 0; i < 1; i++) {
// executor.submit(new ConsumerThread()); // 提交一个 MyThread 对象给线程池
// }
} catch (Exception e) {
e.printStackTrace();
}
}
public TimeSetting getConfigTimes() {
return configTimes;
}
public void setConfigTimes(TimeSetting timeSetting) {
this.configTimes= timeSetting;
}
/**
* 调用算法接口,返回是否命中敏感词
*/
public List<KeywordChatResponse> getKeywordsResult(List<String> msgids, List<String> msgContents, List<String>msgTypes){
// List<ChatResponse>chatResponses = new ArrayList<>();
//
// int length = msgids.size()/1000;
// //分批次请求防止过大问题,每1000个请求一次,最终对结果进行合并
// for (int i =0;i<length+1;i++) {
// int start = 0 + 1000 * i;
// int end = 1000 + 1000 * i;
// if (1000 + 1000 * i > msgids.size() - 1) {
// end = msgids.size() - 1;
// }
// Map<String,List<String>>tmpmap = new HashMap<>();
// tmpmap.put("query",msgContents.subList(start,end));
// tmpmap.put("query_type",msgTypes.subList(start,end));
// tmpmap.put("id",msgids.subList(start,end));
//
// List resultList = (List) HttpRequestUtils.postJson(aiUrl, JSONArray.toJSONString(tmpmap)).get("result");
// if(resultList.size()>0){
// for(int k=0;k<resultList.size();k++){
// Map<String,String>tmp = (HashMap<String, String>) resultList.get(k);
// String id = tmp.get("id");
// String keyword = tmp.get("keyword");
// ChatResponse chatResponse = new ChatResponse();
// chatResponse.setId(id);
// chatResponse.setEventType("1");
// chatResponse.setBehavior_word(keyword);
// chatResponse.setBehavior_word_id("1");
// chatResponse.setHit_time(getCurrentTime());
// chatResponses.add(chatResponse);
// }
// }
// }
// return chatResponses;
//mock data
List<KeywordChatResponse> keywordChatRespons = new ArrayList<>();
for(int k=0;k<msgids.size();k++){
String id = msgids.get(k);
String keyword = "涉黄";
KeywordChatResponse keywordChatResponse = new KeywordChatResponse();
keywordChatResponse.setId(id);
keywordChatResponse.setEventType("1");
keywordChatResponse.setBehavior_word(keyword);
keywordChatResponse.setBehavior_word_id("1");
keywordChatResponse.setHit_time(getCurrentTime());
keywordChatRespons.add(keywordChatResponse);
}
for(int k=0;k<msgids.size();k++){
String id = msgids.get(k);
String keyword = "社恐";
KeywordChatResponse keywordChatResponse = new KeywordChatResponse();
keywordChatResponse.setId(id);
keywordChatResponse.setEventType("1");
keywordChatResponse.setBehavior_word(keyword);
keywordChatResponse.setBehavior_word_id("1");
keywordChatResponse.setHit_time(getCurrentTime());
keywordChatRespons.add(keywordChatResponse);
}
return keywordChatRespons;
}
@PostConstruct
public void init() {
System.out.println("PostConstruct");
// 请求http服务,请求那些参数信息,存放到这些字典里
try {
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 定时器定义,设置执行时间 60shttps://blog.csdn.net/qq_42926722/article/details/126729997
* 一个定时器是定时检测,检测完了后把数据自动进行数据情况操作
* 10s访问一次
*/
@Scheduled(fixedRate = 10*1000)
// @Scheduled(fixedRate = 10*60*1000)
public void process() {
// mockData();
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@ 定时器 @@@@@@@@@@@@@@@@@@@@@@@@@@@");
//处理一下ConcurrentLinkedQueue 的数据清除
detectBuffer();
//每隔10分钟运行一次,统计业务代码,把python代码翻译成
没有合适的资源?快使用搜索试试~ 我知道了~
Jvm优化的Java -Demo
需积分: 1 0 下载量 115 浏览量
2023-10-30
09:57:04
上传
评论
收藏 42.5MB GZ 举报
温馨提示
共52个文件
java:20个
class:16个
xml:7个
在这个代码可以调节JVM参数,打印JVM日志,调节GC算法
资源推荐
资源详情
资源评论
收起资源包目录
java-springboot-demo.tar.gz (52个子文件)
java-springboot-demo
pom.xml 4KB
src
main
resources
application.properties 2KB
java
com
aliyun
openservices
springboot
example
Application.java 420B
normal
ProducerClient.java 746B
DemoMessageListener.java 776B
ConsumerClient.java 2KB
utils
MyListAdd.java 859B
TimeUtils.java 1KB
HttpsApiClient.java 9KB
HttpRequestUtils.java 4KB
http
ConsumerThread.java 5KB
Consumer1.java 5KB
Producer.java 3KB
ProducerThread.java 2KB
service
AiService.java 22KB
control
MyController.java 3KB
bean
KeywordSetting.java 717B
UserInfo.java 568B
AllCheckResult.java 971B
TimeSetting.java 847B
RuleTimes.java 423B
KeywordChatResponse.java 1KB
.idea
jarRepositories.xml 864B
uiDesigner.xml 9KB
workspace.xml 9KB
misc.xml 482B
compiler.xml 801B
.gitignore 50B
encodings.xml 489B
target
classes
application.properties 2KB
com
aliyun
openservices
springboot
example
Application.class 840B
utils
HttpRequestUtils.class 5KB
MyListAdd.class 2KB
TimeUtils.class 2KB
HttpsApiClient.class 9KB
http
ProducerThread.class 3KB
ConsumerThread.class 6KB
service
AiService$1.class 1KB
AiService.class 18KB
control
MyController.class 4KB
bean
RuleTimes.class 845B
KeywordChatResponse.class 2KB
UserInfo.class 1KB
KeywordSetting.class 1KB
TimeSetting.class 1KB
AllCheckResult.class 2KB
java-springboot-demo-1.0.jar.original 33KB
java-springboot-demo-1.0.jar 47.44MB
maven-status
maven-compiler-plugin
compile
default-compile
createdFiles.lst 1KB
inputFiles.lst 3KB
maven-archiver
pom.properties 71B
generated-sources
annotations
.gitignore 333B
共 52 条
- 1
资源评论
Myou_vin
- 粉丝: 1
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功