activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sachindra Nath <Sachindra.N...@efi.com>
Subject RE: Exception occurred: javax.jms.IllegalStateException: Cannot synchronously receive a message when a MessageListener is set
Date Thu, 24 Jul 2008 08:49:12 GMT
The code below is the problem in your Consumer class...
	            consumer.setMessageListener(this);
	            Message message1 = consumer.receive(1000);

With jms, you can receive messages synchronously (using the receive methods)
or asynchronously (using the MessageListener, where the onMessage method of
the listener will be called). You have setup both...

Some JMS implementations do allow this, but there is no reason why you would
like to do it that way. It might actually setup a race condition. Also, the
code in the onMessage method should be:

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

Note the removal of consumer.receive method here.


-----Original Message-----
From: skg [mailto:saurabh_garg05@infosys.com] 
Sent: Thursday, July 24, 2008 2:10 PM
To: users@activemq.apache.org
Subject: Exception occurred: javax.jms.IllegalStateException: Cannot
synchronously receive a message when a MessageListener is set


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%3
A-Cannot-synchronously-receive-a-message-when-a-MessageListener-is-set-tp1862
7640p18627640.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Mime
View raw message