67,515
社区成员
发帖
与我相关
我的任务
分享
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) {
retry(json);
log.error(" sql exception ;", e);
} catch (InterruptedException e) {
retry(json);
log.error(" interrupted; ", e);
} finally {
DBPool.close(pstmt, rs, conn);
}
long exeTime = System.currentTimeMillis() - start;
log.info("thread," + Thread.currentThread().getName() + ":" + exeTime + "ms");
}
}
private void retry(String json) {
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) {
log.error(" sql exception ;", e);
} catch (InterruptedException e) {
log.error(" interrupted; ", e);
} finally {
DBPool.close(pstmt, rs, conn);
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String jsonData = request.getParameter("json");
PrintWriter out = response.getWriter();
if (jsonData != null && !jsonData.equals("")) {
try{
SyncQueue.exe.execute(new DealThread2(jsonData));
out.print("ok");
} catch (RejectedExecutionException e) {
out.print("full");
}
} else {
log.info("wrong param" + jsonData);
out.print("wrong param");
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
}