import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import net.sf.json.JSONObject;
public class SobeyReceiveThread {
static ReceiveJsonDataService receiveJsonDataService = (ReceiveJsonDataService) SpringContextHolder
.getBean("receiveJsonDataService");
static Logger logger = Logger.getLogger(SobeyReceiveThread.class);
static final Object syncobject = new Object();
static Queue<String> receivedata = new LinkedBlockingQueue<String>();
static boolean isRunThread = false;
static Thread workThread = null;
// static Queue<HistoryLibItem> resultQueue = new
// LinkedList<HistoryLibItem>();
public static void start() {
if (isRunThread)
return;
isRunThread = true;
workThread = new Thread(doWork);
workThread.setName(SobeyReceiveThread.class.getSimpleName());
workThread.start();
}
public static void stop() {
if (workThread != null && workThread.isAlive()) {
workThread.interrupt();
}
workThread = null;
}
public static void addJsonData(String jsondata) {
if (jsondata == null)
return;
synchronized (syncobject) {
logger.info("SobeyReceiveThread get jsondata.........");
receivedata.add(jsondata);
}
}
static Runnable doWork = new Runnable() {
public void run() {
logger.info("--------- start convert jsondata work ----------");
while (isRunThread) {
while (receivedata.size() > 0) {
try {
logger.info("get saveJsondata..........");
receiveJsonDataService.saveJsondata(receivedata.poll());
} catch (Exception e) {
// TODO: handle exception
logger.error("Error occured in request thread:" + e.getMessage());
}
try {
Thread.sleep(Constant.Rolltime * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
};
}
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import net.sf.json.JSONObject;
public class SobeyReceiveThread {
static ReceiveJsonDataService receiveJsonDataService = (ReceiveJsonDataService) SpringContextHolder
.getBean("receiveJsonDataService");
static Logger logger = Logger.getLogger(SobeyReceiveThread.class);
static final Object syncobject = new Object();
static Queue<String> receivedata = new LinkedBlockingQueue<String>();
static boolean isRunThread = false;
static Thread workThread = null;
// static Queue<HistoryLibItem> resultQueue = new
// LinkedList<HistoryLibItem>();
public static void start() {
if (isRunThread)
return;
isRunThread = true;
workThread = new Thread(doWork);
workThread.setName(SobeyReceiveThread.class.getSimpleName());
workThread.start();
}
public static void stop() {
if (workThread != null && workThread.isAlive()) {
workThread.interrupt();
}
workThread = null;
}
public static void addJsonData(String jsondata) {
if (jsondata == null)
return;
synchronized (syncobject) {
logger.info("SobeyReceiveThread get jsondata.........");
receivedata.add(jsondata);
}
}
static Runnable doWork = new Runnable() {
public void run() {
logger.info("--------- start convert jsondata work ----------");
while (isRunThread) {
while (receivedata.size() > 0) {
try {
logger.info("get saveJsondata..........");
receiveJsonDataService.saveJsondata(receivedata.poll());
} catch (Exception e) {
// TODO: handle exception
logger.error("Error occured in request thread:" + e.getMessage());
}
try {
Thread.sleep(Constant.Rolltime * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
};
}