oop - Java design pattern for notifications -


i have web based application sends messages client server every time action has been performed in backend. now, i'm writing few tests see if messages being sent client expected messages. each action, when executed, generates message sent across client. in test, i've created websocket client listen server. when perform action, should check whether expected message action being sent. note server takes time send message.

the following pseudo code:

class applicationtest     public void checkifeventcreatedmsgissent() {         application.createnewevent("id101");         // wait till application sends message client         // , check message sent correct 1         // if (expectedmessage == recievedmessagefromclient) {         //   success();         // }      }  class client     public void readmessage(message message) {         // received new event message id101         // message should sent applicationtest.checkifeventcreatedmsgissent , confirmed     } } 

i message client , every thing fine.

but i'm unable figure out how notify applicationtest message has been received client , verify. tried implementing observer design pattern here, made more confused. how should go in implementing , kind of design pattern best suited such scenarios ?

re-writing class few basic assumptions

we storing map of values map<string, object> msg = new hashmap<string, object>() loaded (eventid, eventdetailsobject). above map msg stored in servletcontext, alternatively persistent message on mq or value in db. client doesnt send acknowledgement applicationtest polls find if event created trying not details of other implementation details, let me know if makes sense you?

class applicationtest     public void checkifeventcreatedmsgissent() {         application.createnewevent("id101");         // wait till application sends message client         // , check message sent correct 1         // if (expectedmessage == recievedmessagefromclient) {         //   success();         // }         int icount = 0;         int final maxcount = 5;         // assuming client doesnt send acknowledgement              // server pools find         // below polling code method check if  event created         // servlet context example             // persistent message on mq or value in db         while(icount < 5) {              if (servletcontext.get(msg).get("id101") == null) {                  count++;                  sleep(5000);              } else { system.out.println("eureka, msg sent!")}         }     } class client{    public void readmessage(message message) {         // received new event message id101         // message should sent              // applicationtest.checkifeventcreatedmsgissent , confirmed         // below msg singleton object in servlet context example             // persistent message on mq or value in db         // map<string, object> msg = new hashmap<string, object>();         synchronized(servletcontext) {             servletcontext.get(msg).put("id101", <object>);         }     } } 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -