Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Unable to consume messages or only one message is getting retrieved while connecting to IBM MQ async

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had written a java code to consume messages from IBM MQ.. but while executing the code some times only one message is getting consumed and the process is stopping .. some times no messages are not getting consumed at all.. i had written an asynchronous connection using setMessageListener() but i am not sure where the program is getting wrong, the main method is getting terminated automatically. The below code



But writing  System.in.read() it is consuming messages continuously until an input from system.. but tis is not the right process to deploy the code in production.. please help me in this
 
Saloon Keeper
Posts: 25843
184
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't answer your main question, but in Enterprise Java, the System.in/out/err streams are almost never the right answer. In webapps, for example, those streams technically don't exist and there's no telling where the webapp server will send them.

For System out/err, it is better to use logging. System.in usually isn't useful at all.
 
Venkat S Prasad
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had used Thread.Sleep() as well to check if the system is working , it is working but only for the time mentioned in the sleep method only.. but he main method needs to run continuously without any halt
 
Tim Holloway
Saloon Keeper
Posts: 25843
184
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Venkat S Prasad wrote:I had used Thread.Sleep() as well to check if the system is working , it is working but only for the time mentioned in the sleep method only.. but he main method needs to run continuously without any halt

That's another questionable item. Directly manipulating threads in JEE apps is often a problem. In fact, returning to the JEE webapp environment the JEE spec absolutely forbids spawing threads or doing other thread operations on servlet service methods or Enterprise JavaBeans.
 
Venkat S Prasad
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Venkat S Prasad wrote:I had used Thread.Sleep() as well to check if the system is working , it is working but only for the time mentioned in the sleep method only.. but he main method needs to run continuously without any halt

That's another questionable item. Directly manipulating threads in JEE apps is often a problem. In fact, returning to the JEE webapp environment the JEE spec absolutely forbids spawing threads or doing other thread operations on servlet service methods or Enterprise JavaBeans.


One thing what i want to mention is this is not a JEE app.. it is a simple java application where i am trying to consuming the messages
 
Tim Holloway
Saloon Keeper
Posts: 25843
184
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, JMS is part of JEE, so any app that employs JMS is a JEE app. However as a MQ client, some of what you're doing would be allowable (if not always recommended) where it wouldn't be for server-side code.

A simple MQ client shouldn't be playing around with threading, though. All it needs to do is listen for incoming messages. My memory is a bit fuzzy on how received messages get handled - whether asynchronously, synchonously, or optional selection, but in any case any threads spawned by the MQ client methods should not be interfered with by you. Just do the work and return.

Messages to System.out or logs are OK in a multi-threaded system no matter what's doing the thread control. However, the order in which messages are output may vary.
 
Saloon Keeper
Posts: 615
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems like he is making a console app and wants to print the messages.  He wants to know how to make it not exit without using System.in.read().  

How about a while loop?
 
Marshal
Posts: 27288
87
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Venkat S Prasad wrote:but he main method needs to run continuously without any halt



No, the main method should just start the connection. Presumably that starts a thread which listens for messages and calls the onMessage() method for each message it receives. I don't see how calling Thread.sleep should affect anything. Perhaps you could explain a bit more about how you know that the whole application (not just the main method) is stopping.
 
Paul Clapham
Marshal
Posts: 27288
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also... to know that the application is terminating, you can add a shutdown hook which can tell you that. Here's a tutorial which shows you how to do that.
 
Venkat S Prasad
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Also... to know that the application is terminating, you can add a shutdown hook which can tell you that. Here's a tutorial which shows you how to do that.


Thanks for your reply.. by seeing my code could you please guess  why my init() method is returning back to main method without calling onMessage() when there are lot of messages that need to be processed.. it should not happen in this case ight.. as it is a asynchronous process the onMessag needs to be calledtill the last message gets consumed and then return to main method
 
Venkat S Prasad
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Venkat S Prasad wrote:but he main method needs to run continuously without any halt



No, the main method should just start the connection. Presumably that starts a thread which listens for messages and calls the onMessage() method for each message it receives. I don't see how calling Thread.sleep should affect anything. Perhaps you could explain a bit more about how you know that the whole application (not just the main method) is stopping.


Thanks for your reply.. by seeing my code could you please guess  why my init() method is returning back to main method without calling onMessage() when there are lot of messages that need to be consumed.. it should not happen in this case ight.. as it is a asynchronous process the onMessag needs to be calledtill the last message gets consumed and then return to main method.

As per your request in any message consumer class when ever any message listener starts it needs to start consuming or producing any messages immediately but it is not happening in this case , it is just going back to the previous call where the init method has been called.. but if i keep Thread.sleep it is consuming the messages in that time where the main method has been waiting stae and then quitting.. i want to know  why after starting connection it is taking time to connect to onMessage method
 
Al Hobbs
Saloon Keeper
Posts: 615
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this



onMessage is called asynchronously whenever there is a message.
After you call init() the program ends, so no messages are consumed or maybe 1 if it arrives before it exits.
 
Paul Clapham
Marshal
Posts: 27288
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Venkat S Prasad wrote:Thanks for your reply.. by seeing my code could you please guess  why my init() method is returning back to main method without calling onMessage() when there are lot of messages that need to be consumed.. it should not happen in this case ight.. as it is a asynchronous process the onMessag needs to be calledtill the last message gets consumed and then return to main method.

As per your request in any message consumer class when ever any message listener starts it needs to start consuming or producing any messages immediately but it is not happening in this case , it is just going back to the previous call where the init method has been called.. but if i keep Thread.sleep it is consuming the messages in that time where the main method has been waiting stae and then quitting.. i want to know  why after starting connection it is taking time to connect to onMessage method



First of all your init() method does not call onMessage(). It creates a thread which connects to the MQ server to ask it for messages which may be waiting. As for why it takes time to connect, the thread must connect to the MQ server over the network, perhaps even to a server on a different machine. Such things do not happen instantaneously.

What was the result when you added a shutdown hook to your code? Did it run almost immediately? Or did it not run at all?
 
Venkat S Prasad
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Al Hobbs wrote:try this



onMessage is called asynchronously whenever there is a message.
After you call init() the program ends, so no messages are consumed or maybe 1 if it arrives before it exits.



Thanks .. it Worked but the messages that are consumed are not in an order .. example if three messages gets consumed .. in that two messages prints the message in logs and then they are hitting database .. it is creating a problem
 
Paul Clapham
Marshal
Posts: 27288
87
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Venkat S Prasad wrote:Thanks .. it Worked but the messages that are consumed are not in an order .. example if three messages gets consumed .. in that two messages prints the message in logs and then they are hitting database .. it is creating a problem



It would be a good idea to describe that problem in a new post, then. It seems like a different problem than the one covered in this thread.
 
Al Hobbs
Saloon Keeper
Posts: 615
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There may be a setting for that, or you would have to manually process them in order somehow
 
reply
    Bookmark Topic Watch Topic
  • New Topic