package com.googlesource.gerrit.plugins.rabbitmq;

import com.google.gerrit.common.ChangeListener;
import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.gerrit.server.events.ChangeEvent;
import com.google.gson.Gson;
import com.google.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Timer;

public class RabbitMQManager implements ChangeListener, LifecycleListener {

  private static final Logger LOGGER = LoggerFactory.getLogger(RabbitMQManager.class);
  private final static int MONITOR_FIRATTIME_DELAY = 15000;
  private final Properties properties;
  private final AMQPSession.Factory factory;
  private AMQPSession session;
  private final Gson gson = new Gson();
  private final Timer monitorTimer = new Timer();
  private final ConnectionMonitorTask monitorTask;

  @Inject
  public RabbitMQManager(Properties properties, AMQPSession.Factory factory, ConnectionMonitorTask monitorTask) {
    this.properties = properties;
    this.factory = factory;
    this.monitorTask = monitorTask;
  }

  @Override
  public void start() {
    session = factory.create();
    session.connect();
    monitorTimer.schedule(monitorTask, MONITOR_FIRATTIME_DELAY, properties.getConnectionMonitorInterval());
  }

  @Override
  public void stop() {
    monitorTimer.cancel();
    if (session != null) {
      session.disconnect();
    }
    session = null;
  }

  @Override
  public void onChangeEvent(ChangeEvent event) {
    session.sendMessage(gson.toJson(event));
  }

}

@@ -635,47 +635,8 @@
 nal 
-AMQPSession.Factory factory;%0A  private 
 AMQP
@@ -652,16 +652,16 @@
 ession;%0A
+
   privat
@@ -867,24 +867,16 @@
 sion
-.Factory factory
+ session
 , Co
@@ -957,25 +957,25 @@
 his.
-factory = factory
+session = session
 ;%0A  
@@ -1053,40 +1053,8 @@
 ) %7B%0A
-    session = factory.create();%0A
     
@@ -1255,83 +1255,28 @@
     
-if (session != null) %7B%0A      session.disconnect();%0A    %7D%0A    session = null
+session.disconnect()
 ;%0A  

Update RabbitMQManager