提交 1e43c56f 编写于 作者: sq3536's avatar sq3536

提交

上级 d401e4d5
package cn.ibizlab.core.extensions.domain;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.springframework.util.StringUtils;
import java.io.Serializable;
@Data
public class LocalMsgEvent implements Serializable {
private String body;
public <T> void setMessage(T message)
{
body = JSON.toJSONString(message);
}
public <T> T getMessage(Class<T> clazz)
{
if(StringUtils.isEmpty(body))
return null;
return JSON.parseObject(body,clazz);
}
private String tags = "default";
public int getType()
{
return "default".equals(tags)?2:1;
}
}
...@@ -6,6 +6,7 @@ import cn.ibizlab.core.extensions.cql.ExecResult; ...@@ -6,6 +6,7 @@ import cn.ibizlab.core.extensions.cql.ExecResult;
import cn.ibizlab.core.extensions.domain.BaseRequest; import cn.ibizlab.core.extensions.domain.BaseRequest;
import cn.ibizlab.core.extensions.domain.ResultsMQMsg; import cn.ibizlab.core.extensions.domain.ResultsMQMsg;
import cn.ibizlab.core.extensions.util.DefaultMQProducerService; import cn.ibizlab.core.extensions.util.DefaultMQProducerService;
import cn.ibizlab.core.extensions.util.MsgProducerService;
import cn.ibizlab.core.lite.extensions.domain.FieldObj; import cn.ibizlab.core.lite.extensions.domain.FieldObj;
import cn.ibizlab.core.lite.extensions.domain.ModelObj; import cn.ibizlab.core.lite.extensions.domain.ModelObj;
import cn.ibizlab.core.rule.domain.ExecLog; import cn.ibizlab.core.rule.domain.ExecLog;
...@@ -30,7 +31,7 @@ public class AnalyseEntityServiceImpl extends BaseEntityServiceImpl { ...@@ -30,7 +31,7 @@ public class AnalyseEntityServiceImpl extends BaseEntityServiceImpl {
public static final String Tag_SaveResults = "SaveResults"; public static final String Tag_SaveResults = "SaveResults";
@Autowired @Autowired
private DefaultMQProducerService defaultMQProducerService; private MsgProducerService defaultMQProducerService;
@Autowired @Autowired
@Lazy @Lazy
...@@ -207,7 +208,7 @@ public class AnalyseEntityServiceImpl extends BaseEntityServiceImpl { ...@@ -207,7 +208,7 @@ public class AnalyseEntityServiceImpl extends BaseEntityServiceImpl {
resultsMQMsg.setDatas((List<ExecResult>)saveResults); resultsMQMsg.setDatas((List<ExecResult>)saveResults);
// defaultMQProducerService.sendToMQ(resultsMQMsg, "default"); // defaultMQProducerService.sendToMQ(resultsMQMsg, "default");
} }
defaultMQProducerService.sendToMQ(resultsMQMsg, "default"); defaultMQProducerService.sendResultsMsg(resultsMQMsg);
} }
catch (Exception e) catch (Exception e)
{ {
......
...@@ -8,7 +8,7 @@ import cn.ibizlab.core.analysis.service.impl.DABuildServiceImpl; ...@@ -8,7 +8,7 @@ import cn.ibizlab.core.analysis.service.impl.DABuildServiceImpl;
import cn.ibizlab.core.extensions.domain.BaseRequest; import cn.ibizlab.core.extensions.domain.BaseRequest;
import cn.ibizlab.core.extensions.domain.EngineMQMsg; import cn.ibizlab.core.extensions.domain.EngineMQMsg;
import cn.ibizlab.core.extensions.util.DefaultMQProducerService; import cn.ibizlab.core.extensions.util.DefaultMQProducerService;
import cn.ibizlab.core.extensions.util.MQConsumeMsgListenerProcessor; import cn.ibizlab.core.extensions.util.MsgProducerService;
import cn.ibizlab.core.lite.extensions.domain.EntityObj; import cn.ibizlab.core.lite.extensions.domain.EntityObj;
import cn.ibizlab.core.lite.extensions.model.DataModel; import cn.ibizlab.core.lite.extensions.model.DataModel;
import cn.ibizlab.core.lite.extensions.service.LiteDataCallback; import cn.ibizlab.core.lite.extensions.service.LiteDataCallback;
...@@ -60,7 +60,7 @@ public class DABuildExService extends DABuildServiceImpl { ...@@ -60,7 +60,7 @@ public class DABuildExService extends DABuildServiceImpl {
private IRuleItemService ruleItemService; private IRuleItemService ruleItemService;
@Autowired @Autowired
private DefaultMQProducerService defaultMQProducerService; private MsgProducerService defaultMQProducerService;
@Autowired @Autowired
private LiteModelService liteModelService; private LiteModelService liteModelService;
...@@ -151,7 +151,7 @@ public class DABuildExService extends DABuildServiceImpl { ...@@ -151,7 +151,7 @@ public class DABuildExService extends DABuildServiceImpl {
engineMQMsg.setCount(datas.size()); engineMQMsg.setCount(datas.size());
engineMQMsg.setDatas(datas); engineMQMsg.setDatas(datas);
try { try {
defaultMQProducerService.sendToMQ(engineMQMsg, MQConsumeMsgListenerProcessor.Type_Build); defaultMQProducerService.sendBuildMsg(engineMQMsg);
} catch (Exception ex) { } catch (Exception ex) {
return false; return false;
} }
......
...@@ -2,8 +2,7 @@ package cn.ibizlab.core.extensions.service; ...@@ -2,8 +2,7 @@ package cn.ibizlab.core.extensions.service;
import cn.ibizlab.core.extensions.domain.BaseRequest; import cn.ibizlab.core.extensions.domain.BaseRequest;
import cn.ibizlab.core.extensions.domain.EngineMQMsg; import cn.ibizlab.core.extensions.domain.EngineMQMsg;
import cn.ibizlab.core.extensions.util.DefaultMQProducerService; import cn.ibizlab.core.extensions.util.MsgProducerService;
import cn.ibizlab.core.extensions.util.MQConsumeMsgListenerProcessor;
import cn.ibizlab.core.lite.extensions.domain.EntityObj; import cn.ibizlab.core.lite.extensions.domain.EntityObj;
import cn.ibizlab.util.helper.Setting; import cn.ibizlab.util.helper.Setting;
import cn.ibizlab.core.lite.extensions.model.DataModel; import cn.ibizlab.core.lite.extensions.model.DataModel;
...@@ -60,7 +59,7 @@ public class RuleEngineExService extends RuleEngineServiceImpl { ...@@ -60,7 +59,7 @@ public class RuleEngineExService extends RuleEngineServiceImpl {
private LiteModelService liteModelService; private LiteModelService liteModelService;
@Autowired @Autowired
private DefaultMQProducerService defaultMQProducerService; private MsgProducerService defaultMQProducerService;
@Value("${ibiz.rulepath:/app/file/rules/}") @Value("${ibiz.rulepath:/app/file/rules/}")
private String rulePath; private String rulePath;
...@@ -112,7 +111,7 @@ public class RuleEngineExService extends RuleEngineServiceImpl { ...@@ -112,7 +111,7 @@ public class RuleEngineExService extends RuleEngineServiceImpl {
engineMQMsg.setCount(datas.size()); engineMQMsg.setCount(datas.size());
engineMQMsg.setDatas(datas); engineMQMsg.setDatas(datas);
try { try {
defaultMQProducerService.sendToMQ(engineMQMsg, MQConsumeMsgListenerProcessor.Type_Engine); defaultMQProducerService.sendEngineMsg(engineMQMsg);
} catch (Exception ex) { } catch (Exception ex) {
return false; return false;
} }
......
...@@ -12,6 +12,7 @@ import org.apache.rocketmq.common.message.Message; ...@@ -12,6 +12,7 @@ import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException; import org.apache.rocketmq.remoting.exception.RemotingException;
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.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -20,7 +21,8 @@ import org.springframework.stereotype.Service; ...@@ -20,7 +21,8 @@ import org.springframework.stereotype.Service;
*/ */
@Slf4j @Slf4j
@Service @Service
public class DefaultMQProducerService { @ConditionalOnExpression("(${rocketmq.producer.enabled:false})||(${rocketmq.consumer.enabled:false})")
public class DefaultMQProducerService implements MsgProducerService{
@Value("${rocketmq.producer.ruleEngineTopic:DSTMSG}") @Value("${rocketmq.producer.ruleEngineTopic:DSTMSG}")
private String ruleEngineTopic; private String ruleEngineTopic;
...@@ -33,17 +35,28 @@ public class DefaultMQProducerService { ...@@ -33,17 +35,28 @@ public class DefaultMQProducerService {
DefaultMQProducer defaultMQProducer; DefaultMQProducer defaultMQProducer;
public void sendToMQ(EngineMQMsg engineMQMsg, String tags) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
@Override
public void sendEngineMsg(EngineMQMsg engineMQMsg) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
String msg = JSON.toJSONString(engineMQMsg);
Message sendMsg = new Message(ruleEngineTopic, "Engine", msg.getBytes());
SendResult sendResult = defaultMQProducer.send(sendMsg);
log.info("引擎消息发送响应:" + sendResult.toString());
}
@Override
public void sendBuildMsg(EngineMQMsg engineMQMsg) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
String msg = JSON.toJSONString(engineMQMsg); String msg = JSON.toJSONString(engineMQMsg);
Message sendMsg = new Message(ruleEngineTopic, tags, msg.getBytes()); Message sendMsg = new Message(ruleEngineTopic, "Build", msg.getBytes());
SendResult sendResult = defaultMQProducer.send(sendMsg); SendResult sendResult = defaultMQProducer.send(sendMsg);
log.info("引擎消息发送响应:" + sendResult.toString()); log.info("引擎消息发送响应:" + sendResult.toString());
} }
public void sendToMQ(ResultsMQMsg resultMQMsg, String tags) throws InterruptedException, RemotingException, MQClientException, MQBrokerException { @Override
String msg = JSON.toJSONString(resultMQMsg); public void sendResultsMsg(ResultsMQMsg resultsMQMsg) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
Message sendMsg = new Message(resultsTopic, tags, msg.getBytes()); String msg = JSON.toJSONString(resultsMQMsg);
sendMsg.setKeys(resultMQMsg.getKeyValueField()); Message sendMsg = new Message(resultsTopic, "default", msg.getBytes());
sendMsg.setKeys(resultsMQMsg.getKeyValueField());
SendResult sendResult = defaultMQProducer.send(sendMsg); SendResult sendResult = defaultMQProducer.send(sendMsg);
log.info("构建消息发送响应:" + sendResult.toString()); log.info("构建消息发送响应:" + sendResult.toString());
} }
......
package cn.ibizlab.core.extensions.util;
import cn.ibizlab.core.extensions.cql.ExecResultRepository;
import cn.ibizlab.core.extensions.domain.EngineMQMsg;
import cn.ibizlab.core.extensions.domain.LocalMsgEvent;
import cn.ibizlab.core.extensions.domain.ResultsMQMsg;
import cn.ibizlab.core.extensions.service.DABuildExService;
import cn.ibizlab.core.extensions.service.RuleEngineExService;
import com.lmax.disruptor.EventHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.StringUtils;
import java.util.concurrent.TimeUnit;
@Slf4j
public class LocalMsgEventHandler implements EventHandler<LocalMsgEvent> {
@Autowired
@Lazy
private RuleEngineExService ruleEngineExService;
@Autowired
@Lazy
private DABuildExService daBuildExService;
@Autowired(required = false)
@Lazy
private ExecResultRepository execResultRepository;
@Override
public void onEvent(LocalMsgEvent localMsgEvent, long sequence, boolean endOfBatch) throws Exception {
if (localMsgEvent.getType()==1) {
String tags = localMsgEvent.getTags();
if("Engine".equalsIgnoreCase(tags)) {
EngineMQMsg engineMQMsg = localMsgEvent.getMessage(EngineMQMsg.class);
if (log.isDebugEnabled()) {
log.debug("EngineMQMsg Event Handler: {}", engineMQMsg.getBatch());
}
ruleEngineExService.processData(engineMQMsg);
}else if("Build".equalsIgnoreCase(tags)){
EngineMQMsg engineMQMsg = localMsgEvent.getMessage(EngineMQMsg.class);
if (log.isDebugEnabled()) {
log.debug("BuildMQMsg Event Handler: {}", engineMQMsg.getBatch());
}
daBuildExService.processData(engineMQMsg);
}
}
else if (localMsgEvent.getType()==2) {
ResultsMQMsg resultsMQMsg = localMsgEvent.getMessage(ResultsMQMsg.class);
if (log.isDebugEnabled()) {
log.debug("ResultsMQMsg Event Handler: {}", resultsMQMsg.getKeyValueField());
}
if(!StringUtils.isEmpty(resultsMQMsg.getKeyValueField()))
execResultRepository.saveResultsMQMsg(resultsMQMsg);
}
}
}
package cn.ibizlab.core.extensions.util;
import cn.ibizlab.core.extensions.domain.EngineMQMsg;
import cn.ibizlab.core.extensions.domain.LocalMsgEvent;
import cn.ibizlab.core.extensions.domain.ResultsMQMsg;
import com.alibaba.fastjson.JSON;
import com.baomidou.jobs.model.JobsInfo;
import com.lmax.disruptor.dsl.Disruptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Service;
@Slf4j
@Service
@ConditionalOnExpression("(!${rocketmq.producer.enabled:false})&&(!${rocketmq.consumer.enabled:false})")
public class LocalMsgProducerService implements MsgProducerService{
@Autowired
protected Disruptor<LocalMsgEvent> disruptor;
@Override
public void sendEngineMsg(EngineMQMsg engineMQMsg) throws Exception {
LocalMsgEvent localMsgEvent = new LocalMsgEvent();
localMsgEvent.setMessage(engineMQMsg);
localMsgEvent.setTags("Engine");
if (log.isDebugEnabled()) {
log.debug("EngineMQMsg Event Send: {}", engineMQMsg.getBatch());
}
disruptor.publishEvent((event, sequence, bind) -> {event.setBody(bind.getBody());event.setTags(localMsgEvent.getTags());}, localMsgEvent);
}
@Override
public void sendBuildMsg(EngineMQMsg engineMQMsg) throws Exception {
LocalMsgEvent localMsgEvent = new LocalMsgEvent();
localMsgEvent.setMessage(engineMQMsg);
localMsgEvent.setTags("Build");
if (log.isDebugEnabled()) {
log.debug("BuildMQMsg Event Send: {}", engineMQMsg.getBatch());
}
disruptor.publishEvent((event, sequence, bind) -> {event.setBody(bind.getBody());event.setTags(localMsgEvent.getTags());}, localMsgEvent);
}
@Override
public void sendResultsMsg(ResultsMQMsg resultsMQMsg) throws Exception {
LocalMsgEvent localMsgEvent = new LocalMsgEvent();
localMsgEvent.setMessage(resultsMQMsg);
if (log.isDebugEnabled()) {
log.debug("ResultsMQMsg Event Send: {}", resultsMQMsg.getKeyValueField());
}
disruptor.publishEvent((event, sequence, bind) -> event.setBody(bind.getBody()), localMsgEvent);
}
}
...@@ -23,8 +23,7 @@ import java.util.List; ...@@ -23,8 +23,7 @@ import java.util.List;
@Slf4j @Slf4j
@Component @Component
public class MQConsumeMsgListenerProcessor implements MessageListenerOrderly { public class MQConsumeMsgListenerProcessor implements MessageListenerOrderly {
public static final String Type_Engine = "Engine";
public static final String Type_Build = "Build";
private Object lockObject =new Object(); private Object lockObject =new Object();
...@@ -75,9 +74,9 @@ public class MQConsumeMsgListenerProcessor implements MessageListenerOrderly { ...@@ -75,9 +74,9 @@ public class MQConsumeMsgListenerProcessor implements MessageListenerOrderly {
// Thread.sleep(80000); // Thread.sleep(80000);
// System.out.println("end sleep"); // System.out.println("end sleep");
EngineMQMsg engineMQMsg = JSON.parseObject(body, new TypeReference<EngineMQMsg>() {}); EngineMQMsg engineMQMsg = JSON.parseObject(body, new TypeReference<EngineMQMsg>() {});
if(Type_Engine.equalsIgnoreCase(tags)) { if("Engine".equalsIgnoreCase(tags)) {
ruleEngineExService.processData(engineMQMsg); ruleEngineExService.processData(engineMQMsg);
}else if(Type_Build.equalsIgnoreCase(tags)){ }else if("Build".equalsIgnoreCase(tags)){
daBuildExService.processData(engineMQMsg); daBuildExService.processData(engineMQMsg);
} }
} catch (Exception e) { } catch (Exception e) {
......
package cn.ibizlab.core.extensions.util;
import cn.ibizlab.core.extensions.domain.EngineMQMsg;
import cn.ibizlab.core.extensions.domain.ResultsMQMsg;
public interface MsgProducerService {
public void sendEngineMsg(EngineMQMsg engineMQMsg) throws Exception;
public void sendBuildMsg(EngineMQMsg engineMQMsg) throws Exception;
public void sendResultsMsg(ResultsMQMsg resultsMQMsg) throws Exception;
}
...@@ -57,7 +57,8 @@ public class LiteDataService { ...@@ -57,7 +57,8 @@ public class LiteDataService {
} }
public boolean saveBatch(EntityModel entityModel, String dsName, List<EntityObj> list) { public boolean saveBatch(EntityModel entityModel, String dsName, List<EntityObj> list) {
splitList(list, 500).forEach(objs->{ List<List<EntityObj>> splist=splitList(list, 500);
splist.forEach(objs->{
getEntityService(StringUtils.isEmpty(dsName)?entityModel.getDsName():dsName).saveBatch(dsName,entityModel,objs); getEntityService(StringUtils.isEmpty(dsName)?entityModel.getDsName():dsName).saveBatch(dsName,entityModel,objs);
}); });
return true; return true;
...@@ -65,7 +66,8 @@ public class LiteDataService { ...@@ -65,7 +66,8 @@ public class LiteDataService {
public boolean saveBatch(String systemId, String entityName, String dsName, List<EntityObj> list) { public boolean saveBatch(String systemId, String entityName, String dsName, List<EntityObj> list) {
EntityModel entityModel = liteModelService.getEntityModel(systemId,entityName); EntityModel entityModel = liteModelService.getEntityModel(systemId,entityName);
splitList(list, 500).forEach(objs->{ List<List<EntityObj>> splist=splitList(list, 500);
splist.forEach(objs->{
getEntityService(StringUtils.isEmpty(dsName)?entityModel.getDsName():dsName).saveBatch(dsName,entityModel,objs); getEntityService(StringUtils.isEmpty(dsName)?entityModel.getDsName():dsName).saveBatch(dsName,entityModel,objs);
}); });
return true; return true;
...@@ -150,8 +152,9 @@ public class LiteDataService { ...@@ -150,8 +152,9 @@ public class LiteDataService {
EntityModel entityModel = dataModel.getFactEntityModel(); EntityModel entityModel = dataModel.getFactEntityModel();
List<ModelObj> rt = new ArrayList<>(); List<ModelObj> rt = new ArrayList<>();
List<EntityObj> factEntityList = new ArrayList<>(); List<EntityObj> factEntityList = new ArrayList<>();
List<List<EntityObj>> splist=splitList(kEntityObjs, 1000);
if (fillPropertys == null || fillPropertys.size() == 0 || fillPropertys.contains(dataModel.getFactPorperty().getPropertyName())) if (fillPropertys == null || fillPropertys.size() == 0 || fillPropertys.contains(dataModel.getFactPorperty().getPropertyName()))
splitList(kEntityObjs, 1000).forEach(list -> factEntityList.addAll(getEntityService(entityModel.getDsName()).selectBase(entityModel.getDsName(), entityModel, QueryFilter.createQuery().cust(dataModel.lookup(list))))); splist.forEach(list -> factEntityList.addAll(getEntityService(entityModel.getDsName()).selectBase(entityModel.getDsName(), entityModel, QueryFilter.createQuery().cust(dataModel.lookup(list)))));
factEntityList.forEach(entityObj -> factEntityList.forEach(entityObj ->
rt.add(new ModelObj().setDataModel(dataModel).setFactEntity(entityObj.setProperty(dataModel.getFactPorperty())))); rt.add(new ModelObj().setDataModel(dataModel).setFactEntity(entityObj.setProperty(dataModel.getFactPorperty()))));
fillEntityObj(dataModel, fillPropertys, rt); fillEntityObj(dataModel, fillPropertys, rt);
...@@ -162,6 +165,7 @@ public class LiteDataService { ...@@ -162,6 +165,7 @@ public class LiteDataService {
} }
public void fillEntityObj(DataModel dataModel, Set<String> fillPropertys, List<ModelObj> modelObjs) { public void fillEntityObj(DataModel dataModel, Set<String> fillPropertys, List<ModelObj> modelObjs) {
List<List<ModelObj>> splist=splitList(modelObjs, 1000);
if (dataModel.getObjectProperties().size() > 1) { if (dataModel.getObjectProperties().size() > 1) {
for (Property property : dataModel.getObjectProperties()) { for (Property property : dataModel.getObjectProperties()) {
if (property.getPropertyName().equals(dataModel.getFactPorperty().getPropertyName())) if (property.getPropertyName().equals(dataModel.getFactPorperty().getPropertyName()))
...@@ -169,7 +173,7 @@ public class LiteDataService { ...@@ -169,7 +173,7 @@ public class LiteDataService {
EntityModel entityModel = property.getEntityModel(); EntityModel entityModel = property.getEntityModel();
List<EntityObj> entityObjs = new ArrayList<>(); List<EntityObj> entityObjs = new ArrayList<>();
if (fillPropertys == null || fillPropertys.size() == 0 || fillPropertys.contains(property.getPropertyName())) if (fillPropertys == null || fillPropertys.size() == 0 || fillPropertys.contains(property.getPropertyName()))
splitList(modelObjs, 1000).forEach(list -> entityObjs.addAll(getEntityService(entityModel.getDsName()).selectBase(entityModel.getDsName(), entityModel, QueryFilter.createQuery().cust(property.lookup(list))))); splist.forEach(list -> entityObjs.addAll(getEntityService(entityModel.getDsName()).selectBase(entityModel.getDsName(), entityModel, QueryFilter.createQuery().cust(property.lookup(list)))));
entityObjs.forEach(entityObj -> { entityObjs.forEach(entityObj -> {
entityObj.setProperty(property); entityObj.setProperty(property);
...@@ -180,19 +184,31 @@ public class LiteDataService { ...@@ -180,19 +184,31 @@ public class LiteDataService {
} }
private <T> List<List<T>> splitList(List<T> list, int groupSize) {
int length = list.size(); public static <T> List<List<T>> splitList(List<T> list, int count){
// 计算可以分成多少组 List<List<T>> lists = new ArrayList<List<T>>(); // 结果集
int num = (length + groupSize - 1) / groupSize; // TODO
List<List<T>> newList = new ArrayList<>(num); int listSize = list.size(); // 原集合长度
for (int i = 0; i < num; i++) { int listCount = listSize / count; // 可直接拆分集合数量
// 开始位置
int fromIndex = i * groupSize; // 按量拆分
// 结束位置 for (int i = 0; i < listCount ; i++) {
int toIndex = (i + 1) * groupSize < length ? (i + 1) * groupSize : length; List<T> sub2=new ArrayList<>();
newList.add(list.subList(fromIndex, toIndex)); for(int j=i*count;j<(i + 1) * count;j++)
sub2.add(list.get(j));
lists.add(sub2);
} }
return newList;
// 如果按量拆分后还有剩余, 收尾
int remainder = listSize % count;
if(remainder > 0) {
List<T> sub2=new ArrayList<>();
for(int j=listSize - remainder;j<listSize;j++)
sub2.add(list.get(j));
lists.add(sub2);
}
return lists;
} }
......
package cn.ibizlab.core.util.config;
import cn.ibizlab.core.extensions.domain.LocalMsgEvent;
import cn.ibizlab.core.extensions.util.LocalMsgProducerService;
import cn.ibizlab.core.extensions.util.LocalMsgEventHandler;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.SleepingWaitStrategy;
import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import com.lmax.disruptor.util.DaemonThreadFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ThreadFactory;
@Configuration
@ConditionalOnExpression("(!${rocketmq.producer.enabled:false})&&(!${rocketmq.consumer.enabled:false})")
public class LocalMsgConfiguration {
@Bean
@ConditionalOnMissingBean
public WaitStrategy waitStrategy() {
return new SleepingWaitStrategy(200,1000*1000*10);
}
@Bean
@ConditionalOnMissingBean
public ThreadFactory threadFactory() {
return DaemonThreadFactory.INSTANCE;
}
@Bean
@ConditionalOnMissingBean
public LocalMsgEventHandler localMsgEventHandler() {
return new LocalMsgEventHandler();
}
@Bean
@ConditionalOnClass({Disruptor.class})
public Disruptor<LocalMsgEvent> disruptor(WaitStrategy waitStrategy, ThreadFactory threadFactory,
LocalMsgEventHandler localMsgEventHandler) {
Disruptor<LocalMsgEvent> disruptor = new Disruptor<>(() -> new LocalMsgEvent(), 1024 * 1024,
threadFactory, ProducerType.SINGLE, waitStrategy);
disruptor.handleEventsWith(localMsgEventHandler);
// 启动
disruptor.start();
// WEB 容器关闭执行
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
// OK
disruptor.shutdown();
// wait up to 10 seconds for the ringbuffer to drain
RingBuffer<LocalMsgEvent> ringBuffer = disruptor.getRingBuffer();
for (int i = 0; i < 20; i++) {
if (ringBuffer.hasAvailableCapacity(ringBuffer.getBufferSize())) {
break;
}
try {
// give ringbuffer some time to drain...
Thread.sleep(500);
} catch (InterruptedException e) {
// ignored
}
}
disruptor.shutdown();
} catch (Exception e) {
// to do nothing
}
}));
return disruptor;
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册