[rabbitmq-discuss] Exception in Creating Rabbit MQ Connection

sagar sagar131 at gmail.com
Fri Feb 25 06:18:23 GMT 2011


 Hi guys ,
              I am using RabbitMQ version 2.3 on server and in code I
am using java RabbitMQ client jar 2.3.1.

              The whole application is in Scala in which there are 10
actors each has its own  Queue on RabbitMQ  Server.
               each Actor is accessing its Queue through its own
methods like get Message and addMessage.
               below I have  attached One sample code for Actor
methods in Queue Manger .

               in every actor continuously checks for any Message in
queue like


      def act {
               this ! 1
                loop {
                  react {
                        case 1 =>
processMessage(QueueManager.getMessage); this ! 1

                         }
                       }
                      }

          this works fine ..But sometimes it gives me error like

 Exception in Creating Rabbit MQ Connection
java.net.ConnectException: Connection timed out
       at java.net.PlainSocketImpl.socketConnect(Native Method)
       at
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:
310)
       at
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:
176)
       at
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:
163)
       at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
       at java.net.Socket.connect(Socket.java:546)
       at java.net.Socket.connect(Socket.java:495)
       at
com.rabbitmq.client.ConnectionFactory.createFrameHandler(ConnectionFactory.java:
338)
       at
com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:
376)
       at
com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:
399)


    The server is running on its default port 5672. and actors are
running on same machine .
 the QueueManager is


object QueueManager {
  private val logger: Logger = Logger.getLogger(this.getClass.getName)
  private val FACTORY = RabbitMQConstants.getFactory

  private var conn: Connection = null
  private var channel: Channel = null

  /**
   * @return
   * com.prolinkd.actor.QueueObject
   * reads a Message from rabbitMQ Queue
   */
  def getMessage: QueueObject = synchronized{
    if (conn == null || (channel != null && (!channel.isOpen))) {
      getConnection
      if (conn == null) {
        return null
      }
    }
    var obj = new ConnectionObject
    try {
      val response = channel.basicGet(CONNECTION_QUEUE, false)
      if (response == null) return null
      val msg = new java.io.ObjectInputStream(
        new
java.io.ByteArrayInputStream(response.getBody)).readObject()
      obj = msg.asInstanceOf[ConnectionObject]
      channel.basicAck(response.getEnvelope().getDeliveryTag(), false)
    } catch {
      case e: Exception => logger.error("error in Get Message", e); //
endConnection
    }
    //endConnection
    obj
  }


  /**
   * Get Connected with RabbitMQ Server
   */
  private def getConnection {
    try {
      conn = FACTORY.newConnection
      if (conn == null) {
        logger.info(" FAILED TO CREATE CONNECTION WITH RABBITMQ ON  "
+ FACTORY.getHost + ":" + FACTORY.getPort)
      }
      channel = conn.createChannel
      if (channel == null) {
        logger.info(" FAILED TO CREATE CHANNEL WITH RABBITMQ ON  " +
FACTORY.getHost + ":" + FACTORY.getPort)
      }
      if (conn != null) {
        val durable = true
        channel.exchangeDeclare(CONNECTION_EXCHANGE, "direct",
durable)
        channel.queueDeclare(CONNECTION_QUEUE, durable, false, false,
null)
        channel queueBind (CONNECTION_QUEUE, CONNECTION_EXCHANGE,
CONNECTION_ROUTING_KEY)
      }
    } catch {
      case e: Exception => logger.error("Exception In Getting
Connection to RabbitMQ ", e)
    }

  }

}


we can't figure out the particular issue.can you guys  help us please


More information about the rabbitmq-discuss mailing list