Skip to content

Commit

Permalink
cleaned the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Shashi Ranjan committed Jun 24, 2015
1 parent 633a023 commit ffa9a9b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> analyticsData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Messenger> MESSANGERPOOL = new ThreadLocal<Messenger>() {

@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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> analyticsData) {
Expand All @@ -68,12 +70,12 @@ public void execute(Map<String, Object> 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) {
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> analyticsData;

public SendAnalyticsTask(Map<String, Object> analyticsData) {
LOGGER.debug("New task created:" + this.toString());
this.analyticsData = analyticsData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class SendAnalyticsTaskTest {



private Work mockMessanger = new Messenger(){
private Executor mockMessanger = new Messenger(){
@Override
public void terminate() {
val.addAndGet(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,7 +90,7 @@ public class AnalyticsFilterTest {


@Injectable
private Work messanger = new Messenger() {
private Executor messanger = new Messenger() {

@Override
public void terminate() {
Expand Down

0 comments on commit ffa9a9b

Please sign in to comment.