rabbitmq connection refused

依然范特西╮ 提交于 2021-01-29 13:56:15

问题


I'm trying to consume a rabbitMq queue and append the message to a TextView but I get this error:

java.net.ConnectException:failed to connect to to /127.0.0.1(port 5672) from/127.0.0.1(port 49582)after 6000ms:isConnected failed:ECONNREFUSED(Connection refused).

I'm new to android and I don't know what to do.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String QUEUE_NAME = "Invitation2";
    TextView tv = findViewById(R.id.ident);
    Thread thread = new Thread(new Runnable(){
        public void run(){
            try {
                ConnectionFactory factory = new ConnectionFactory();
                factory.setHost("localhost");
                Connection connection = factory.newConnection();
                Channel channel = connection.createChannel();
                channel.queueDeclare(QUEUE_NAME, false, false, false, null);
                DeliverCallback deliverCallback = (consumerTag, delivery) -> {
                    String message = new String(delivery.getBody(), "UTF-8");
                    try {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tv.append(message);
                            }
                        });
                    }catch (Exception e){
                        Log.i("error", e.toString());
                    }
                };
                channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
            }catch (Exception e){
                Log.i("error", e.toString());
            }
        }
    });
    thread.start();
}

来源:https://stackoverflow.com/questions/61251592/rabbitmq-connection-refused

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!