日期:2014-05-18 浏览次数:20731 次
public class SyncQueue {
public static final ExecutorService executorService = Executors.newFixedThreadPool(10);
public static final BlockingQueue<String> queue = new ArrayBlockingQueue<String>(200);
public static final Deque<String> dq = new LinkedBlockingDeque<String>(10);
// public static boolean addjob(String jsonData) {
// return queue.offer(jsonData);
// }
}
public class Clean extends HttpServlet {
private static final long serialVersionUID = 1L;
private static Logger log = Logger.getLogger(Clean.class);
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String jsonData = request.getParameter("json");
if (jsonData != null && !jsonData.equals("")) {
if (SyncQueue.queue.offer(jsonData)) {//添加到缓存队列
SyncQueue.executorService.execute(new DealThread());//调用线程执行
log.info("ok," + jsonData);
response.sendError(HttpServletResponse.SC_OK);
} else {
log.info("full !!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + jsonData);
response.sendError(HttpServletResponse.SC_TEMPORARY_REDIRECT);
}
} else {
log.info("wrong param" + jsonData);
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
public class DealThread implements Runnable {
private static Logger log = Logger.getLogger(DealThread.class);
private String name = "";
public DealThread() {
}
public DealThread(String name) {
this.name = name;
}
@Override
public void run() {
String json = SyncQueue.queue.poll();
if (json != null) {
log.info("josn:" + json);
long start = System.currentTimeMillis();
Connection conn = DBPool.getInstance().getConnection("live");
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement("select * from channel where id=?");
pstmt.setInt(1, Integer.parseInt(json));
rs = pstmt.executeQuery();
while (rs.next()) {
log.info("thread," + Thread.currentThread().getName() + ":" + rs.getString("channelName"));
}
Thread.sleep(new Random().nextInt(4000));//for test模拟大任务
} catch (SQLException e) {