简体   繁体   中英

RabbitMQ - How i catch ForgivingExceptionHandler?

I'm trying to catch/cover all unexpected error such as error connection in rabbitmq. i've tried all what i think is related Exception (Exception, IOException, SocketException) even the general exception itself (all of this is not catching the error) so i can prefer other flow, maybe as example: doing log submit using 3rd party system, or notify admin via email while maintain code to reconnect for 3 more times instead showing this error message and stopping all execution.

Here's the uncatched error message :

16:27:51.476 [AMQP Connection 192.168.7.167:5672] ERROR com.rabbitmq.client.impl.ForgivingExceptionHandler - An unexpected connection driver error occured
java.net.SocketException: Socket Closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:171)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
    at java.io.DataInputStream.readUnsignedByte(DataInputStream.java:288)
    at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:91)
    at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:184)
    at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:598)
    at java.lang.Thread.run(Thread.java:748)

Here's my code :


    package TestPackage;

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

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.SocketException;
    import java.util.Properties;
    import java.util.concurrent.TimeoutException;

    public class TestQueueSend {
        static ConnectionFactory rbmqFactory;
        static Connection rbmqConn;
        static Channel rbmqChannel;

        public static void main(String[] args){
            Properties rabbitMQConf = new Properties();
            try {
                rabbitMQConf.load(new FileInputStream("./config/rabbitmqconf.properties"));
            } catch (IOException errorLoadRabbitMQConfig) {
                errorLoadRabbitMQConfig.getStackTrace();
            }

            rbmqFactory = new ConnectionFactory();
            rbmqFactory.setUsername(rabbitMQConf.getProperty("rabbit_mq_username"));
            rbmqFactory.setPassword(rabbitMQConf.getProperty("rabbit_mq_password"));
            rbmqFactory.setVirtualHost(rabbitMQConf.getProperty("rabbit_mq_virtualHost"));
            rbmqFactory.setHost(rabbitMQConf.getProperty("rabbit_mq_host"));
            rbmqFactory.setPort(Integer.parseInt(rabbitMQConf.getProperty("rabbit_mq_port")));

            System.out.println("Running RabbitMQConnection!");

            try {
                System.out.println("Connection START : " + rabbitMQConf.getProperty("rabbit_mq_host"));
                rbmqConn = rbmqFactory.newConnection();

                System.out.println("Create Channel");
                rbmqChannel = rbmqConn.createChannel();

                String queueName = "queueTest";
                String queueContent = "Kasur ini rusak";

                System.out.println("Queue Declare");
                rbmqChannel.queueDeclare(queueName,false,false,false,null);
                rbmqChannel.basicPublish("",queueName,null,queueContent.getBytes());

                System.out.println(" [x] Sent '" + queueContent + "'");

                rbmqChannel.close();
                rbmqConn.close();
            } catch(SocketException errorSocketCatch) {
                System.out.println("ERROR_SOCKET");
                System.out.println(errorSocketCatch.getMessage());
            } catch(IOException errorIOCatch) {
                System.out.println("ERROR_IO");
                System.out.println(errorIOCatch.getMessage());
            } catch (TimeoutException errorTimeoutCatch) {
                System.out.println("ERROR_TIMEOUT");
                System.out.println(errorTimeoutCatch.getMessage());
            } catch (Exception errorGeneric) {
                System.out.println("ERROR_GENERIC");
                System.out.println(errorGeneric.getMessage());
            }
        }
    }

for other information, this is what i use :

Maven 3.3 
com rabbitmq amqp client 5.7.2 (5.7.1 not work too)
org slf4j slf4j api 1.7.26 
ch qos logback classic:1.2.3

for the ide i'm using :

IntelliJ IDEA 2019.1.3 (Community Edition)
Build #IC-191.7479.19, built on May 28, 2019
JRE: 1.8.0_202-release-1483-b58 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0

由于 param: virtual-host: /ABC而不是virtual-host: ABC的错误使用,我遇到了这个错误

Because the exception occurs in another thread in third-party code, you cannot actually catch it without modifying that code. However, you can hide it, as you requested to do in a comment.

To hide this exception you could edit your logging configuration file (logback.xml for logback in your case) and set the log level for the com.rabbitmq.client.impl.ForgivingExceptionHandler to OFF . But I would not advise doing this in general as it could hide important diagnostic information. So a better solution might be one of the following:

  1. Call System.exit(0) to exit the program before the exception happens
  2. Alternatively, set the same log level dynamically to OFF at the end of the program.

This happened to me because the rabbitmq user did not have permission to access the virtual host. After adding the permission the exception disappeared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM