简体   繁体   English

为要插入数据库的数据实现Queue

[英]Implementing Queue for data to be inserted in the database

We are developing a vehicle tracking system in which several GPS devices keep sending their GPS locations to the server using TCP connection. 我们正在开发一种车辆跟踪系统,其中几个GPS设备使用TCP连接将其GPS位置发送到服务器。 The TCP communicator decodes the GPS location and inserts that data into the database. TCP通信器解码GPS位置并将该数据插入数据库。 Right now, one thread of TCP communicator serves one device request. 现在,TCP通信器的一个线程服务于一个设备请求。 After decoding the GPS data, it creates a connection to the database and inserts the data and releases the connection. 解码GPS数据后,它会创建与数据库的连接并插入数据并释放连接。 As number of devices are increasing, the number of concurrent connections to the database (which is MySQL) are also increasing. 随着设备数量的增加,与数据库(即MySQL)的并发连接数也在增加。 Hence, I want to implement a queue where each thread of TCP communicator will push the data to one end and one job will take data from other end and keep it inserting into the database. 因此,我想实现一个队列,其中TCP通信器的每个线程将数据推送到一端,一个作业将从另一端获取数据并将其插入数据库。

Can anybody suggest me the best solution to handle all this? 谁能建议我处理这一切的最佳解决方案? Our application is based on Java and database is MySQL. 我们的应用程序基于Java,数据库是MySQL。

Thanks, Saurabh 谢谢,Saurabh

您可以使用类似ConcurrentLinkedQueue的线程安全队列实现来对数据进行排队

You could just create a simple Thread that handles the database writes. 您可以创建一个处理数据库写入的简单Thread Then have your communicator threads queue the data that needs to be written with it. 然后让你的通信器线程对需要用它写入的数据进行排队。 Something like this: 像这样的东西:

public class DatabaseQueue extends Thread {
    private LinkedBlockingQueue<Data> queue = new LinkedBlockingQueue<Data>();

    public void queueData(Data data) {
        queue.add(data);
    }

    public void run() {
        while (true) {
            Data data = queue.take();
            // write data to database
        }
    }
}

A plain concurrent queue is best if you are going to batch your data and it support easy batching techniques (and therefor database insert performance) 如果要批量处理数据并且支持简单的批处理技术(以及因此数据库插入性能),则最好的并发队列是最好的

However a more flexible approach if you want to do other things as well is to use an ExecutorService with a fixed numebr of threads. 但是,如果您想要执行其他操作,则更灵活的方法是使用具有固定数量线程的ExecutorService。 This way you can add tasks to do anything, to a limited degree of concurrency. 这样,您可以添加任务以执行任何操作,达到有限的并发度。

You need to implement a message queue. 您需要实现消息队列。

Have a look at either RabbitMQ or ActiveMQ . 看看RabbitMQActiveMQ

one job will take data from other end and keep it inserting into the database. 一个作业将从另一端获取数据并将其插入数据库中。

ConcurrentLinkedQueue is just an overkill, it is for more than one thread to access lock free.The poll method take unnecessary sleep intervals,also u need to wait till the queue is filled , again another kind of sleep. ConcurrentLinkedQueue只是一种矫枉过正,它可以让多个线程无法访问锁定。 poll方法需要不必要的休眠时间间隔,也需要等到队列填满,再次进行另一种休眠。 If you only have one thread putting stuff into the queue, and another thread taking stuff out of the queue 如果你只有一个线程将东西放入队列,另一个线程将东西从队列中取出

Queue<GPRSObj> queue = Collections.synchronizedList(new LinkedList<GPRSObj>());

or 要么

LinkedBlockingQueue  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM