Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Which is a suitable architecture?
- public class cServer
- {
- private LinkedBlockingQueue<String> databaseQueue = new LinkedBlockingQueue<String>();
- class ConnectionHandler implements Runnable {
- ConnectionHandler(Socket receivedSocketConn1) {
- this.receivedSocketConn1=receivedSocketConn1;
- }
- // gets data from an inbound connection and queues it for databse update
- public void run(){
- databaseQueue.add(message); // put to db queue
- }
- }
- class DatabaseProcessor implements Runnable {
- public void run(){
- // open database connection
- createConnection();
- while (true){
- message = databaseQueue.take(); // keep taking message from the queue add by connectionhandler and here I will have a number of queries to run in terms of select,insert and updates.
- }
- }
- void createConnection(){
- System.out.println("Crerate Connection");
- connCreated = new Date();
- try{
- dbconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1?"+"user=user1&password=*******");
- dbconn.setAutoCommit(false);
- }
- catch(Throwable ex){
- ex.printStackTrace(System.out);
- }
- }
- }
- public void main()
- {
- new Thread(new DatabaseProcessor()).start(); //calls the DatabaseProcessor
- try
- {
- final ServerSocket serverSocketConn = new ServerSocket(8000);
- while (true){
- try{
- Socket socketConn1 = serverSocketConn.accept();
- new Thread(new ConnectionHandler(socketConn1)).start();
- }
- catch(Exception e){
- e.printStackTrace(System.out);
- }
- }
- }
- catch (Exception e){
- e.printStackTrace(System.out);
- }
- }
- }
Add Comment
Please, Sign In to add comment