Archived
0

modify emdn threads executor

This commit is contained in:
iMoHax
2014-09-04 14:12:11 +04:00
parent fb8dfeb42d
commit 9e104943f7
4 changed files with 93 additions and 91 deletions

View File

@@ -6,75 +6,61 @@ import org.zeromq.ZMQ;
import org.zeromq.ZMQException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
public class EMDN {
private final static Logger LOG = LoggerFactory.getLogger(EMDN.class);
private final Market cache = new Market();
private final ConcurrentHashMap<String, Station> cache = new ConcurrentHashMap<>(40, 0.9f, 1);
private final ZMQ.Context context;
private ExecutorService executor;
private String subServer;
private ZMQ.Context context = null;
private ZMQ.Socket subscriber = null;
private ScheduledExecutorService executor;
private ScheduledFuture<?> receive;
private Future<?> receive;
private boolean clear;
public EMDN() {
context = ZMQ.context(1);
}
public EMDN(String subServer, boolean clearOnShutdown) {
this();
this.subServer = subServer;
clear = clearOnShutdown;
}
private void init(){
context = ZMQ.context(1);
subscriber = context.socket(ZMQ.SUB);
}
public void start(){
if (isActive()) return;
init();
LOG.info("Connect to server {}", subServer);
subscriber.connect(subServer);
LOG.trace("Subscribe");
subscriber.subscribe(new byte[0]);
executor = Executors.newSingleThreadScheduledExecutor();
receive = executor.scheduleWithFixedDelay(() -> {
try {
byte[] receivedData = subscriber.recv(0);
LOG.trace("Received data: {}", receivedData);
if (receivedData == null) return;
//receivedData = decompress(receivedData);
String market_csv = new String(receivedData, "UTF-8");
parseCSV(market_csv);
} catch (ZMQException | UnsupportedEncodingException ex) {
if (!executor.isShutdown())
LOG.error("Error on get data from EMDN", ex);
}
}, 0, 1, TimeUnit.MILLISECONDS);
if (executor == null) executor = Executors.newSingleThreadExecutor();
receive = executor.submit(new Receiver());
}
public void stop() {
if (isActive()){
LOG.info("Stop EMDN client");
receive.cancel(false);
receive = null;
if (clear)
cache.clear();
}
}
public void shutdown() {
if (isActive()){
LOG.info("Shutdown EMDN client");
receive.cancel(false);
LOG.info("Shutdown EMDN client");
stop();
if (executor != null) {
executor.shutdown();
subscriber.close();
context.term();
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException ignore) {
}
}
subscriber = null;
context = null;
if (clear)
cache.clear();
context.term();
}
private void parseCSV(String csv) {
LOG.debug("Parse csv: {}", csv);
if (csv.isEmpty()) return;
@@ -86,7 +72,7 @@ public class EMDN {
LOG.trace("Item: {}", item);
String stName = flds[8].split("\\(")[0].trim();
LOG.trace("Station: {}", stName);
Station station = cache.getVendor(stName);
Station station = cache.get(stName);
if (station != null){
LOG.trace("Is old, update");
station.update(item);
@@ -94,7 +80,7 @@ public class EMDN {
LOG.trace("Is new, create");
station = new Station(stName);
station.update(item);
cache.addVendor(station);
cache.put(stName, station);
}
}
@@ -116,19 +102,51 @@ public class EMDN {
return res;
}
public Station getVendor(String name){
return cache.getVendor(name);
public Station get(String name){
return cache.get(name);
}
public Station pop(String name) {
return cache.remove(name);
}
public boolean isActive(){
return subscriber!=null;
return receive!=null;
}
public void connectTo(String subServer){
if (subServer.equals(this.subServer)) return;
boolean active = isActive();
if (active) shutdown();
if (active) stop();
this.subServer = subServer;
if (active) start();
}
private class Receiver implements Runnable {
@Override
public void run() {
try (ZMQ.Socket subscriber = context.socket(ZMQ.SUB)){
subscriber.setReceiveTimeOut(10000);
LOG.info("Connect to server {}", subServer);
subscriber.connect(subServer);
LOG.trace("Subscribe");
subscriber.subscribe(new byte[0]);
while (!executor.isShutdown() && !receive.isCancelled()){
try {
byte[] receivedData = subscriber.recv(0);
LOG.trace("Received data: {}", receivedData);
if (receivedData == null) return;
//receivedData = decompress(receivedData);
String market_csv = new String(receivedData, "UTF-8");
parseCSV(market_csv);
} catch (ZMQException | UnsupportedEncodingException ex) {
LOG.error("Error on get data from EMDN", ex);
}
}
} catch (Exception ex){
LOG.error("Error on connect to EMDN", ex);
}
}
}
}

View File

@@ -1,19 +0,0 @@
package ru.trader.emdn;
import java.util.concurrent.ConcurrentHashMap;
public class Market {
private final ConcurrentHashMap<String, Station> vendors = new ConcurrentHashMap<>(40, 0.9f, 1);
public Station getVendor(String name){
return vendors.get(name);
}
public void addVendor(Station vendor){
vendors.put(vendor.getName(), vendor);
}
public void clear(){
vendors.clear();
}
}

View File

@@ -20,7 +20,7 @@ public class EMDNTest extends Assert {
public void testGetData() throws Exception {
// wait submit
Thread.sleep(4000);
Station station = markettool.getVendor("Eranin");
Station station = markettool.get("Eranin");
assertNotNull(station);
ItemData itemData = station.getData("cropharvesters");
assertNotNull(itemData);