activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skg <saurabh_gar...@infosys.com>
Subject Exception occurred: javax.jms.IllegalStateException: Cannot synchronously receive a message when a MessageListener is set
Date Thu, 24 Jul 2008 08:39:43 GMT

Hi,
I worked on a simple java programm which acts as the producer and the
consumer as well.Now i a want to do it with two seperate java programm, one
acting as the consumer and the other one as producer.
While executing the consumer programm i am getting this exception.
Exception occurred: javax.jms.IllegalStateException: Cannot synchronously
receive a message when a MessageListener is set
 I am using Eclipse as development tool. I have attached the code for the
producer and consumer as well.

The producer
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SimpleProducer {
	private static final Log LOG = LogFactory.getLog(SimpleProducer.class);

    private SimpleProducer() {
    }

    /**
     * @param args the destination name to send to and optionally, the
number of
     *                messages to send
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws InterruptedException {
    	System.out.println("rrrrrrr");
        Context jndiContext = null;
        ConnectionFactory connectionFactory = null;
        Connection connection = null;
        Session session = null;
        Destination destination = null;
        MessageProducer producer = null;
        String destinationName = null;
        final int numMsgs;

       /* if ((args.length < 1) || (args.length > 2)) {
            LOG.info("Usage: java SimpleProducer <destination-name>
[<number-of-messages>]");
            System.exit(1);
        }*/
        destinationName = "MyQueue";
        LOG.info("Destination name is " + destinationName);
        if (args.length == 2) {
            numMsgs = (new Integer(args[1])).intValue();
        } else {
            numMsgs = 1;
        }

        /*
         * Create a JNDI API InitialContext object
         */
        try {
            jndiContext = new InitialContext();
        } catch (NamingException e) {
            LOG.info("Could not create JNDI API context: " + e.toString());
            System.exit(1);
        }

        /*
         * Look up connection factory and destination.
         */
        try {
            connectionFactory =
(ConnectionFactory)jndiContext.lookup("ConnectionFactory");
            destination = (Destination)jndiContext.lookup(destinationName);
        } catch (NamingException e) {
            LOG.info("JNDI API lookup failed: " + e);
            System.exit(1);
        }

        /*
         * Create connection. Create session from connection; false means
         * session is not transacted. Create sender and text message. Send
         * messages, varying text slightly. Send end-of-messages message.
         * Finally, close connection.
         */
        try {
            connection = connectionFactory.createConnection();
            session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
            producer = session.createProducer(destination);
            TextMessage message = session.createTextMessage();
            for (int i = 0; i < numMsgs; i++) {
                message.setText("This is message " + (i + 1));
                LOG.info("Sending message: " + message.getText());
                producer.send(message);
            }

            /*
             * Send a non-text control message indicating end of messages.
             */
            producer.send(session.createMessage());
            System.out.println("after the sender");
            Thread.sleep(10000);

            /*MessageConsumer consumer = null;
            consumer = session.createConsumer(destination);
            Message message1 = consumer.receive(1000);
            TextMessage txtMsg = (TextMessage)message;
            String msg = txtMsg.getText();
            System.out.println("Receiving"+msg);*/
        } catch (JMSException e) {
            LOG.info("Exception occurred: " + e);
        } finally {
            if (connection != null) {
                try {
                   connection.close();
                } catch (JMSException e) {
                }
            }
        }
    }

}

The Consumer 

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Consumer implements MessageListener{
	private static final Log LOG = LogFactory.getLog(Consumer.class);
	Context jndiContext = null;
    ConnectionFactory connectionFactory = null;
    Connection connection = null;
    Session session = null;
    Destination destination = null,destination1;
    MessageProducer producer = null;
    String destinationName = null;
   // final int numMsgs;
    MessageConsumer consumer = null;
	    public static void main(String[] args) {
	    	System.out.println("rrrrrrr");
	         Consumer c=new Consumer();
	        c.run();
	    }
	     public void run()
	     {
	      destinationName = "MyQueue";
	        try {
	            jndiContext = new InitialContext();
	        } catch (NamingException e) {
	            LOG.info("Could not create JNDI API context: " + e.toString());
	            System.exit(1);
	        }

	        /*
	         * Look up connection factory and destination.
	         */
	        try {
	            connectionFactory =
(ConnectionFactory)jndiContext.lookup("ConnectionFactory");
	            //destination =
(Destination)jndiContext.lookup(destinationName);
	        } catch (NamingException e) {
	            LOG.info("JNDI API lookup failed: " + e);
	            System.exit(1);
	        }
	        try {
	            connection = connectionFactory.createConnection();
	            session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
	            destination= session.createQueue("example.MyQueue");
	            
	            consumer = session.createConsumer(destination);
	            consumer.setMessageListener(this);
	            Message message1 = consumer.receive(1000);
	            TextMessage txtMsg = (TextMessage)message1;
	            String msg = txtMsg.getText();
	            System.out.println("Receiving"+msg);
	        } catch (JMSException e) {
	            LOG.info("Exception occurred: " + e);
	        } finally {
	            if (connection != null) {
	                try {
	                    connection.close();
	                } catch (JMSException e) {
	                }
	            }
	        }
	    }

		public void onMessage(Message message1) {
			// TODO Auto-generated method stub
			try
			{
		    message1 = consumer.receive(1000);
            TextMessage txtMsg = (TextMessage)message1;
            String msg = txtMsg.getText();
            System.out.println("Receiving"+msg);
			}catch (JMSException e) {
            }
		}

	}


Can u please tell me why i am getting this error.If i embedd both producer
and consumer it works file but in different files i am getting problems.
-- 
View this message in context: http://www.nabble.com/Exception-occurred%3A-javax.jms.IllegalStateException%3A-Cannot-synchronously-receive-a-message-when-a-MessageListener-is-set-tp18627640p18627640.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Mime
View raw message