I would like to add some capabilities to the server. Firstly, it should accept and handle connections with multiple clients, so that there are no mixups between clients. Secondly, there should be some very basic JDBC connectivity: serialize a result set as a List, and then, as requested pop from the list and send that instance to a client for updates. When the client sends back an updated record, update the database accordingly.
package net.bounceme.dur.driver;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server {
private static final Logger log = Logger.getLogger(Server.class.getName());
private final RecordQueue recordsQueue = new RecordQueue();
public static void main(String[] args) {
Properties props = PropertiesReader.getProps();
int portNumber = Integer.parseInt(props.getProperty("port"));
while (true) {
try {
new Server().inOut(portNumber);
} catch (java.net.SocketException se) {
Logger.getLogger(Server.class.getName()).log(Level.FINE, "spammy", se);
} catch (IOException ioe) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ioe);
} catch (ClassNotFoundException cnf) {
Logger.getLogger(Server.class.getName()).log(Level.INFO, null, cnf);
}
}
}
public void inOut(int portNumber) throws IOException, ClassNotFoundException, java.net.SocketException {
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket socket = serverSocket.accept();
ObjectOutputStream objectOutputStream = null;
MyRecord recordFromClient = null;
try (ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream())) {
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
recordFromClient = (MyRecord) objectInputStream.readObject();
}
objectOutputStream.flush();
objectOutputStream.close();
log.info(recordFromClient.toString());
}
}
Is that a reasonable progression from this server code? Obviously, I would only add a single feature at a time. While I would love to learn Log4J or a similar testing framework, my immediate concern is adding functionality to server code.
What are some pitfalls I might run into? What would be the most pragmatic approach to increasing the functionality of the server side operations?
For example, I might start with a Queue, and then only later tie that into a database. Each client will only have access to a single record, so I'm not concerned about corrupt data. Will there be a problem when multiple clients are trying to access the Queue, however? The clients will only need pop and add, nothing more. The client will update, or modify, each record instance it receives.