2020-11-13 ThreadPoolExecutor

1 篇文章 0 订阅
package com.want.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.want.mapper187.StoreInfoMapper;
import com.want.response.ResponseEntity;
import com.want.service.IStoreInfoWebService;
import com.want.vo.Page;
import com.want.vo.StoreInfo;
import com.want.vo.StoreVisitTag;

@Service
public class StoreInfoServiceImpl implements IStoreInfoWebService {

	private static final Logger LOG = LoggerFactory.getLogger(StoreInfoServiceImpl.class);

	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");

	@Autowired
	private StoreInfoMapper mapper;

	private final static Integer BATCH_SIZE = 50;
	private final static Integer corePoolSize = 4; // 线程池中核心线程数的最大值
	private final static Integer maximumPoolSize = 4; // 线程池中能拥有最多线程数

	private static ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 60L,
			TimeUnit.MINUTES, new SynchronousQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy());

	@Override
	@Transactional
	public ResponseEntity syncStoreInfo(List<StoreInfo> list, Page page) {
		ResponseEntity result = new ResponseEntity();
		if (list == null || list.size() <= 0) {
			result.setType("E");
			result.setMessage("门店信息数据不可为空");
			return result;
		}
		for (int i = 0; i < page.getTotal(); i++) {
			StoreInfo temp = new StoreInfo();
			BeanUtils.copyProperties(list.get(0), temp);
			temp.setRegionalName((i + 1) + "");
			list.add(temp);
		}
		String beginDate = dateFormat.format(new Date());
		LOG.info("----StoreInfoServiceImpl.syncStoreInfo--begindate:" + beginDate);
		try {
			if (page.getCurrent() == 1) {
				// 首次推送数据 先删除历史数据
				LOG.info("开始删除历史数据");
				mapper.clearStoreInfoTag();
				mapper.deleteStoreInfo();
				LOG.info("删除历史数据成功");
			}
			new Thread(()->{
				save(list);
			}).start();
			result.setType("S");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setType("E");
			result.setMessage("同步失败:" + e.toString());
			LOG.error("同步失败:" + e.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return result;
		} finally {
			LOG.info("beginDate:" + beginDate + ",enddate:" + dateFormat.format(new Date()));
		}

	}

	// 保存数据到DB
	@SuppressWarnings("finally")
	private ResponseEntity save(List<StoreInfo> list) {
		ResponseEntity result = new ResponseEntity();
		// 分几批次执行
		Integer times = (list.size() + BATCH_SIZE - 1) / BATCH_SIZE;
		
		long startTime = System.currentTimeMillis();
		CountDownLatch countDownLatch = new CountDownLatch(times);// corePoolSize
		try {
			for (int i = 0; i < times; i++) {
				long start = System.currentTimeMillis();
				int begin = i * BATCH_SIZE;
				int end = (i + 1) * BATCH_SIZE >= list.size() ?list.size():(i + 1) * BATCH_SIZE;
				executor.execute(() -> { 
					try {
						synchronized(this) {
							// 构建主键和标签
							SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMdd");
							List<StoreInfo> storeInfoList=new ArrayList<StoreInfo>();
							List<StoreVisitTag> storeVisitTagList = new ArrayList<StoreVisitTag>();
							int cot=0;
							for (StoreInfo it : list.subList(begin, end)) {
								String randomStr = ((int) ((Math.random() * 9 + 1) * 1000000000)) + "";
								String sid = dateFormat1.format(new Date()) + UUID.randomUUID()+(++cot);
								it.setSid(sid);
								it.getLabelFees().stream().forEach(item -> {
									storeVisitTagList .add(new StoreVisitTag(sid, "1", item.getDate(), item.getName(), it.getDataTime()));
								});
								it.getCounterMark().stream().forEach(item -> {
									storeVisitTagList .add(new StoreVisitTag(sid, "2", item.getDate(), item.getName(), it.getDataTime()));
								});
								storeInfoList.add(it);
							}
							// 保存门店信息和标签
							mapper.insertBatchStoreInfo(storeInfoList);
							mapper.insertBatchStoreInfoTag(storeVisitTagList);
						} 
					} finally {
						countDownLatch.countDown();
					}
				});
				
				LOG.info("第" + (begin / BATCH_SIZE + 1) + "批数据写入" + (System.currentTimeMillis() - start) / 1000 + "秒");
			}
			try {
				countDownLatch.await();
			} catch (Exception e) {
				e.printStackTrace();
			}
			LOG.info(times + "个线程执行用时: " + (System.currentTimeMillis() - startTime) / 1000 + "秒");
//			executor.shutdown();
//			 while(true){
//		            if(executor.isTerminated()){
//		                break;
//		            }
//		        }
			result.setType("S");
			result.setMessage("同步门店信息成功");
		} catch (Exception e) {
			LOG.error("Exception...错误:{}", e);
			e.printStackTrace();
			result.setType("E");
			result.setMessage("同步失败:"+e.toString());
			LOG.error("同步失败:"+e.toString());
		} finally {
			return result;
		}
	}

	public static void main(String[] args) throws Exception {
		try {
			System.out.println(1 / 0);
		} catch (Exception e2) {
			for (int i = 0; i < 10; i++) {
				System.out.println(i);
			}
			Thread t = new Thread(() -> {
				System.out.println("Thread-----");
				try {
					Thread.sleep(1000);
					System.out.println("----起来搞事情啦 -----");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
			t.start();
			// t.join();
		}

		System.out.println("22222");
	}
}
package com.want.service;

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import com.want.response.ResponseEntity;
import com.want.vo.Page;
import com.want.vo.StoreInfo;

@WebService(serviceName = "IStoreInfoWebService", // 与接口中指定的name一致
		targetNamespace = "http://service.want.com", // 与接口中的命名空间一致,一般是接口的包名倒
		endpointInterface = "com.want.service.IStoreInfoWebService"// 接口地址
)

public interface IStoreInfoWebService {
	@WebMethod
	@WebResult(name = "String", targetNamespace = "")
	ResponseEntity syncStoreInfo(@WebParam(name = "storeInfo") List<StoreInfo> list,@WebParam(name = "page")Page page);
}

package com.want.config.webservice;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.want.controller.CustomerBusinessInfoWebServiceController;
import com.want.controller.ProdStructWebServiceController;
import com.want.service.CommonService;
import com.want.service.IStoreInfoVisitWebService;
import com.want.service.IStoreInfoWebService;

@Configuration
public class WebServiceConfig {

	
    @SuppressWarnings({ "rawtypes", "unchecked" })
	@Bean
    public ServletRegistrationBean disServlet(){
        return new ServletRegistrationBean(new CXFServlet(),"/WebService/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus()
    {
        return  new SpringBus();
    }
    
    @Autowired
	private CommonService commonService;
 
	/** JAX-WS **/
	@Bean
	public Endpoint endpoint() {
		EndpointImpl endpoint = new EndpointImpl(springBus(), commonService);
		endpoint.publish("/CommonService");
		return endpoint;
	}
	
	@Autowired
	private IStoreInfoWebService storeInfoServiceImpl;
	
	/** JAX-WS **/
	@Bean
	public Endpoint storeInfoEndpoint() {
		EndpointImpl endpoint = new EndpointImpl(springBus(), storeInfoServiceImpl);
		endpoint.publish("/storeInfoServiceImpl");
		return endpoint;
	}
	
	@Autowired
	private IStoreInfoVisitWebService storeInfoVisitServiceImpl;
	
	/** JAX-WS **/
	@Bean
	public Endpoint StoreInfoVisitEndpoint() {
		EndpointImpl endpoint = new EndpointImpl(springBus(), storeInfoVisitServiceImpl);
		endpoint.publish("/storeInfoVisitServiceImpl");
		return endpoint;
	}
	@Bean
    public ProdStructWebServiceController prodStructService()
    {
        return  new ProdStructWebServiceController();
    }
   
    @Bean
    public Endpoint prodStructEndpoint() {
        EndpointImpl endpoint=new EndpointImpl(springBus(), prodStructService());
        endpoint.publish("/syncProdStruct"); 
        return endpoint;
    }
    @Bean
    public CustomerBusinessInfoWebServiceController customerBusinessInfoService()
    {
        return  new CustomerBusinessInfoWebServiceController();
    }
   
    @Bean
    public Endpoint customerBusinessInfoServiceEndpoint() {
        EndpointImpl endpoint=new EndpointImpl(springBus(), customerBusinessInfoService());
        endpoint.publish("/syncCustomerBusinessInfo"); 
        return endpoint;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值