Home > database >  Getting SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" for rabbitmq
Getting SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" for rabbitmq

Time:07-11

I am trying to learn rabbitmq with it's JAVA APIs.

Producer

package org.rabbitmq.org.rabbitmq.helloworld;
import  com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

public class Producer {
    private static final String  QUEUE_NAME="hello";

    public static void main(String[] args) throws Exception{
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("http://localhost:15672");

        try(Connection conn = factory.newConnection(); Channel channel = conn.createChannel()){
            channel.queueDeclare(QUEUE_NAME,false,false,false,null);
            String msg = "Hi, i am Rahul";
            System.out.println("Pushed message -> " msg);
        }
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>Java</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>Java</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
    <dependency>
      <groupId>com.rabbitmq</groupId>
      <artifactId>amqp-client</artifactId>
      <version>5.15.0</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Upon running the Producer.java i am getting the following error.

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.net.UnknownHostException: http://localhost:15672: nodename nor servname provided, or not known
    at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:52)
    at java.base/java.net.InetAddress$PlatformResolver.lookupByName(InetAddress.java:1048)
    at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1638)
    at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:997)
    at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1628)
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1494)
    at com.rabbitmq.client.DnsRecordIpAddressResolver.resolveIpAddresses(DnsRecordIpAddressResolver.java:83)
    at com.rabbitmq.client.DnsRecordIpAddressResolver.getAddresses(DnsRecordIpAddressResolver.java:73)
    at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:58)
    at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:160)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1216)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1173)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1131)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1294)
    at org.rabbitmq.org.rabbitmq.helloworld.Producer.main(Producer.java:13)

Note : My rabbitmq server is running on port 15672. enter image description here

CodePudding user response:

Firstly, it's worth pointing out that the message from SLF4J isn't the real problem here. That's just a warning saying that SLF4J couldn't find a suitable logging library to use to write logs.

The real problem here is the UnknownHostException. I believe the problem is caused by this line:

        factory.setHost("http://localhost:15672");

You need to pass the host name to factory.setHost, but you are passing a URL instead, and this URL contains a protocol (http) and a port number (15672) as well as the host name (localhost).

Connections to RabbitMQ appear not to use the HTTP protocol, so the http:// part is incorrect. Also, it seems the port number is passed using a separate method call rather than as part of the hostname.

Try the following instead:

        factory.setHost("localhost");
        factory.setPort(15672);

I have to admit that I've never used RabbitMQ before, so I can't guarantee that the code above works. I've based my answer on the 'Connecting to RabbitMQ' section of the RabbitMQ API guide.

  • Related