Skip to content

Commit

Permalink
Fix bug in AMQP appender that was causing loops in error conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrisbin committed Feb 23, 2012
1 parent ed1b8d8 commit 7baae85
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ target
*.iml
*.ipr
*.iws
.idea/
@@ -1,28 +1,25 @@
package com.jbrisbin.vcloud.logging;

import java.io.IOException;
import java.util.Calendar;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.ErrorCode;
import org.apache.log4j.spi.LoggingEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Calendar;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @author Jon Brisbin <jon.brisbin@npcinternational.com>
*/
public class RabbitMQAppender extends AppenderSkeleton {

private final Logger log = LoggerFactory.getLogger(getClass());
private final boolean DEBUG = log.isDebugEnabled();
private ConnectionFactory factory = new ConnectionFactory();
private Connection connection = null;
private Channel channel = null;
Expand Down Expand Up @@ -101,14 +98,14 @@ public String getExchange() {

public void setExchange(String exchange) {
this.exchange = exchange;
Channel mq = null;
Channel mq;
try {
mq = getChannel();
synchronized (mq) {
mq.exchangeDeclare(exchange, "topic", true, false, null);
}
} catch (IOException e) {
log.error(e.getMessage(), e);
errorHandler.error(e.getMessage(), e, ErrorCode.GENERIC_FAILURE);
}
}

Expand All @@ -133,7 +130,7 @@ protected void append(final LoggingEvent event) {
try {
workerPool.submit(new AppenderPublisher(event));
} catch (IOException e) {
log.error(e.getMessage(), e);
errorHandler.error(e.getMessage(), e, ErrorCode.WRITE_FAILURE);
}
}

Expand All @@ -142,14 +139,14 @@ public void close() {
try {
channel.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
errorHandler.error(e.getMessage(), e, ErrorCode.CLOSE_FAILURE);
}
}
if (null != connection && connection.isOpen()) {
try {
connection.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
errorHandler.error(e.getMessage(), e, ErrorCode.CLOSE_FAILURE);
}
}
}
Expand All @@ -168,19 +165,13 @@ protected void setFactoryDefaults() {

protected Channel getChannel() throws IOException {
if (null == channel || !channel.isOpen()) {
if (DEBUG) {
log.debug("Opening a new channel...");
}
channel = getConnection().createChannel();
}
return channel;
}

protected Connection getConnection() throws IOException {
if (null == connection || !connection.isOpen()) {
if (DEBUG) {
log.debug("Opening a new connection...");
}
connection = factory.newConnection();
}
return connection;
Expand Down
Expand Up @@ -16,15 +16,26 @@

package com.jbrisbin.vcloud.classloader;

import com.rabbitmq.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.URL;
import java.util.concurrent.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author J. Brisbin <jon@jbrisbin.com>
Expand Down

0 comments on commit 7baae85

Please sign in to comment.