17

I have installed "erlang" and "rabbitmq" in my windows 7 machine. But when I am trying to run this code I am getting One exception.

package com.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class SendMessage {
  private final static String QUEUE_NAME = "hello";
  public static void main(String[] argv) throws Exception { 
    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("UTF-8"));
    System.out.println(" [x] Sent '" + message + "'");
    channel.close();
    connection.close();
  }
}

I am getting this Exception.

Exception in thread "main" com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.

This is the log:

11-Apr-2016::12:45:06 === Adding vhost 'localhost'

=INFO REPORT==== 11-Apr-2016::14:08:52 === accepting AMQP connection <0.360.0> (127.0.0.1:55327 -> 127.0.0.1:5672)

=ERROR REPORT==== 11-Apr-2016::14:08:52 === Error on AMQP connection <0.360.0> (127.0.0.1:55327 -> 127.0.0.1:5672, state: starting):

=INFO REPORT==== 11-Apr-2016::14:08:52 === closing AMQP connection <0.360.0> (127.0.0.1:55327 -> 127.0.0.1:5672)

When I am trying to list the users I am not getting any existing user and add_user is also not working in cmd link

ssuperczynski
  • 3,190
  • 3
  • 44
  • 61
Chiranjit.B
  • 241
  • 1
  • 3
  • 6
  • Please [edit] your post and add the exception there rather than in a comment. – Jiri Tousek Apr 11 '16 at 08:44
  • Seems you are missing the crypto package, where did you download the Erlang setup? – Gabriele Santomaggio Apr 11 '16 at 10:06
  • i have downloaded the Erlang file from "http://erlangcentral.org/downloads/" – Chiranjit.B Apr 11 '16 at 10:32
  • remove it, a try with this http://erlang.org/download/otp_win64_18.3.exe – Gabriele Santomaggio Apr 11 '16 at 12:10
  • I have similar issue in UbuntuVM that is on Azure Cloud. We had opened up ports `5671 & 5672` as `tcp` ports, wherein there was a option to open up a port as there was `RabbitMQ` or `AMQP` option in drop down list in `firewall` section, I think once I do that and retry, I could overcome this issue. In legacy `tcp` way, there are some blockers I hope. – Dev Anand Sadasivam May 02 '18 at 07:18
  • `5671` is needed in my case for `shovel plugin` for `broker` based message push from one rabbitmq to other rabbitmq, when I checked its,- for SSL/TLS layer this port is used. I'm not sure, when I turned this port on, shovel push we tried was succeeded. – Dev Anand Sadasivam May 02 '18 at 07:24
  • Or it could ipv6 issue of `:::`, for `netstat -tuplen|grep 5672`, it returns `tcp6 0 0 :::5672 :::* LISTEN 114 20147390 -` as [Discussed what is ip ::: in ipv6](https://stackoverflow.com/questions/27480094/ipv6-is-equivalent-to-0-0-0-0-when-listening-for-connections). As per here `::1:5672` is loopback in ipv6. In `:::5672` double colon `::` is ipv6 unspecified. – Dev Anand Sadasivam May 02 '18 at 07:47
  • for `grep 114` I could see ports are listening for `4369`, `15672`, `25672` as well on `0.0.0.0:port` – Dev Anand Sadasivam May 02 '18 at 07:50
  • I think `Erlang` takes care of this `:::`,- `unspecified`,- `ipv6` issue as it hasn't caused issue. – Dev Anand Sadasivam May 15 '18 at 09:27
  • 1
    Does this answer your question? [Spring AMQP + RabbitMQ 3.3.5 ACCESS\_REFUSED - Login was refused using authentication mechanism PLAIN](https://stackoverflow.com/questions/26811924/spring-amqp-rabbitmq-3-3-5-access-refused-login-was-refused-using-authentica) – Jon Schneider Apr 28 '20 at 15:06

3 Answers3

20

In your ConnectionFactory you need to set your username and password, if your have created any or you can use the default user "guest" with password "guest", which can be accessible only from localhost.

Manmay
  • 835
  • 7
  • 11
  • yes I tried this. factory.setHost("localhost"); factory.setUsername("guest"); factory.setPassword("guest"); But still i am getting the same Exception. – Chiranjit.B Apr 11 '16 at 11:18
  • From the first line of error log : Adding vhost 'localhost'. Have you created any new virtual host on your local RabbitMQ ? Because the default virtual host should be "/" . – Manmay Apr 11 '16 at 13:56
  • And you can create users with RabbitMQ admin console (through browser) – Manmay Apr 11 '16 at 13:57
  • 1
    No i have not created any vhost on local RabbitMQ . Admin console also not opening in browser. – Chiranjit.B Apr 12 '16 at 04:43
  • 1
    Please install RabbitmQ Management Plugin . Use the command : "rabbitmq-plugins enable rabbitmq_management" then you can access the admin console. – Manmay Apr 12 '16 at 08:10
  • maybe you need this factory.setVirtualHost(); – Jack1987 Nov 21 '16 at 17:27
  • If I delete `guest` account for RabbitMQ, following error is raising as commented here in this [feed](https://stackoverflow.com/questions/50170318/java-lang-reflect-invocationtargetexception-rabbitmq-guest-login-deletion-impl#comment87362804_50170318) – Dev Anand Sadasivam May 09 '18 at 05:42
  • By default, the `guest` user is prohibited from connecting to the broker remotely; it can only connect over a `loopback` interface (i.e. `localhost`). This applies both to `AMQP 0-9-1` and to any other protocols enabled via plugins. Any other users you create will not (by default) be restricted in this way. This [excerpt](https://www.rabbitmq.com/access-control.html) I got from RabbitMQ [site](https://www.rabbitmq.com/access-control.html). Its simple as that port `15672` not been exposed over the net, which `Admin Console`,- `port` of `RabbitMQ`. – Dev Anand Sadasivam May 09 '18 at 05:55
  • `application.yml`,- spring boot configuration file has `guest/guest` at `localhost` under `rabbitmq:` section, under the `exchange` we were working on, that had caused the problem as it was overriding my `json` config entries in my `spring boot application`. – Dev Anand Sadasivam May 15 '18 at 09:27
5

you can create new user (userA) and password (userA123).
And set

factory.setHost("your_pc_ip");
factory.setUsername("userA");
factory.setPassword("userA123");

in sender and receiver classes.

Ashok
  • 61
  • 1
  • 2
0

It's a bit vexing to come across this error message while following the official helloworld guide exactly.

With some research, here's what works for me:

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("10.0.0.2");   // * default rabbitmq port: 5672
factory.setUsername("myUsr");  // ensure a new user is created on the server
factory.setPassword("p@ssword");
factory.setVirtualHost("vh1"); // ensure a new virtual host is created on the server
                               // and the user has granted the access right to it

try (Connection connection = factory.newConnection();
     Channel channel = connection.createChannel()) {

    // create a queue
    String queueName = "myQueueName";
    Boolean durable = false;    // if ture, messages will be retained even after server is restarted
    Boolean exclusive = false;  // if true, other connections will not be able to access this queue
    Boolean autoDelete = false; // if true, queue is deleted when all consumers have finished using it
    Map<String, Object> arguments = null;
    channel.queueDeclare(queueName, durable, exclusive, autoDelete, arguments);

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

To add a new virtual host from web management UI:

enter image description here

To add a new user from web management UI:

enter image description here

To grant user the access right to virtual host:

(click the user name to enter the detail page) enter image description here

Rohim Chou
  • 907
  • 10
  • 16