Disruptor的开发应用

        工作时遇到更新大量数据问题简单应用了一下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 mdcList ;

private Integer type;

private Map params;


public VinuxCenterMemberSellerMapper getSellerMapper() {
    return sellerMapper;
}

public void setSellerMapper(VinuxCenterMemberSellerMapper sellerMapper) {
    this.sellerMapper = sellerMapper;
}

public List getMdcList() {
    return mdcList;
}

public void setMdcList(List mdcList) {
    this.mdcList = mdcList;
}

public Integer getType() {
    return type;
}

public void setType(Integer type) {
    this.type = type;
}

public Map getParams() {
    return params;
}

public void setParams(Map 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 mdcList ;

private Integer type;

private Map params;


public VinuxCenterMemberSellerMapper getSellerMapper() {
    return sellerMapper;
}

public void setSellerMapper(VinuxCenterMemberSellerMapper sellerMapper) {
    this.sellerMapper = sellerMapper;
}

public List getMdcList() {
    return mdcList;
}

public void setMdcList(List mdcList) {
    this.mdcList = mdcList;
}

public Integer getType() {
    return type;
}

public void setType(Integer type) {
    this.type = type;
}

public Map getParams() {
    return params;
}

public void setParams(Map params) {
    this.params = params;
}

}
4package 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 params = new HashMap(); 

        VinuxCenterMemberSellerMapper sellerMapper = event.getSellerMapper();

        List 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
5disruptor的配置和调用
Map

你可能感兴趣的:(并发编程)