RabbitMQ“Hello World”示例给出“Connection Refused”

时间:2013-03-15 14:22:32

标签: java exception-handling rabbitmq

我试图从这里制作“hello world”应用程序:RabbitMQ Hello World

以下是我的制作人类的代码:

package com.mdnaRabbit.producer;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

import java.io.IOException;

public class App {

    private final static String QUEUE_NAME = "hello";

    public static void main( String[] argv) throws IOException{
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello World!";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(" [x] Sent" + "'");
        channel.close();
        connection.close();
    }
}

这就是我在实施时所得到的:

Exception in thread "main" java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at com.rabbitmq.client.ConnectionFactory.createFrameHandler(ConnectionFactory.java:445)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:504)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:533)
at com.mdnaRabbit.producer.App.main(App.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 1

造成这种情况的原因是什么?

我在Error in making a socket connection

找到了问题的解决方案

8 个答案:

答案 0 :(得分:15)

为了解决这个问题,我安装了RabbitMQ服务器。如果未安装rabbitmq-server,则会抛出此错误。

答案 1 :(得分:3)

我也收到了“拒绝连接”错误:

Exception in thread "main" java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at com.rabbitmq.client.impl.FrameHandlerFactory.create(FrameHandlerFactory.java:32)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:588)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:612)
at ReceiveLogs.main(ReceiveLogs.java:14)

我通过将/etc/rabbitmq/rabbitmq-env.conf内的IP地址设置为错误的IP地址而犯了一个错误:

NODE_IP_ADDRESS=10.0.1.45

我删除了此配置参数,错误就消失了。

答案 2 :(得分:1)

我只是通过执行:

解决了这个问题
 sudo rabbitmq-server

答案 3 :(得分:1)

启动Rabbit MQ Server。启动此服务器的批处理文件存在于rabbitmq_server-3.6.0 \ sbin> rabbitmq-server.bat启动下,然后它将起作用。

答案 4 :(得分:1)

在我的情况下,它尝试启动服务器时出现以下错误 <Rabbit intall path>\rabbitmq_server-3.6.0\sbin>rabbitmq-server.bat start ERROR: epmd error for host Protocol: inet_tcp: register/listen error: econnrefused: nxdomain (non-existing domain)

我所做的是在我的host文件中添加以下行: 127.0.0.1 localhost

然后启动了rabbitmq-server。在此之后,我再也没有得到connection refuse错误。希望这会有所帮助。

答案 5 :(得分:0)

你必须启动Rabbit MQ Serever

在Windows文件名中:RabbitMQ服务 - 启动

您可以使用此代码:

import java.io.IOException;
import java.util.ResourceBundle;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class NewTaskController implements Runnable {
    private final String message;

     private static final String EXCHANGE_NAME = "test";

     private static final String ROUTING_KEY = "test";

     public NewTaskController(final String message) {
         this.message = message;
     }

     @Override
     public void run() {

        //getting data from application.properties
         //for the rabbit_mq configuration

         ResourceBundle mRB = ResourceBundle.getBundle("application");


         System.out.println("*****NewTaskController************"+mRB.getString("rabbitmq.port"));
        String rabbitmq_username = mRB.getString("rabbitmq.username");
        String rabbitmq_password = mRB.getString("rabbitmq.password");
                String rabbitmq_hostname = mRB.getString("rabbitmq.hostname");
                        int rabbitmq_port = Integer.parseInt(mRB.getString("rabbitmq.port"));
     ConnectionFactory factory = new ConnectionFactory();
     factory.setUsername(rabbitmq_username);
     factory.setPassword(rabbitmq_password);
     factory.setHost(rabbitmq_hostname);
     factory.setPort(rabbitmq_port);
     Connection conn;
     try {
     conn = factory.newConnection();
     Channel channel = conn.createChannel();

     channel.exchangeDeclare(EXCHANGE_NAME, "direct", true);
     String queueName = channel.queueDeclare().getQueue();
     System.out.println(queueName);
     channel.queueBind(queueName, EXCHANGE_NAME, ROUTING_KEY);
     System.out.println("Producing message: " + message + " in thread: " + Thread.currentThread().getName());
     channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, null, message.getBytes());

     try {
        channel.close();
    } catch (TimeoutException e) {
        e.printStackTrace();
    }
     conn.close();
     } catch (IOException | TimeoutException e) {
     e.printStackTrace();
     }
}
}

application.properties文件:

rabbitmq.username=guest
rabbitmq.password=guest
rabbitmq.hostname=localhost
rabbitmq.port=5672

答案 6 :(得分:0)

有时候您只需要重新启动Mac。在这里尝试了所有其他解决方案以及其他问题,听起来很愚蠢,重新启动才使它最终重新开始运行并能够运行到达http:// localhost:15672 /

这是在我完成brew upgrade之后(这可能使我处于不良状态)。

答案 7 :(得分:-1)

如果您已经在本地安装RabbitMQ,则简单的解决方法的确是rabbitmq-server

从Mac OS X Sierra迁移到High Sierra后,我遇到了此问题,作为防火墙问题。我已经安装了RabbitMQ。但是,我一直收到此Connection Refused错误。我必须执行以下操作:

  1. brew uninstall rabbitmq
  2. brew install rabbitmq
  3. rabbitmq-server (并允许防火墙权限)
  4. 在本地运行应用。