How to timeout your Socket.io Client Listener

Hi!

[Versão em Português]

So I have bumped into an issue with the socket.io-client module where you cannot timeout listeners, so if for example you did this in your code:


 socket_client.on("my_event",(response)=>{
   
    console.log("Event responded");
   
});

There are no properties you can pass (As far as I have looked into it, I know there are open issues about this though) to timeout this listener, so if you have a long time client connected it will experience an increase of memory, this is not a memory leak though, the objects are actually there waiting to get into action, but there could be a situation where an error ocurred and your listener will stay there forever until you stop the process.

So to mitigate this you could add to your code this very simple function:


var addListener = (socket_client, eventName, 
                  callback, timeout)=>{

    // Adding the event listener as
    //you would normally do

    socket_client.on(eventName,callback);

    // If the listener is not cleaned yet
    //after timeout, this will do the trick

    setTimeout(()=>{

      if(socket_client.hasListeners(eventName)){

        socket_client.removeListener(eventName, callback);

        console.log(eventName + " listener was cleaned");

      }

    }, timeout);

  }

Thank you.

Advertisements

AWS Lambda Function Hello World! in Java

The below is literally all the code you have to do in order to have a functioning AWS Lamda function:

package com.wordpress.vrsbrazil.lambda.functions;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

/**
 * Created by vrsbrazil on 02/03/2017.
 */
public class TestFunctionHandler implements RequestHandler<Object, Object> {

    public Object handleRequest(Object o, Context context) {
        return ("Hello, " + o + "!");
    }

}

Here is the dependency you need:

<dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.0.0</version>
        </dependency>

Now to test it, you will need some simple JUnit coding, for this test coding here is the dependencies you will need:

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>

When you test a lambda function you will need a Lamda context, as you are making an offline test you will need to make you own, this is dead simple, you just need a class that implements com.amazonaws.services.lambda.runtime.Context, so here is the context I created:

import com.amazonaws.services.lambda.runtime.ClientContext;
import com.amazonaws.services.lambda.runtime.CognitoIdentity;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;

/**
 * Created by vrsbrazil on 02/03/2017.
 */
public class LamdaTestContext implements Context{
    public String getAwsRequestId() {
        return null;
    }

    public String getLogGroupName() {
        return null;
    }

    public String getLogStreamName() {
        return null;
    }

    public String getFunctionName() {
        return null;
    }

    public CognitoIdentity getIdentity() {
        return null;
    }

    public ClientContext getClientContext() {
        return null;
    }

    public int getRemainingTimeInMillis() {
        return 0;
    }

    public int getMemoryLimitInMB() {
        return 0;
    }

    public LambdaLogger getLogger() {
        return null;
    }
}

Finally, here is the test code:

import com.wordpress.vrsbrazil.lambda.functions.TestFunctionHandler;
import com.amazonaws.services.lambda.runtime.Context;
import org.junit.Assert;
import org.junit.Test;

/**
 * Created by vrsbrazil on 02/03/2017.
 */
public class TesteTestFunction {

    @Test
    public void testLambdaFunctionHandler() {

        TestFunctionHandler function = new TestFunctionHandler();
        Context ctx = new LamdaTestContext();
        String s = "Teste";

        Object output = function.handleRequest(s, ctx);

        if (output != null) {
            System.out.println(output.toString());
        }
        Assert.assertEquals("Hello, Teste!", output);


    }

}

Here is a git project for you: https://github.com/vrsbrazil/awslambdajavahelloworld

Thank you!

How to map mongodb collections on spring-data-mongodb when the names differ from your pojo definition


package com.wordpress.vrsbrazil;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

/*When collection name is different*/
@Document(collection="myCollectionName")
public class MyPojo {

@Id
public String id;

/*When field name is different*/
@Field("collectionFieldName")
public Integer myAttribute;

//getters and setters

}