File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Producer/Consumer Design impacts exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Producer/Consumer Design impacts exception handling" Watch "Producer/Consumer Design impacts exception handling" New topic
Author

Producer/Consumer Design impacts exception handling

Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
I have following classes for solving the producer consumer scenario.



Now i am passing the same socket client object to both my Producer and Consumer Threads. They further pass it to the SockClientProducer and SockClientConsumer Threads which use the output stream and the input stream respectively.
Now if socket exception occurs then i am supposed to reconnect and ensure my application does not go down.
My problem is that in this design the same socket connection is being used in two different threads. if a socket exception occurs then both the threads will throw the exception. Now where should i recover from the socket exception. in Producer thread or Consumer thread or the code calling the Producer and Consumer threads. Do i need to change my design.

Please any ideas are welcome!


thanks
Harvinder
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Do not you need ServerSocket in your application design, that will listen on a port to connection with the producer, than producer and consumer start interaction.

I tried something, I do not know your exact requirement, so sorry if I mislead you.



SCJP 6
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
@Punit: First of all thanks for responding. You may assume that some Socket Server is listening on some port. I create a client socket connection object to that Server and pass that object to both Producer and Consumer classes. The Producer and Consumer classes then pass the same socket object to the SockClientConsumer and SockClientProducer classes respectively. The SockClientConsumer class uses the socket output stream to write the messages to the Server and the SockClientProducer class uses the socket input stream to read the messages from the Server. The issue is that both classes run in two different threads viz. Producer and Consumer respectively and use the same socket object. And i am starting the Producer and Consumer threads from the main method of another class say MessageManager. Now if a socket exception occurs due to loss of connection then that would be thrown in both the running threads.
Since my application needs to recover from that exception and reconnect i have a problem if i try to reconnect from both the Producer and Cosumer Threads as i would be trying to connect to the same server socket twice whereas i need to do it once.

Ideally i guess it should be another wrapper class say SocketManager which should be called from main() method and which further starts the Producer and Consumer threads. Now the SocketManager runs in an infinite while loop and keeps on checking the health of the socket connection. if socket connection goes down then it reconnects and makes the new socket object available to the Producer and Consumer thread objects.

But is this design change viable from future enhancement perspective. e.g. what if my Consumer consumes messages from a Database. or from a file. What if my Producer needs to produce messages and persist to a file or database.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Have you made something, I tried what you said, but unfortunately Producer is just sending messages to server and server is just behaving like consumer. Server is creating a new thread of consumer, consumer is using this server's socket to read whatever Producer it writing on server and displaying. I am not able to understand how both will connect to same socket explicitly. I have only connected Producer to the server, and server is just instantiating consumer so consumer can consume from server whatever given by Producer to the server. I have given my code what I tried, If you have something running as you said, I will try to help you on that.

Server code:


MessageManager code:



Producer code:


SockClientProducer code:



Consumer code:


SockClientConsumer code:


Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
I think i am responsible for this confusion. Well let me explain in more detail from scratch.
I have a Producer Thread which reads messages from the database and puts it into a request Queue. The Consumer Thread that i have talked of in my previous posts is actually consuming the messages from the Request Queue and putting sending it to the Socket Server.
Similarly, the Server generates certain responses for every request received and the Producer Thread that i have talked of in the previous post takes the response and puts it into the Response Queue. Another Consumer Thread takes the messages from the Response Queue and writes them to the Database as well as a file for backup.

So i have two sets of Producers and Consumers using there separate Request and Response Queues.
I hope i have not confused you further but this is the actual scenario.
I just picked up the socket communication part as that was creating problems from reconnection point of view as the atomicity of reconnection was not being achieved by my current design of the classes. By atomicity i mean that if an socket exception occurs in both the producer and consumer threads then the reconnect attempt should not be tried at two locations as the socket connection is only single.
My problem in addition to clean socket exception recovery, is also how to design the classes to achieve maximum flexibility from future scalability perspective.
Hope it helps you to help me.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


Harvinder are you trying to make something like this.
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
Yes, the diagram caputres the flow exactly.
 
Don't get me started about those stupid light bulbs.
 
subject: Producer/Consumer Design impacts exception handling