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()));
}
}
@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);
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 + "秒");
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();
}
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",
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;
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), commonService);
endpoint.publish("/CommonService");
return endpoint;
}
@Autowired
private IStoreInfoWebService storeInfoServiceImpl;
@Bean
public Endpoint storeInfoEndpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), storeInfoServiceImpl);
endpoint.publish("/storeInfoServiceImpl");
return endpoint;
}
@Autowired
private IStoreInfoVisitWebService storeInfoVisitServiceImpl;
@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;
}
}