package com.central.core.socket.asynpush.threadWorkers;
import com.central.core.socket.asynpush.CyNettyModel;
import com.central.core.socket.asynpush.concurrent.ThreadFactoryImpl;
import com.central.core.socket.netty.util.NettyClient;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Slf4j
public class CyNettyQueue implements DataTransfer {
private NettyClient nettyClient = ApplicationContextUtil.getBean(NettyClient.class);
private BlockingQueue<CyNettyModel> productQueue;
private final int poolSize;
ExecutorService exService;
public CyNettyQueue(BlockingQueue<CyNettyModel> productQueue, int poolSize) {
super();
this.productQueue = productQueue;
this.poolSize = poolSize;
exService = Executors.newFixedThreadPool(poolSize, new ThreadFactoryImpl("msgTrans_ProductQueue_", false));
}
@Override
public void run() {
for (int i = 0; i < poolSize; i++) {
exService.execute(new Runnable() {
@Override
public void run() {
while (true) {
CyNettyModel pcCenterOrder = null;
try {
pcCenterOrder = productQueue.take();
if (pcCenterOrder == null) {
continue;
}
nettyClient.connectServer(pcCenterOrder.getImei(), pcCenterOrder.getMsg(),pcCenterOrder.getIccid());
log.info("成研队列大小为--->"+productQueue.size());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
});
}
}
@Override
public void start() throws Exception {
new Thread(this).start();
}
}