FileDocCategorySizeDatePackage
JmsClient_TicketConsumer.javaAPI DocExample2174Sun Mar 17 22:53:24 GMT 2002com.titan.clients

JmsClient_TicketConsumer.java

package com.titan.clients;

import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueConnection;
import javax.jms.Session;
import javax.jms.QueueSession;
import javax.jms.Queue;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.JMSException;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;

import com.titan.travelagent.TicketDO;

public class JmsClient_TicketConsumer implements javax.jms.MessageListener{

    public static void main(String [] args) throws Exception {
        
        new JmsClient_TicketConsumer();
        
        while(true) { Thread.sleep(10000); }
        
    }
        
    public JmsClient_TicketConsumer() throws Exception {
            
        Context jndiContext = getInitialContext();
        
        QueueConnectionFactory factory = (QueueConnectionFactory)
			jndiContext.lookup("java:comp/env/jms/QueueFactory");
        
        Queue ticketQueue = (Queue)
			jndiContext.lookup("titan-TicketQueue");

        QueueConnection connect = factory.createQueueConnection();

        QueueSession session = 
			connect.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 
        
        QueueReceiver receiver = session.createReceiver(ticketQueue);

        receiver.setMessageListener(this);
        
		System.out.println("Listening for messages on titan-TicketQueue...");
        connect.start();
    }
    
    public void onMessage(Message message) {

        try {
        
			ObjectMessage objMsg = (ObjectMessage)message;
			TicketDO ticket = (TicketDO)objMsg.getObject();
			System.out.println("********************************");
			System.out.println(ticket);
			System.out.println("********************************");
        
        } catch (JMSException jmsE) {
            jmsE.printStackTrace();
        }
       System.out.println("finished consuming");
    }

    public static Context getInitialContext() 
                          throws javax.naming.NamingException {
		return new InitialContext();
    }
}