20

我正在尝试从这里制作“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

这是什么原因造成的?

我在这里找到了解决我的问题的方法建立套接字连接时出错

4

8 回答 8

20

为了处理它,我安装了 RabbitMQ 服务器。如果未安装 rabbitmq-server,则会抛出此错误。通过点击http://localhost:15672/确保您已安装 RabbitMQ 服务器并且它已启动并运行

于 2013-03-21T05:59:09.550 回答
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)

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

NODE_IP_ADDRESS=10.0.1.45

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

于 2014-09-08T22:48:02.340 回答
1

我通过执行简单地解决了这个问题:

 sudo rabbitmq-server
于 2015-10-25T15:04:16.747 回答
1

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

于 2016-02-01T21:49:17.423 回答
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错误消息。希望这可以帮助。

于 2016-03-03T19:05:26.253 回答
1

有时你只需要重新启动 Mac。在这里尝试了所有其他解决方案以及来自不同问题的其他事情,尽管听起来很愚蠢,但重新启动最终让它恢复运行并能够访问 http://localhost:15672/

这是在我做了一个之后brew upgrade(这可能使我处于糟糕的状态)。

于 2020-11-18T17:13:26.943 回答
0

您必须启动 Rabbit MQ 服务器

在 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
于 2017-07-20T15:03:05.220 回答
-1

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

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

  1. brew uninstall rabbitmq
  2. brew install rabbitmq
  3. rabbitmq-server (并允许防火墙权限)
  4. 在本地运行应用程序。
于 2018-08-07T19:48:21.723 回答