Use mongodb in smartfox server

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

zakir
Posts: 4
Joined: 01 Mar 2019, 10:19

Use mongodb in smartfox server

Postby zakir » 01 Mar 2019, 10:32

Hi,

I am using mongodb for database in smartfox server. I am sharing my code, i just want to know can i reuse my Zone Mongo Connection(MongoClinet) directly inside the room or i have to create new Mongo Connection for room.

MyZoneExtension.java :-

Code: Select all

import com.mongodb.MongoException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.smartfoxserver.v2.core.SFSEventType;
import com.smartfoxserver.v2.extensions.SFSExtension;
import sfs2x.extension.poker.db.MongoDBManager;

public class MyZoneExtension extends SFSExtension {
    @Override
    public void init() {
        trace("======init()=====");
        MongoDBManager.initializeConnection(this);
       
        this.addEventHandler(SFSEventType.SERVER_READY, ConnectionCheckHandler.class);
        this.addEventHandler(SFSEventType.USER_LOGIN, LoginEventHandler.class);
    }

    @Override
    public void destroy() {
        MongoDBManager.getInstance().closeConnection();
    }

}


MongoDBManager.java :-

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import com.smartfoxserver.v2.extensions.SFSExtension;

public class MongoDBManager {

    private static MongoDBManager mongoDbManager = null;
    private MongoClient mongoClient = null;
    private MongoDatabase database = null;
    private static SFSExtension ext;

    private MongoDBManager() {
        if (mongoClient == null) {
            mongoClient = MongoClients.create("<mongo db uri>");
            database = mongoClient.getDatabase("test_slots_db");
        }
       
        ext.trace("====MongoDBManager()===");
    }
   
    public static void initializeConnection (SFSExtension extension) {
        ext = extension;
        if (mongoDbManager == null) {
            mongoDbManager = new MongoDBManager();
        }
    }

    public static MongoDBManager getInstance() {
        return mongoDbManager;
    }

    public MongoClient getConnection() {
        return mongoClient;
    }
   
    public MongoDatabase getDBManager() {
        return database;
    }

    public void closeConnection() {
        if (mongoClient != null) {
            mongoClient.close();
        }
        ext.trace("====closeConnection()===");
    }
}
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Use mongodb in smartfox server

Postby Lapo » 01 Mar 2019, 11:04

zakir wrote:Hi,

I am using mongodb for database in smartfox server. I am sharing my code, i just want to know can i reuse my Zone Mongo Connection(MongoClinet) directly inside the room or i have to create new Mongo Connection for room.

MyZoneExtension.java :-

Code: Select all

import com.mongodb.MongoException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.smartfoxserver.v2.core.SFSEventType;
import com.smartfoxserver.v2.extensions.SFSExtension;
import sfs2x.extension.poker.db.MongoDBManager;

public class MyZoneExtension extends SFSExtension {
    @Override
    public void init() {
        trace("======init()=====");
        MongoDBManager.initializeConnection(this);
       
        this.addEventHandler(SFSEventType.SERVER_READY, ConnectionCheckHandler.class);
        this.addEventHandler(SFSEventType.USER_LOGIN, LoginEventHandler.class);
    }

    @Override
    public void destroy() {
        MongoDBManager.getInstance().closeConnection();
    }

}


MongoDBManager.java :-

Code: Select all

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import com.smartfoxserver.v2.extensions.SFSExtension;

public class MongoDBManager {

    private static MongoDBManager mongoDbManager = null;
    private MongoClient mongoClient = null;
    private MongoDatabase database = null;
    private static SFSExtension ext;

    private MongoDBManager() {
        if (mongoClient == null) {
            mongoClient = MongoClients.create("<mongo db uri>");
            database = mongoClient.getDatabase("test_slots_db");
        }
       
        ext.trace("====MongoDBManager()===");
    }
   
    public static void initializeConnection (SFSExtension extension) {
        ext = extension;
        if (mongoDbManager == null) {
            mongoDbManager = new MongoDBManager();
        }
    }

    public static MongoDBManager getInstance() {
        return mongoDbManager;
    }

    public MongoClient getConnection() {
        return mongoClient;
    }
   
    public MongoDatabase getDBManager() {
        return database;
    }

    public void closeConnection() {
        if (mongoClient != null) {
            mongoClient.close();
        }
        ext.trace("====closeConnection()===");
    }
}
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Use mongodb in smartfox server

Postby Lapo » 01 Mar 2019, 11:07

Hi,
yes, you can reuse it, but pay attention that your reference to the SFSExtension is marked as "static".
If you call initalizeConnection() from both Zone and Room extension you need to make sure you don't overwrite the static field.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
zakir
Posts: 4
Joined: 01 Mar 2019, 10:19

Re: Use mongodb in smartfox server

Postby zakir » 01 Mar 2019, 13:03

Hi Lapo,

Thanks for reply, i have write the below code in Room extension to check whether the my mongo connection exist or not and i got the connection does not exist, please help me how can i reuse the connection, give some code example.


PokerRoomExtension.java :-

Code: Select all

import com.smartfoxserver.v2.core.SFSEventType;
import com.smartfoxserver.v2.extensions.SFSExtension;
import sfs2x.extension.poker.db.MongoDBManager;


public class PokerRoomExtension extends SFSExtension {

    @Override
    public void init() {
        trace("PokerRoomExtension.init");
        if (MongoDBManager.getInstance() != null && MongoDBManager.getInstance().getConnection() != null) {
            trace("PokerRoomExtension---> Mongo connections reused ");
        } else {
            trace("PokerRoomExtension---> Mongo connections is null ");
        }
        this.addEventHandler(SFSEventType.USER_JOIN_ROOM, RoomJoinHandler.class);
    }
   
    @Override
    public void destroy(){
        trace("PokerRoomExtension.destroy");
    }
}



RoomJoinHandler.java

Code: Select all

import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;
import org.bson.Document;
import sfs2x.extension.poker.db.MongoDBManager;

/**
 *
 * @author rnf-zakir
 */
public class RoomJoinHandler extends BaseServerEventHandler{

    @Override
    public void handleServerEvent(ISFSEvent isfse) throws SFSException {
        trace("=====RoomJoinHandler.handleServerEvent=====");
        MongoDatabase db = MongoDBManager.getInstance().getDBManager();
        MongoCollection<Document> testCollection = db.getCollection("Test");
        trace("RoomJoinHandler ==> Total Documents Count = " + testCollection.countDocuments());
    }
   
}
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Use mongodb in smartfox server

Postby Lapo » 01 Mar 2019, 15:11

Hi,
Extensions by default are loaded in separate class loaders, to avoid reloading at runtime. This means that your Room and Zone extension cannot share objects by default.
In our documentation we describe the mechanism in detail. I'd recommend reading this section:
http://docs2x.smartfoxserver.com/Extens ... assLoading

There are however multiple ways to work around this limitation.
One is this described here -> https://smartfoxserver.com/blog/the-singleton-solution/
A second one would be to deploy both Zone and Room Extensions to the SFS2X/extensions/_lib_/ folder, so that they are loaded by the same class loader.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
zakir
Posts: 4
Joined: 01 Mar 2019, 10:19

Re: Use mongodb in smartfox server

Postby zakir » 04 Mar 2019, 08:07

Hi Lapo,

Thanks for your reply, now its working.

But i see one stranger thing, room extensions are loading first then the zone extension while the zone extension is parent of room extension.
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Use mongodb in smartfox server

Postby Lapo » 04 Mar 2019, 10:08

Hi,
that's normal: Rooms are created in the Zone before the Zone Extension is created.

If this is a problem in terms of accessing the Zone from your Room Extension init() method I would suggest to use the the SERVER_READY event which fires when all Zones are ready and the server is listening actively on the configured ports.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
zakir
Posts: 4
Joined: 01 Mar 2019, 10:19

Re: Use mongodb in smartfox server

Postby zakir » 04 Mar 2019, 10:32

Hi Lapo,

Thanks for giving clear description about room and zone, my issue get resolved.

Thanks a lots .

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Google [Bot] and 57 guests