How to receive messages from Glassfish (v3) JMS queue

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

How to receive messages from Glassfish (v3) JMS queue

jiteshks
 0  down vote  favorite
share [g+] share [fb] share [tw]
       

I have searched for the solution of my problem as much as I could. My App runs on Glassfish v3.This app sends a message to Glassfish JMS queue and this message is supposed to be read by a standalone client on the same host but outside Glassfish JVM.

I have written standalone client java code - have included appserv-rt.jar and gf-client.jar from Glassfish installation directory.

This client code is unable to receive the message.System out statements print till "got consumer".After that nothing happens.

If I change the name of the queue-I get an error saying Queue not found.So it seems client code is able to find queue but it does not receive any messages.What do I need to include in my client code?

Here is my Java class :-

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;

import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.Queue;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.Category;

import domain.RedirectFile;

 public class ZblBulkUploadThread implements Runnable,MessageListener{

 private static final Category log = Category.getInstance(ZblBulkUploadThread.class) ;



    private Queue queue;

public void run()
{
    try
    {

        System.out.println(" inside try") ;
        InitialContext jndiContext = null;

        MessageConsumer messageConsumer=null;


        jndiContext = new InitialContext();

        System.out.println(" got context ") ;

        ConnectionFactory connectionFactory = (ConnectionFactory)jndiContext.
                lookup("jms/SimpleConnectionFactory");



        System.out.println("got connectionfactory") ;

    Connection connection = connectionFactory.createConnection();

    System.out.println("got connection") ;
    Session session = connection.createSession(false,
            Session.AUTO_ACKNOWLEDGE);

    queue = (Queue)jndiContext.lookup("jms/SimpleQueue") ;

    System.out.println("got queue"+queue.getQueueName()) ;

    messageConsumer = session.createConsumer(queue);

    System.out.println(" selector "+messageConsumer.getMessageSelector()) ;

    System.out.println("got consumer") ;

    Message message = messageConsumer.receive() ;

    System.out.println("Message is "+message) ;

    System.out.println("destination is "+message.getJMSDestination()) ;

    ObjectMessage om = ((ObjectMessage)message) ;
    try
    {

    RedirectFile file = (RedirectFile)om.getObject() ;

    log.debug("filePath "+file.getFilePath()) ;
    log.debug(" userName "+file.getUserName()) ;
    log.debug(" mode is "+file.getMode()) ;

    System.out.println("filePath "+file.getFilePath()) ;
    System.out.println(" userName "+file.getUserName()) ;
    System.out.println(" mode is "+file.getMode()) ;
    }
    catch(Exception ex)
    {
        log.error("ERROR "+ex.getMessage()) ;
        ex.printStackTrace() ;
    }

    log.debug("session created") ;

    }
    catch(Exception ex)
    {

        ex.printStackTrace() ;
        log.error("Error "+ex.getMessage()) ;
    }
}


public void onMessage(Message message)
{
    System.out.println("Message received "+message) ;
}

public static void main(String[] args)
{
    ZblBulkUploadThread zbut = new ZblBulkUploadThread() ;
    new Thread(zbut).start() ;
}

Thanks!