*
The moose likes Beginning Java and the fly likes problem with implementation of MVC with several viewers and single model + controller? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with implementation of MVC with several viewers and single model + controller?" Watch "problem with implementation of MVC with several viewers and single model + controller?" New topic
Author

problem with implementation of MVC with several viewers and single model + controller?

Tushar Goel
Ranch Hand

Joined: Dec 29, 2013
Posts: 220
Hi All,

I am working on an project in which some data needs to be received through socket , do some processing on it and then store on the database. After that it will display on GUI. GUI should be updated automatically.

I am able to implement basic model in which data is storing in the database and displayed then on the GUI. I tried to implement MVC design patter as well.

Now i need to create a GUI which can be used by several users at a time means several jvm session for the GUI. I got struck at this point.

Some detail: i have one jvm session in which i am receiving , processing and storing the data. Now i want to fire an alert whenever any new entry is made into the database. Also each time new GUI is opened it should be registered with the model so that model send
updated data alert to all this users.

When i am trying to open several GUI and trying to register with the Model then it is not increasing count of registered user. For each time it is showing 1 count only and whenever a new entry is made count is showing 0. Is it because both are running in different jvm sessions ?

Some code for the model which is running in different package and jvm

GUI..

code for GUI..




Model code



K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

Hmm sound like multi-threading issue in my SCJD assignment.

Anyway first your GUI code or client should get the "same" instance of the model. Since your data is in the database, the model code should theoretically get the latest data from the database when it instantiates. This also means you should look into connection pooling.

I see you are trying to make your Model class into a singleton with the private constructor thing, which can be one approach. But base on my point earlier, I doubt the Model class needs to a singleton.

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Tushar Goel
Ranch Hand

Joined: Dec 29, 2013
Posts: 220
Thanks Tsang, I have connection pooling in my mind but i will do that later when my base model is working fine.

I am using same instance of the model so all the GUI are using same instances. It still showing same count.. Also like every GUI instance create new JVM session as they have separate main method so is it multi threading issue? I have spent several days into it but not able to figure out...

My GUI code is as below:



Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2165
    
  47
Tushar Goel wrote:I am using same instance of the model

No you're not. A singleton is only a single instance within a JVM.
As each new GUI starts within a new JVM, calling Model.getModelInstance() gets the Model singleton instance for that JVM and not for your whole system.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

Looking at the code, it is indeed NOT a true singleton, but close.

Tushar Goel
Ranch Hand

Joined: Dec 29, 2013
Posts: 220
As each new GUI starts within a new JVM, calling Model.getModelInstance() gets the Model singleton instance for that JVM and not for your whole system.


Thanks Tony but then what to do? How to avoid this problem?


Looking at the code, it is indeed NOT a true singleton, but close.


Thanks Tsang, i will make correction..


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

Tushar Goel wrote:Thanks Tony but then what to do? How to avoid this problem?

I'm no JEE expert, but this strikes me as a problem that must exist for most web-based apps, since they're likely to have ONE database that services lots of screens (or web pages). Not so sure about one controller though.

The only thing I would suggest (and it's purely my personal bias) is that change notification should be active (ie, "I have changed") rather than passive ("have you changed?"), and I suspect that either approach could be achieved with something like JMS - although there may well be better, or more targeted, alternatives these days.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Tushar Goel
Ranch Hand

Joined: Dec 29, 2013
Posts: 220
@Winston Thanks for the suggestion but i dont have any idea how to move forward? Will RMI help in this case i read it is used to run remote methods running on another JVM sessions.


@Tsang
Looking at the code, it is indeed NOT a true singleton, but close.


Could you please explain me the difference between close and true and any drawback of using close one?

Tushar Goel
Ranch Hand

Joined: Dec 29, 2013
Posts: 220
I also thought to implement socket between model and gui. model sends serialized object on particular port and which ever client attached to it will listen it. data flow is like as below


[Thumbnail for Selection_212.png]

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

I think you need to step back and rethink your design.

Is your application a server/client or is that "model" of yours just a POJO?

From my understanding so far, I reckon your model basically represents the data so having one instance (singleton) for the whole app is desirable.

Under such scenario, your model is some sort of "data server" interacting with the database.

Questions worth asking yourself:
What will the "model" do? What functions/methods will it have?
How will clients (GUI) connect to the model? socket, RMI
How often will data get saved/updated? eg when client click save button, upon editing a row/cell

So much questions can be asked about the server side (model) or GUI side. But I'll let you figure out the model part first. Just don't overdo the design.
Tushar Goel
Ranch Hand

Joined: Dec 29, 2013
Posts: 220

Pardon me if i do not make any sense..I am very much confused and struck here..

Is your application a server/client or is that "model" of yours just a POJO?


Actually it is both.. I am receiving data as a server/client covert data as POJO and using model class add that into the database. Now once data is stored in to the database then model send notifications to the all GUI client but as GUI
clients are running onto different JVM so to interact between my model and GUI i thought 2 ways either RMI or using socket. There may be other way like Winston suggested earlier but others am not able to figure it out..


Under such scenario, your model is some sort of "data server" interacting with the database.


Yes, model should receive data first to load into the database and once it loads successful then it will inform others about the changes. Also if the user click on the particular row in the JTable (on right hand click acknowledge
option should come in pop up menu) then that entry should be removed from the database which again then handle through the model.

How will clients (GUI) connect to the model? socket, RMI


I dont have much idea of RMI but know about socket. So i thought to interact between GUI and model i should use socket.


What will the "model" do? What functions/methods will it have?


I think i explained earlier. There are few more methods needs to be added to interact between GUI and model if i implemented using socket. Will think about it and let you know shortly.

How often will data get saved/updated? eg when client click save button, upon editing a row/cell


model will send notification to the GUI almost every second but the request from the GUI towards client will be not very often.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

I see where this is going.

You need 2 classes: 1 for the interacting with data (eg your Model class) and another one is the actual server (socket or RMI).

The server will actually be the class that instantiates the Model class. Clients (GUI) will ultimately interact with just the server. The server then "delegate" the functions (get/insert/update etc) to the data/model class.

You are building an app that is indeed similar if not identical to the SCJD assignment (GUI, data server, socket/RMI server).
Tushar Goel
Ranch Hand

Joined: Dec 29, 2013
Posts: 220
Thanks Tsang, it making sense to me now...

The server will actually be the class that instantiates the Model class.


Dont you think i should use single reference of the model as i made model class constructor private or should allow multiple?


The server then "delegate" the functions (get/insert/update etc) to the data/model class.


Do you think i should maintain some type of queue like blocking queue or its varaint to manage these requests? As it is possible that data is loading to the database very frequent as compare to the it informs the GUI client.
By doing this i thought a queue is managed such that no information is lost. typically it is like producer/consume problem

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

Tushar Goel wrote:Dont you think i should use single reference of the model as i made model class constructor private or should allow multiple?

I have a feeling that you're concentrating a bit too much on the "how" here. The "model" part of MVC doesn't necessarily mean a specific "model class"; it simply refers to a single port of call to update structural data, and it might involve 50 classes if that's what it needs (although chances are that they may well all be in the same model.jar).

If you try to cram it all into a single class, you may well end up with the same sort of monolithic monster that OO was designed to prevent.

HIH

Winston
Tushar Goel
Ranch Hand

Joined: Dec 29, 2013
Posts: 220
Thanks Winston.. Yes, this evening when i was thinking about my project i got a feeling that i am concentrating much on the "How" part.

I think i should focus on more on working model and its behavior and after that i should think about how to improve but as i am new to these so by default my concentration always going on "How".
Tushar Goel
Ranch Hand

Joined: Dec 29, 2013
Posts: 220
I tried to implement the same what Tsang is suggested and added few more thing Now instead of any queue what i did i create an interface which having single method which needs to be implemented by the server class which interact with the client directlt. model directly call this method and this method has implementation of forwarding data to all the connected client. It works now perfectly (i think so). Now i going to display this into the GUI.

some code is :

Registration :




model class:




class which implements this method:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: problem with implementation of MVC with several viewers and single model + controller?
 
Similar Threads
Again about MVC
Servlet chaining
Applet focus problem, I think
exception while creating JTables
How to improve performance?