• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Producer/Consumer Design impacts exception handling

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Harvinder are you trying to make something like this.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the diagram caputres the flow exactly.
 
We don't have time to be charming! Quick, read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic