@@ -0,0 +1,1959 @@
+/**%0A * Contains classes for server that execute database queries%0A * for multiple clients.%0A */%0Apackage goodsbase.qserver;%0A%0Aimport goodsbase.database.DbConnection;%0A%0Aimport java.sql.Connection;%0Aimport java.sql.SQLException;%0Aimport java.util.concurrent.BlockingQueue;%0Aimport java.util.concurrent.LinkedBlockingQueue;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A%0A/**%0A * Executes database tasks. Blocks DbTask object until the computation is done.%0A * Notifies when task is complete.%0A * When thread is interrupted declines new tasks.%0A * Finishes working when all tasks are complete.%0A * @author Daria%0A */%0Apublic class QueryExecutor implements Runnable %7B%0A%09%0A%09/**%0A%09 * @throws SQLException when cannot connect to database%0A%09 */%0A%09public QueryExecutor() throws SQLException %7B%0A%09%09connection = DbConnection.getConnection();%09%09%0A%09%7D%0A%09%0A%09%0A%09@Override%0A%09public void run() %7B%0A%09%0A%09%09while (true)%7B%09%09%09%0A%09%09%09try %7B%0A%09%09%09%09execTask(queue.take());%0A%09%09%09%7D catch (InterruptedException e) %7B%09%0A%09%09%09%09/*stop accepting new tasks*/%0A%09%09%09%09interrupted = true;%09%0A%09%09%09%09/*complete all remaining tasks*/%0A%09%09%09%09while(!queue.isEmpty()) %7B%0A%09%09%09%09%09execTask(queue.remove());%0A%09%09%09%09%7D%0A%09%09%09%09/*stop service*/%0A%09%09%09%09log.info(%22Stopping query executor service...%22);%0A%09%09%09%09try %7B%0A%09%09%09%09%09connection.close();%0A%09%09%09%09%7D catch (SQLException e1) %7B%0A%09%09%09%09%09log.log(Level.WARNING, %22Exception caught when closing connection%22, e1);%0A%09%09%09%09%7D%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%7D%09%0A%09%7D%0A%09%0A%09/** Adds DbTask to the queue%0A%09 * @return true if task is accepted*/%0A%09public boolean addTask(DbTask task) %7B%0A%09%09if(!interrupted)%0A%09%09%09return queue.offer(task);%0A%09%09else return false;%0A%09%7D%0A%09%0A%09private void execTask(DbTask task)%7B%0A%09%09synchronized(task) %7B%0A%09%09%09task.execute(connection);%0A%09%09%09/*notify the waiting thread*/%0A%09%09%09task.notify();%0A%09%09%7D%0A%09%7D%0A%09%0A%09private static final BlockingQueue%3CDbTask%3E queue = new LinkedBlockingQueue%3CDbTask%3E();%0A%09private static final Logger log = Logger.getLogger(QueryExecutor.class.getName());%0A%09private static Connection connection; %0A%09private boolean interrupted = false;%0A%7D%0A

add QueryExecutor new file: src/goodsbase/qserver/QueryExecutor.java