You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by skg <sa...@infosys.com> on 2008/07/24 10:39:43 UTC

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


RE: Exception occurred: javax.jms.IllegalStateException: Cannot synchronously receive a message when a MessageListener is set

Posted by Sachindra Nath <Sa...@efi.com>.
You will need to start the connection for receiving messages. After setting
up your receiver, call connection.start();

consumer = session.createConsumer(destination);
connection.start();
Message message1 = consumer.receive(1000);

Read the jms api/a good book.

I believe activemq is supposed to be logging warning messages for these
cases, which I donot see in your logs. Which version are you working with?

Apart from that, I see a lot of errors in your error logs....that are related
to port in use issues (for port 1099). Correct them...

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


Hi,
i made the changes u have mentioned but i am not receiving any message. I am
putting the output of the console for the producer and the consumer as well.

Producer console window
rrrrrrr
Jul 24, 2008 2:27:38 PM SimpleProducer main
INFO: Destination name is MyQueue
Jul 24, 2008 2:27:39 PM org.apache.activemq.broker.BrokerService start
INFO: Using Persistence Adapter:
AMQPersistenceAdapter(activemq-data\localhost)
Jul 24, 2008 2:27:39 PM org.apache.activemq.broker.BrokerService getBroker
INFO: ActiveMQ null JMS Message Broker (localhost) is starting
Jul 24, 2008 2:27:39 PM org.apache.activemq.broker.BrokerService getBroker
INFO: For help or more information please see: http://activemq.apache.org/
Jul 24, 2008 2:27:39 PM org.apache.activemq.broker.jmx.ManagementContext$1
run
WARNING: Failed to start jmx connector: Cannot bind to URL
[rmi://localhost:1099/jmxrmi]: javax.naming.CommunicationException [Root
exception is java.rmi.ConnectIOException: error during JRMP connection
establishment; nested exception is: 
	java.net.SocketException: Software caused connection abort: recv
failed]
Jul 24, 2008 2:27:39 PM org.apache.activemq.store.amq.AMQPersistenceAdapter
start
INFO: AMQStore starting using directory: activemq-data\localhost
Jul 24, 2008 2:27:39 PM org.apache.activemq.kaha.impl.KahaStore initialize
INFO: Kaha Store using data directory activemq-data\localhost\kr-store\state
Jul 24, 2008 2:27:39 PM org.apache.activemq.store.amq.AMQPersistenceAdapter
start
INFO: Active data files: [1]
Jul 24, 2008 2:27:39 PM org.apache.activemq.kaha.impl.KahaStore initialize
INFO: Kaha Store using data directory activemq-data\localhost\kr-store\data
Jul 24, 2008 2:27:39 PM org.apache.activemq.broker.BrokerService start
INFO: ActiveMQ JMS Message Broker (localhost,
ID:blrkec41498d-3757-1216889859509-0:0) started
Jul 24, 2008 2:27:39 PM org.apache.activemq.broker.TransportConnector start
INFO: Connector vm://localhost Started
Jul 24, 2008 2:27:40 PM SimpleProducer main
INFO: Sending message: This is message 1
after the senderJul 24, 2008 2:27:40 PM
org.apache.activemq.broker.TransportConnector stop
INFO: Connector vm://localhost Stopped
Jul 24, 2008 2:27:40 PM org.apache.activemq.broker.BrokerService stop
INFO: ActiveMQ Message Broker (localhost,
ID:blrkec41498d-3757-1216889859509-0:0) is shutting down
Jul 24, 2008 2:27:40 PM org.apache.activemq.broker.BrokerService stop
INFO: ActiveMQ JMS Message Broker (localhost,
ID:blrkec41498d-3757-1216889859509-0:0) stopped

The consumer console window

Jul 24, 2008 2:30:23 PM org.apache.activemq.broker.BrokerService start
INFO: Using Persistence Adapter:
AMQPersistenceAdapter(activemq-data\localhost)
Jul 24, 2008 2:30:23 PM org.apache.activemq.broker.BrokerService getBroker
INFO: ActiveMQ null JMS Message Broker (localhost) is starting
Jul 24, 2008 2:30:23 PM org.apache.activemq.broker.BrokerService getBroker
INFO: For help or more information please see: http://activemq.apache.org/
Jul 24, 2008 2:30:23 PM org.apache.activemq.store.amq.AMQPersistenceAdapter
start
INFO: AMQStore starting using directory: activemq-data\localhost
Jul 24, 2008 2:30:23 PM org.apache.activemq.broker.jmx.ManagementContext$1
run
WARNING: Failed to start jmx connector: Cannot bind to URL
[rmi://localhost:1099/jmxrmi]: javax.naming.CommunicationException [Root
exception is java.rmi.ConnectIOException: error during JRMP connection
establishment; nested exception is: 
	java.net.SocketException: Software caused connection abort: recv
failed]
Jul 24, 2008 2:30:23 PM org.apache.activemq.kaha.impl.KahaStore initialize
INFO: Kaha Store using data directory activemq-data\localhost\kr-store\state
Jul 24, 2008 2:30:23 PM org.apache.activemq.store.amq.AMQPersistenceAdapter
start
INFO: Active data files: [1]
Jul 24, 2008 2:30:23 PM org.apache.activemq.kaha.impl.KahaStore initialize
INFO: Kaha Store using data directory activemq-data\localhost\kr-store\data
Jul 24, 2008 2:30:23 PM org.apache.activemq.broker.BrokerService start
INFO: ActiveMQ JMS Message Broker (localhost,
ID:blrkec41498d-3764-1216890023650-0:0) started
Jul 24, 2008 2:30:24 PM org.apache.activemq.broker.TransportConnector start
INFO: Connector vm://localhost Started
Jul 24, 2008 2:30:24 PM org.apache.activemq.broker.TransportConnector stop
INFO: Connector vm://localhost Stopped
Jul 24, 2008 2:30:24 PM org.apache.activemq.broker.BrokerService stop
INFO: ActiveMQ Message Broker (localhost,
ID:blrkec41498d-3764-1216890023650-0:0) is shutting down
Jul 24, 2008 2:30:24 PM org.apache.activemq.broker.BrokerService stop
INFO: ActiveMQ JMS Message Broker (localhost,
ID:blrkec41498d-3764-1216890023650-0:0) stopped

Can u help me .

skg wrote:
> 
> 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
7640p18627931.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Exception occurred: javax.jms.IllegalStateException: Cannot synchronously receive a message when a MessageListener is set

Posted by skg <sa...@infosys.com>.
Hi,
i made the changes u have mentioned but i am not receiving any message. I am
putting the output of the console for the producer and the consumer as well.

Producer console window
rrrrrrr
Jul 24, 2008 2:27:38 PM SimpleProducer main
INFO: Destination name is MyQueue
Jul 24, 2008 2:27:39 PM org.apache.activemq.broker.BrokerService start
INFO: Using Persistence Adapter:
AMQPersistenceAdapter(activemq-data\localhost)
Jul 24, 2008 2:27:39 PM org.apache.activemq.broker.BrokerService getBroker
INFO: ActiveMQ null JMS Message Broker (localhost) is starting
Jul 24, 2008 2:27:39 PM org.apache.activemq.broker.BrokerService getBroker
INFO: For help or more information please see: http://activemq.apache.org/
Jul 24, 2008 2:27:39 PM org.apache.activemq.broker.jmx.ManagementContext$1
run
WARNING: Failed to start jmx connector: Cannot bind to URL
[rmi://localhost:1099/jmxrmi]: javax.naming.CommunicationException [Root
exception is java.rmi.ConnectIOException: error during JRMP connection
establishment; nested exception is: 
	java.net.SocketException: Software caused connection abort: recv failed]
Jul 24, 2008 2:27:39 PM org.apache.activemq.store.amq.AMQPersistenceAdapter
start
INFO: AMQStore starting using directory: activemq-data\localhost
Jul 24, 2008 2:27:39 PM org.apache.activemq.kaha.impl.KahaStore initialize
INFO: Kaha Store using data directory activemq-data\localhost\kr-store\state
Jul 24, 2008 2:27:39 PM org.apache.activemq.store.amq.AMQPersistenceAdapter
start
INFO: Active data files: [1]
Jul 24, 2008 2:27:39 PM org.apache.activemq.kaha.impl.KahaStore initialize
INFO: Kaha Store using data directory activemq-data\localhost\kr-store\data
Jul 24, 2008 2:27:39 PM org.apache.activemq.broker.BrokerService start
INFO: ActiveMQ JMS Message Broker (localhost,
ID:blrkec41498d-3757-1216889859509-0:0) started
Jul 24, 2008 2:27:39 PM org.apache.activemq.broker.TransportConnector start
INFO: Connector vm://localhost Started
Jul 24, 2008 2:27:40 PM SimpleProducer main
INFO: Sending message: This is message 1
after the senderJul 24, 2008 2:27:40 PM
org.apache.activemq.broker.TransportConnector stop
INFO: Connector vm://localhost Stopped
Jul 24, 2008 2:27:40 PM org.apache.activemq.broker.BrokerService stop
INFO: ActiveMQ Message Broker (localhost,
ID:blrkec41498d-3757-1216889859509-0:0) is shutting down
Jul 24, 2008 2:27:40 PM org.apache.activemq.broker.BrokerService stop
INFO: ActiveMQ JMS Message Broker (localhost,
ID:blrkec41498d-3757-1216889859509-0:0) stopped

The consumer console window

Jul 24, 2008 2:30:23 PM org.apache.activemq.broker.BrokerService start
INFO: Using Persistence Adapter:
AMQPersistenceAdapter(activemq-data\localhost)
Jul 24, 2008 2:30:23 PM org.apache.activemq.broker.BrokerService getBroker
INFO: ActiveMQ null JMS Message Broker (localhost) is starting
Jul 24, 2008 2:30:23 PM org.apache.activemq.broker.BrokerService getBroker
INFO: For help or more information please see: http://activemq.apache.org/
Jul 24, 2008 2:30:23 PM org.apache.activemq.store.amq.AMQPersistenceAdapter
start
INFO: AMQStore starting using directory: activemq-data\localhost
Jul 24, 2008 2:30:23 PM org.apache.activemq.broker.jmx.ManagementContext$1
run
WARNING: Failed to start jmx connector: Cannot bind to URL
[rmi://localhost:1099/jmxrmi]: javax.naming.CommunicationException [Root
exception is java.rmi.ConnectIOException: error during JRMP connection
establishment; nested exception is: 
	java.net.SocketException: Software caused connection abort: recv failed]
Jul 24, 2008 2:30:23 PM org.apache.activemq.kaha.impl.KahaStore initialize
INFO: Kaha Store using data directory activemq-data\localhost\kr-store\state
Jul 24, 2008 2:30:23 PM org.apache.activemq.store.amq.AMQPersistenceAdapter
start
INFO: Active data files: [1]
Jul 24, 2008 2:30:23 PM org.apache.activemq.kaha.impl.KahaStore initialize
INFO: Kaha Store using data directory activemq-data\localhost\kr-store\data
Jul 24, 2008 2:30:23 PM org.apache.activemq.broker.BrokerService start
INFO: ActiveMQ JMS Message Broker (localhost,
ID:blrkec41498d-3764-1216890023650-0:0) started
Jul 24, 2008 2:30:24 PM org.apache.activemq.broker.TransportConnector start
INFO: Connector vm://localhost Started
Jul 24, 2008 2:30:24 PM org.apache.activemq.broker.TransportConnector stop
INFO: Connector vm://localhost Stopped
Jul 24, 2008 2:30:24 PM org.apache.activemq.broker.BrokerService stop
INFO: ActiveMQ Message Broker (localhost,
ID:blrkec41498d-3764-1216890023650-0:0) is shutting down
Jul 24, 2008 2:30:24 PM org.apache.activemq.broker.BrokerService stop
INFO: ActiveMQ JMS Message Broker (localhost,
ID:blrkec41498d-3764-1216890023650-0:0) stopped

Can u help me .

skg wrote:
> 
> 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-tp18627640p18627931.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


RE: Exception occurred: javax.jms.IllegalStateException: Cannot synchronously receive a message when a MessageListener is set

Posted by Sachindra Nath <Sa...@efi.com>.
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.