ConcurrentLinkedQueue

package net;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DeviceTokenTest {

	private static Log logger = LogFactory.getLog(DeviceTokenTest.class);

	public static void main(String[] args) {
		// testCase();
		testCase2();

	}

	public static void testCase2() {
		ConcurrentLinkedQueue<String> deviceTokenQueue = new ConcurrentLinkedQueue<String>();
		deviceTokenQueue.add("1");
		deviceTokenQueue.add("2");

		List<String> list = new ArrayList<String>();
		list.add("3");
		list.add("4");
		list.add("5");

		deviceTokenQueue.addAll(list);

		while (!deviceTokenQueue.isEmpty()) {
			String str = deviceTokenQueue.remove();
			System.err.println(str);
		}
	}

	public static void testCase() {
		DeviceTokenManager manager = new DeviceTokenManager("1");
		manager.init();

		while (true) {
			if (manager.isComplete() && manager.isEmpty()) {
				break;
			}
			sendPushMessage(manager.getDeviceTokenEx());
		}
	}

	public static void sendPushMessage(String deviceToken) {
		logger.info("consume deviceToken...");
	}
}


package net;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import app.db.PoolingDriverExample;

public class DeviceTokenManager {

	private Log logger = LogFactory.getLog(DeviceTokenManager.class);

	private ConcurrentLinkedQueue<String> deviceTokenQueue = new ConcurrentLinkedQueue<String>();

	private int MAX_QUEUE = 1000;
	private int TOTAL_RECORD = 0;
	private int TOTAL_PAGE = 0;
	private int CURRENT_PAGE = 0;
	private volatile boolean COMPLETE = false;

	private String appId;

	public DeviceTokenManager(String appId) {
		if (null == deviceTokenQueue) {
			deviceTokenQueue = new ConcurrentLinkedQueue<String>();
		}

		this.appId = appId;
	}

	public void init() {
		logger.info("initializing parameter...");
		this.TOTAL_RECORD = DeviceTokenDaoImpl.getDeviceTokenTotalCount(appId);
		this.TOTAL_PAGE = TOTAL_RECORD % MAX_QUEUE == 0 ? TOTAL_RECORD / MAX_QUEUE : TOTAL_RECORD / MAX_QUEUE + 1;
		this.CURRENT_PAGE = 0;
		logger.info("initializing complete...");
	}

	public boolean isEmpty() {
		// import sun.nio.cs.ext.ISCII91;
		return deviceTokenQueue.isEmpty();
	}

	public boolean isComplete() {
		return COMPLETE;
	}

	public String getDeviceTokenEx() {
		if (isEmpty()) {
			pumpEx();
		}
		if (isEmpty()) {
			return null;
		}
		return deviceTokenQueue.remove();
	}

	public void pumpEx() {
		if (isComplete()) {
			logger.info("all device token pumped...");
		} else {
			List<String> list = DeviceTokenDaoImpl.getDeviceTokenByPage(appId, CURRENT_PAGE, MAX_QUEUE);
			if (0 == list.size()) {
				logger.info("all device token pumped...");
				COMPLETE = true;
			} else {
				deviceTokenQueue.addAll(list);
				++CURRENT_PAGE;
			}
		}
	}

}

class DeviceTokenDaoImpl {

	private static Log logger = LogFactory.getLog(DeviceTokenDaoImpl.class);

	private static final String QUERY_DEVICE_RECORD = "SELECT t.deviceToken FROM t_subscribeUser l, t_deviceToken r, WHERE l.jid = r.username AND r.deviceToken IS NOT NULL AND r.subId = ?;";
	private static final String QUERY_DEVICE_RECORD_BY_PAGE = "SELECT t.deviceToken FROM t_subscribeUser l, t_deviceToken r, WHERE l.jid = r.username AND r.deviceToken IS NOT NULL AND r.subId = ? LIMIT ?, ?;";
	private static final String QUERY_DEVICE_RECORD_TOTAL = "SELECT COUNT(r.deviceToken) FROM t_subscribeUser l, t_deviceToken r, WHERE l.jid = r.username AND r.deviceToken IS NOT NULL AND r.subId = ?;";

	public static int getDeviceTokenTotalCount(String appId) {

		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;

		try {
			conn = PoolingDriverExample.getConnection();
			pst = conn.prepareStatement(QUERY_DEVICE_RECORD_TOTAL);
			pst.setString(1, appId);
			rs = pst.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
		} finally {
			PoolingDriverExample.close(rs);
			PoolingDriverExample.close(pst);
			PoolingDriverExample.close(conn);
		}
		return 0;
	}

	public static List<String> getDeviceTokenByPage(String appId, int page, int range) {

		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;

		List<String> list = new ArrayList<String>();

		try {
			conn = PoolingDriverExample.getConnection();
			pst = conn.prepareStatement(QUERY_DEVICE_RECORD_BY_PAGE);
			pst.setString(1, appId);
			pst.setInt(1, page * range);
			pst.setInt(2, range);

			rs = pst.executeQuery();
			while (rs.next()) {
				list.add(rs.getString("deviceToken"));
			}
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
		} finally {
			PoolingDriverExample.close(rs);
			PoolingDriverExample.close(pst);
			PoolingDriverExample.close(conn);
		}
		return list;
	}
}

你可能感兴趣的:(Concurrent)