Note that there are some explanatory texts on larger screens.

plurals
  1. POHow JMS work in Java?
    text
    copied!<p>How does async JMS work? I've below sample code:</p> <pre><code>public class JmsAdapter implements MessageListener, ExceptionListener { private ConnectionFactory connFactory = null; private Connection conn = null; private Session session = null; public void receiveMessages() { try { this.session = this.conn.createSession(true, Session.SESSION_TRANSACTED); this.conn.setExceptionListener(this); Destination destination = this.session.createQueue("SOME_QUEUE_NAME"); this.consumer = this.session.createConsumer(destination); this.consumer.setMessageListener(this); this.conn.start(); } catch (JMSException e) { //Handle JMS Exceptions Here } } @Override public void onMessage(Message message) { try { //Do Message Processing Here //Message sucessfully processed... Go ahead and commit the transaction. this.session.commit(); } catch(SomeApplicationException e) { //Message processing failed. //Do whatever you need to do here for the exception. //NOTE: You may need to check the redelivery count of this message first //and just commit it after it fails a predefined number of times (Make sure you //store it somewhere if you don't want to lose it). This way you're process isn't //handling the same failed message over and over again. this.session.rollback() } } </code></pre> <p>}</p> <p>But I'm new to Java &amp; JMS. I'll probably consume messages in onMessage method. But I don't know how does it work exactly. </p> <p>Do I need to add main method in JmsAdapter class? After adding main method, do I need to create a jar &amp; then run the jar as "java -jar abc.jar"?</p> <p>Any help is much appreciated.</p> <p>UPDATE: What I want to know is that if I add main method, should I simply call receiveMessages() in main? And then after running, will the listener keep on running? And if there are messages, will it retrieve automatically in onMessage method?</p> <p>Also, if the listener is continuously listening, doesn't it take CPU??? In case of threads, when we create a thread &amp; put it in sleep, the CPU utilization is zero, how doe it work in case of listener?</p> <p>Note: I've only Tomcat server &amp; I'll not be using any jms server. I'm not sure if listener needs any specific jms server such as JBoss? But in any case, please assume that I'll not be having anything except tomcat. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload