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
I had this error because of incorrect usage of param: virtual-host: /ABC instead of virtual-host: ABC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With