diff --git a/src/main/java/com/mashape/analytics/agent/connection/pool/Work.java b/src/main/java/com/mashape/analytics/agent/connection/pool/Executor.java similarity index 97% rename from src/main/java/com/mashape/analytics/agent/connection/pool/Work.java rename to src/main/java/com/mashape/analytics/agent/connection/pool/Executor.java index 3646cbb..abcd87d 100644 --- a/src/main/java/com/mashape/analytics/agent/connection/pool/Work.java +++ b/src/main/java/com/mashape/analytics/agent/connection/pool/Executor.java @@ -25,7 +25,7 @@ a copy of this software and associated documentation files (the import java.util.Map; -public interface Work { +public interface Executor { void terminate(); void execute(Map analyticsData); } diff --git a/src/main/java/com/mashape/analytics/agent/connection/pool/MessangerPool.java b/src/main/java/com/mashape/analytics/agent/connection/pool/MessangerPool.java index 5b5f1f5..bbfb24c 100644 --- a/src/main/java/com/mashape/analytics/agent/connection/pool/MessangerPool.java +++ b/src/main/java/com/mashape/analytics/agent/connection/pool/MessangerPool.java @@ -20,21 +20,43 @@ a copy of this software and associated documentation files (the LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ + */ package com.mashape.analytics.agent.connection.pool; + +import org.apache.log4j.Logger; + +import com.mashape.analytics.agent.filter.AnalyticsFilter; + /* * Messenger pool with each messenger having their own zmq socket */ public class MessangerPool { + + final static Logger logger = Logger.getLogger(MessangerPool.class); + private static final ThreadLocal MESSANGERPOOL = new ThreadLocal() { + + @Override + public void remove() { + Messenger messenger = MESSANGERPOOL.get(); + logger.debug("Messenger removed: " + messenger.toString()); + messenger.terminate(); + super.remove(); + } + protected Messenger initialValue() { + Messenger messenger = new Messenger(); + logger.debug("Messenger Created: " + messenger.toString()); return new Messenger(); } }; - - public static Messenger get(){ + + public static Messenger get() { return MESSANGERPOOL.get(); } + public static void remove() { + MESSANGERPOOL.remove(); + } } diff --git a/src/main/java/com/mashape/analytics/agent/connection/pool/Messenger.java b/src/main/java/com/mashape/analytics/agent/connection/pool/Messenger.java index ec8e1c7..6423129 100644 --- a/src/main/java/com/mashape/analytics/agent/connection/pool/Messenger.java +++ b/src/main/java/com/mashape/analytics/agent/connection/pool/Messenger.java @@ -50,15 +50,17 @@ a copy of this software and associated documentation files (the /* * Opens a connection to Analytics server and sends data */ -public class Messenger implements Work { +public class Messenger implements Executor { + + private static Logger LOGGER = Logger.getLogger(Messenger.class); - Logger logger = Logger.getLogger(Messenger.class); private ZMQ.Context context; private ZMQ.Socket socket; - + public Messenger(){ context = ZMQ.context(1); socket = context.socket(ZMQ.PUSH); + LOGGER.debug("Socket created: " + socket.toString()); } public void execute(Map analyticsData) { @@ -68,12 +70,12 @@ public void execute(Map analyticsData) { String port = analyticsData.get(ANALYTICS_SERVER_PORT).toString(); socket.connect("tcp://" + analyticsServerUrl + ":" + port); socket.send(data); - logger.debug("Message sent:" + data); + LOGGER.debug("Message sent:" + data); } public void terminate() { if (socket != null) { - logger.debug("Closing socket:" + socket.toString()); + LOGGER.debug("Closing socket:" + socket.toString()); socket.close(); } if (context != null) { @@ -118,6 +120,7 @@ private Creator setCreator() { @Override protected void finalize() throws Throwable { this.terminate(); - logger.debug("Messanger resources destroyed:"+ this.toString()); + LOGGER.debug("Messanger resources destroyed:"+ this.toString()); + super.finalize(); } } diff --git a/src/main/java/com/mashape/analytics/agent/connection/pool/SendAnalyticsTask.java b/src/main/java/com/mashape/analytics/agent/connection/pool/SendAnalyticsTask.java index 796e65f..6993744 100644 --- a/src/main/java/com/mashape/analytics/agent/connection/pool/SendAnalyticsTask.java +++ b/src/main/java/com/mashape/analytics/agent/connection/pool/SendAnalyticsTask.java @@ -25,13 +25,21 @@ a copy of this software and associated documentation files (the import java.util.Map; +import org.apache.log4j.Logger; + +import com.mashape.analytics.agent.filter.AnalyticsFilter; + /* * Task use a pooled Messenger to send data */ public class SendAnalyticsTask implements Runnable { + + final static Logger LOGGER = Logger.getLogger(AnalyticsFilter.class); + private Map analyticsData; public SendAnalyticsTask(Map analyticsData) { + LOGGER.debug("New task created:" + this.toString()); this.analyticsData = analyticsData; } diff --git a/src/test/java/com/mashape/analytics/agent/connection/pool/SendAnalyticsTaskTest.java b/src/test/java/com/mashape/analytics/agent/connection/pool/SendAnalyticsTaskTest.java index 77591df..3a5ea31 100644 --- a/src/test/java/com/mashape/analytics/agent/connection/pool/SendAnalyticsTaskTest.java +++ b/src/test/java/com/mashape/analytics/agent/connection/pool/SendAnalyticsTaskTest.java @@ -24,7 +24,7 @@ public class SendAnalyticsTaskTest { - private Work mockMessanger = new Messenger(){ + private Executor mockMessanger = new Messenger(){ @Override public void terminate() { val.addAndGet(1); diff --git a/src/test/java/com/mashape/analytics/agent/filter/AnalyticsFilterTest.java b/src/test/java/com/mashape/analytics/agent/filter/AnalyticsFilterTest.java index 748d111..f03fa78 100644 --- a/src/test/java/com/mashape/analytics/agent/filter/AnalyticsFilterTest.java +++ b/src/test/java/com/mashape/analytics/agent/filter/AnalyticsFilterTest.java @@ -53,7 +53,7 @@ import org.junit.runner.RunWith; import com.mashape.analytics.agent.connection.pool.Messenger; -import com.mashape.analytics.agent.connection.pool.Work; +import com.mashape.analytics.agent.connection.pool.Executor; import com.mashape.analytics.agent.mapper.AnalyticsDataMapper; import com.mashape.analytics.agent.modal.Entry; import com.mashape.analytics.agent.modal.Timings; @@ -90,7 +90,7 @@ public class AnalyticsFilterTest { @Injectable - private Work messanger = new Messenger() { + private Executor messanger = new Messenger() { @Override public void terminate() {