Guest User

Untitled

a guest
Aug 1st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. Which is a suitable architecture?
  2. public class cServer
  3. {
  4. private LinkedBlockingQueue<String> databaseQueue = new LinkedBlockingQueue<String>();
  5.  
  6. class ConnectionHandler implements Runnable {
  7. ConnectionHandler(Socket receivedSocketConn1) {
  8. this.receivedSocketConn1=receivedSocketConn1;
  9. }
  10.  
  11.  
  12. // gets data from an inbound connection and queues it for databse update
  13.  
  14. public void run(){
  15. databaseQueue.add(message); // put to db queue
  16. }
  17. }
  18. class DatabaseProcessor implements Runnable {
  19. public void run(){
  20. // open database connection
  21. createConnection();
  22. while (true){
  23. 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.
  24. }
  25. }
  26.  
  27. void createConnection(){
  28. System.out.println("Crerate Connection");
  29. connCreated = new Date();
  30. try{
  31. dbconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1?"+"user=user1&password=*******");
  32. dbconn.setAutoCommit(false);
  33. }
  34. catch(Throwable ex){
  35. ex.printStackTrace(System.out);
  36. }
  37. }
  38. }
  39.  
  40. public void main()
  41. {
  42. new Thread(new DatabaseProcessor()).start(); //calls the DatabaseProcessor
  43. try
  44. {
  45. final ServerSocket serverSocketConn = new ServerSocket(8000);
  46. while (true){
  47. try{
  48. Socket socketConn1 = serverSocketConn.accept();
  49. new Thread(new ConnectionHandler(socketConn1)).start();
  50. }
  51. catch(Exception e){
  52. e.printStackTrace(System.out);
  53. }
  54. }
  55. }
  56. catch (Exception e){
  57. e.printStackTrace(System.out);
  58. }
  59.  
  60. }
  61.  
  62. }
Add Comment
Please, Sign In to add comment