//Import the JMS API classes.
import javax.jms.ConnectionFactory;
import javax.jms.MessageListener;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.QueueSender;
import javax.jms.QueueReceiver;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Message;
import javax.jms.TextMessage;
//Import the classes to use JNDI.
import javax.naming.*;
import javax.swing.JOptionPane;

import com.sun.messaging.jms.JMSException;

import java.util.*;

public class JbossJmsListen {

    /**
     * Main method.
     *
     * @param args      not used
     *
     */
    public static int i = 0;
    static QueueSession myQSess;
    //public QueueSession myQSess=null;
    
    public static void main(String[] args) {
        
                ResourceBundle rb = ResourceBundle.getBundle("jndi");
                
        long time = 0;
        QueueConnection myQConn=null;
        myQSess=null;
        
        try {

                        InitialContext iniCtx = new InitialContext();
                        ConnectionFactory cf = (ConnectionFactory) 
iniCtx.lookup("/ConnectionFactory");
                        QueueConnectionFactory myQConnFactory = 
(QueueConnectionFactory) cf;
                        myQConn = myQConnFactory.createQueueConnection();

                        myQSess = 
myQConn.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);
                        //myQSess = myQConn.createQueueSession(true,-1);
                        //QueueSession myQSess = conn.createQueueSession(false, 
Session.AUTO_ACKNOWLEDGE);

                        Queue myQueue = (Queue) 
iniCtx.lookup("/queue/testQueue");

            time = System.currentTimeMillis();

            // IMPORTANT: Receive calls will be blocked if the connection is
            // not explicitly started, so make sure that we do so!
            //Step 9:
            //Start the QueueConnection created in step 3.
            myQConn.start();            

            //Step 8:
            //Create a QueueReceiver message consumer.
            QueueReceiver myQueueReceiver = myQSess.createReceiver(myQueue);
            
            // register a listener
            System.out.println("registering message listener for messages");
            MyListener listener1 = new MyListener(1);

            myQueueReceiver.setMessageListener(listener1);

            System.out.println("starting listener connection");

            //Thread.sleep(5000);       // give the receivers time to print 
messages
            JOptionPane.showMessageDialog(null, "Click to stop listening");

        } catch (JMSException je) {
            System.err.println("caught " + je);
            Exception e = je.getLinkedException();
            if (e != null) {
                System.err.println("subexception: " + e);
            }

        } catch (Exception e) {
            System.err.println("caught " + e);

        } finally {
            if (myQSess != null) {
                try {
                    System.out.println("closing session");
                    //myQSess.rollback();
                    myQSess.close();
                } catch (javax.jms.JMSException e) {
                        // TODO Auto-generated catch block
                    System.err.println("caught " + e);
                                        e.printStackTrace();
                                }
            }
            if (myQConn != null) {
                try {
                    System.out.println("closing connection");
                    myQConn.close();
                } catch (javax.jms.JMSException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                    System.err.println("connection.close() threw " + e);
                }
            }
        }
    }


/*
 * This class provides the message listener.
 */
    static class MyListener implements MessageListener {
        // an id to allow us to distinguish between different instances
        int id;

        MyListener(int id) {
            this.id = id;
        }


        /**
         * This method provides the implementation of the MessageListener
         * interface.
         */
        public void onMessage(Message message) {
            String body;

            try {
                // DEBUG used to remove binary messages
                //myQSess.commit();
                //System.out.println("in onMessage() of listener ");

                if (message instanceof TextMessage) {
                    // display the message contents
                    body = ((TextMessage) message).getText();
                    //myQSess.rollback();
                    //myQSess.commit();

                    if (body != null) {
                        System.out.println("message contained: '" + body + "'");
                        i++;
                    } else {
                        System.out.println("error: message contained no body");
                    }
                    // display the message 'colour' property
//      String colour = message.getStringProperty("colour");
//      if (colour == null) {
//        System.out.println("the message didn't have a colour property!");
//      } else {
//        System.out.println("colour="+colour);
//      }
                } else {
                    System.out.println("error: message was not a TextMessage as 
expected");

                    System.out.println(message);

                    if (message instanceof Message)
                    {
                        String msg_type = message.getJMSType();
                        System.out.println("JMS Message Type = " + msg_type ); 
                    }
                }

            } catch (JMSException je) {
                System.out.println("onMessage caught " + je);
                Exception e = je.getLinkedException();
                if (e != null) System.out.println("linked exception: " + e);

            } catch (Exception e) {
                System.out.println("onMessage caught " + e);
            }
        }

    }

}


View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3946166#3946166

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3946166


-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the Cost and Risk!
Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
_______________________________________________
JBoss-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to