When using the latest version 5.0.0 of the rabbitmq client.jar sending a message is resulting in the following NPE:
java.lang.ExceptionInInitializerError
at com.rabbitmq.client.impl.AMQConnection.defaultClientProperties(AMQConnection.java:75)
at com.rabbitmq.client.ConnectionFactory.<init>(ConnectionFactory.java:100)
at
<skipped>
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at com.rabbitmq.client.impl.ClientVersion.<clinit>(ClientVersion.java:35)
... 89 more
The code used for sending is taken from the most simple example and looks as follows:
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try {
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"));
channel.close();
connection.close();
} catch (Exception e) {
LOG.error("Error publishing event", e);
}
The reason for this exception was traced down to the following lines of code in the ClientVersion-class.
public class ClientVersion {
private static final Properties version = new Properties();
public static final String VERSION;
public ClientVersion() {
}
static {
InputStream inputStream = ClientVersion.class.getClassLoader().getResourceAsStream("version.properties");
try {
version.load(inputStream);
} catch (IOException var10) {
;
} finally {
try {
if(inputStream != null) {
inputStream.close();
}
} catch (IOException var9) {
;
}
}
VERSION = version.getProperty("com.rabbitmq.client.version", ClientVersion.class.getPackage().getImplementationVersion());
}
}
This statement resolves to NULL: InputStream inputStream = ClientVersion.class.getClassLoader().getResourceAsStream("version.properties");
The reason is that the wrong class loader is used (the bundle class loader) as this is running in an OSGI-bundle and thus the version.properties file cannot be found.
We kind of "solved" this problem by switching to an earlier version (3.5.1) of the rabbitmq client, but this is for sure more a workaround than a real solution ;-).
Question now is: Is there a solution to still make use of the latest version in an OSGI context?
Thanks in advance
- Thomas