0

This is regarding Websphere Application Serve, Java Message Service, Message Queue and Listener.

I have a scenario where my listener can have more sessions. So my MDB (message driven bean) can have multiple instances while running on WAS server thread.

My MDB will process/parse the received message and make some db transactions. So if my MDB receives more messages(suppose take 2 messages), MDB will create 2 instances since my listener sessions value has been set to 2 and try to process both messages at same time on the single thread. I have kept a thread.sleep(1000) to hold one message processing while other instance is processing. But both MDB instances are running on same thread, my entire process is on sleep for 1 second and again trying to do both processes parallelly and rollbacking both processes.

Any suggestions on this scenario.

JoshMc
  • 10,239
  • 2
  • 19
  • 38
johnny nb
  • 1
  • 1
  • 1
  • Multiple instances are designed and meant to be used for messages that can be processed in parallel. If for whatever reason your application does not allow parallel message processing, then do not use multiple sessions. The only safe way to make sure only one message is processed at a time is to serialize message processing which completely defeats the purpose of multiple sessions. – Yuri Steinschreiber Jul 25 '16 at 05:12
  • Thank you Yuri Steinschreiber for your response.. But here is my requirement. my system can process different user messages at the same time (i mean parallel). But if the messages are from the same user then i need to process one after one. – johnny nb Jul 30 '16 at 16:41

1 Answers1

0

Without going into much detail on why you don't want to process both messages together, you may randomize sleep interval with something like this.

(int) Math.floor(Math.random() * 10)

This is not a full proof method. It will just decrease probability of two messages being processed at the same time based on randomization range you select (This method will hugely reduce your throughput).

I am sure that there would be better method for resolving your issue, something like synchronized block. Could you please update question with reason why you don't want to process messages in parallel?

Raghu Sodha
  • 166
  • 1
  • 5
  • Synchronization of any type will make the message processing sequential, thus defeating the purpose of using multiple instances in the first place. So why bother? – Yuri Steinschreiber Jul 25 '16 at 05:14
  • I agree with you that synchronization will make processing sequential. But I would prefer synchronized block with multi threading over single thread configuration in some cases. For example, If there is heavy CPU intensive message processing followed by piece of code which cannot run in parallel like database lookup and insert, I would make lookup and insert synchronized while keep heavy processing multi threaded. – Raghu Sodha Jul 25 '16 at 17:53
  • I would go back to the requirements and design before using multithreading with synchronization, be that explicit synchronization or some clever use of sleep. The best synchronization is no synchronization at all, and the vast majority of applications out there do not need it besides what is provided by the database itself. Usually message processing restructuring is what one needs, not the synchronization of a poorly designed processing. – Yuri Steinschreiber Jul 25 '16 at 17:59
  • "The best synchronization is no synchronization at all" - I totally agree. – Raghu Sodha Jul 25 '16 at 18:04
  • Hi all, here is my requirement. my system can process different user messages at the same time (i mean parallel). But if the messages are from the same user then i need to process one after one.. – johnny nb Jul 30 '16 at 16:42