工作时遇到更新大量数据问题简单应用了一下Disruptor。Disruptor被称为一个并发框架,但主要是一个数据队列,想要掌握它,应先学习线程容器:BlockingQueue等知识 ,并清晰的认识:多个生产者---队列---多个消费者的模式。
1导jar包
2 创建一个基本的包装类
`package vinux.web.openApi.controller;
import java.util.List;
import java.util.Map;
import vinux.service.dao.VinuxCenterMemberSellerMapper;
import vinux.service.entity.VinuxCenterMemberSeller;
public class Element {
private VinuxCenterMemberSellerMapper sellerMapper;
private List<VinuxCenterMemberSeller> mdcList ;
private Integer type;
private Map<String, Object> params;
public VinuxCenterMemberSellerMapper getSellerMapper() {
return sellerMapper;
}
public void setSellerMapper(VinuxCenterMemberSellerMapper sellerMapper) {
this.sellerMapper = sellerMapper;
}
public List<VinuxCenterMemberSeller> getMdcList() {
return mdcList;
}
public void setMdcList(List<VinuxCenterMemberSeller> mdcList) {
this.mdcList = mdcList;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Map<String, Object> getParams() {
return params;
}
public void setParams(Map<String, Object> params) {
this.params = params;
}
}
3这是一个添加到队列的类
package vinux.web.openApi.controller;
import java.util.List;
import java.util.Map;
import vinux.service.dao.VinuxCenterMemberSellerMapper;
import vinux.service.entity.VinuxCenterMemberSeller;
public class Element {
private VinuxCenterMemberSellerMapper sellerMapper;
private List<VinuxCenterMemberSeller> mdcList ;
private Integer type;
private Map<String, Object> params;
public VinuxCenterMemberSellerMapper getSellerMapper() {
return sellerMapper;
}
public void setSellerMapper(VinuxCenterMemberSellerMapper sellerMapper) {
this.sellerMapper = sellerMapper;
}
public List<VinuxCenterMemberSeller> getMdcList() {
return mdcList;
}
public void setMdcList(List<VinuxCenterMemberSeller> mdcList) {
this.mdcList = mdcList;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Map<String, Object> getParams() {
return params;
}
public void setParams(Map<String, Object> params) {
this.params = params;
}
}
4
package vinux.web.openApi.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import vinux.service.dao.VinuxCenterMemberSellerMapper;
import vinux.service.entity.VinuxCenterMemberSeller;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.WorkHandler;
/**
*
* updateProductWhereC_PRODUCT_COUNT\\
*
* dayStartTime
*
* dayEndTime
* */
public class Handler1 implements EventHandler, WorkHandler {
@Override
public void onEvent(Element event) throws Exception {
try {
SimpleDateFormat myFmt1=new SimpleDateFormat("yyyy-MM-dd");
Map<String, Object> params = new HashMap<String, Object>();
VinuxCenterMemberSellerMapper sellerMapper = event.getSellerMapper();
List<VinuxCenterMemberSeller> mdcList = event.getMdcList();
for (VinuxCenterMemberSeller vinuxCenterMemberSeller : mdcList) {
Date calculateDate = vinuxCenterMemberSeller.getCalculateDate();
String format = myFmt1.format(calculateDate);
params.put("dayStartTime", format+" 00:00:00");
params.put("dayEndTime", format+" 23:59:59");
params.put("id", vinuxCenterMemberSeller.getUserId());
sellerMapper.updateProductWhereC_PRODUCT_COUNT(params);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onEvent(Element event, long arg1, boolean arg2) throws Exception {
this.onEvent(event);
}
}
这里可以看成一个Service
Map
5disruptor的配置和调用