Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7054079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:34:58+00:00 2026-05-28T03:34:58+00:00

I’ve Oracle Advanced Queue implemented & I’m writing a listener program. Below is my

  • 0

I’ve Oracle Advanced Queue implemented & I’m writing a listener program. Below is my sample:

package com.myprog;

import java.io.File;
import java.io.FileInputStream;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;

import oracle.jms.AQjmsFactory;
import oracle.jms.AQjmsSession;

import org.apache.log4j.Logger;

public class abc implements MessageListener, ExceptionListener {
private static String queueUserName = "admin";
private static String queueName = "my_queue";

// Initialize the logger
private static Logger log = Logger.getLogger(abc.class);

public static void main(String[] args) {
    final String METHOD_NAME  = "main()";

    abc a = new abc();              

      Queue queue;
      try {
       QueueConnection QCon = getConnection();  
       Session session = QCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
       QCon.start();

       queue = ((AQjmsSession) session).getQueue(queueUserName, queueName);
       MessageConsumer consumer = session.createConsumer(queue);       

       consumer.setMessageListener(a);
       QCon.setExceptionListener(a);

       consumer.close();
       session.close();
       QCon.close();
      } catch (JMSException e) {  
       e.printStackTrace();
      }         

}

public static QueueConnection getConnection() {
  String hostname = "myhost";
  String oracle_sid = "mysid";
  int portno = 1521;
  String userName = "myapp";
  String password = "pwd";
  String driver = "thin";
  QueueConnectionFactory QFac = null;
  QueueConnection QCon = null;
  try {
   // get connection factory , not going through JNDI here
   QFac = AQjmsFactory.getQueueConnectionFactory(hostname, oracle_sid, portno,driver);

   // create connection
   QCon = QFac.createQueueConnection(userName, password);
   } catch (Exception e) {
    e.printStackTrace();
  }
  return QCon;
}

@Override
public void onException(JMSException e) {
    log.error(e);       
}

@Override
public void onMessage(Message message) {
     TextMessage msg = (TextMessage) message;

     try {
         String m = msg.getText();
         System.out.println("m="+m);
         log.info("MESSAGE RECEIVED " + m);
     } catch (JMSException e) {
        log.error(e); 
     }
}

}

Please note that this program is a standalone program which will keep running & listening to messages in oracle queue.

Unfortunately, when I create a jar of this class file & run it, it just runs & then exits & consumes only 1 message in the queue. Why the listener not keep on running & listening to queue?

I thought it’ll keep listening & retrieve all the messages in the queue & then will remain in listen mode forever, but its not behaving that way.

Appreciate if some one can tell me what’s going wrong.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:34:59+00:00Added an answer on May 28, 2026 at 3:34 am

    Here’s an example of how another JMS example loops to process multiple messages.

    Performs a JNDI lookup of the ConnectionFactory and Destination.
    Creates a Connection and a Session.
    Creates a MessageConsumer:
    
    consumer = session.createConsumer(dest);
    Starts the connection, causing message delivery to begin:
    
    connection.start();
    Receives the messages sent to the destination until the end-of-message-stream control message is received:
    
    while (true) {
      Message m = consumer.receive(1);
      if (m != null) {
        if (m instanceof TextMessage) {
          message = (TextMessage) m;
          System.out.println("Reading message: " +
            message.getText());
        } else {
          break;
        }
      }
    }
    
    Because the control message is not a TextMessage, the receiving program terminates the while loop and stops receiving messages after the control message arrives.
    Closes the connection in a finally block, automatically closing the session and MessageConsumer.
    

    You may be able to get away with just wrapping this code in the while loop. It depends on how JMS makes you handle the connection and session objects and if they automatically close, but you can try to wrap just this.

    while(true) {
           QCon.start();
    
           queue = ((AQjmsSession) session).getQueue(queueUserName, queueName);
           MessageConsumer consumer = session.createConsumer(queue);       
    
           consumer.setMessageListener(a);
           QCon.setExceptionListener(a);
    
           consumer.close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am writing an app with both english and french support. The app requests
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.